So that we can separate upload file into user specific directory?

Comments

skilip’s picture

You can do this using hook_swfupload on operation 'move_uploaded_file'. Here's an example:

/**
 * Implementation of hook_swfupload().
 */
function MODULENAME_swfupload(&$file, $op, &$instance) {
  switch ($op) {
    case 'move_uploaded_file':
      global $user;
      $file->filepath = "files/$user->uid/"; // Files will be stored in an user folder
      break;
  }
}
martinwillb’s picture

Where does this line of code go exactly...in the swfupload module itself? Where will it allow me to use the %UID wildcard so I can get my multiple uploaded files in my users directory.

thanks

mattyoung’s picture

You make your own module and put that function there.

martinwillb’s picture

I'm new to Drupal. How much coding is involved with creating my own module. I know it's got to be more than just coping the code above into notepad and naming it with a .module extension. Can you point me to a post or issue in reference to this?

Thanks

mattyoung’s picture

Pick a name for the module, I'll use 'MODULENAME', you change it to the name you want. Module name should be all lower case with no underscore in them.

create a directory 'MODULENAME' in the module directory, this can be sites/all/modules or sites/example.com/modules

Save this to MODULENAME.info inside the 'MODULENAME' directory:

; $Id$
name = "My module"
description = "Customize swfupload filepath"
core = "6.x"

Change name and description to your liking.

Save this to MODULENAME.module inside the 'MODULENAME' directory:

<?php
/**
* Implementation of hook_swfupload().
*/
function MODULENAME_swfupload(&$file, $op, &$instance) {
  switch ($op) {
    case 'move_uploaded_file':
      global $user;
      $file->filepath = "files/$user->uid/"; // Files will be stored in an user folder
      break;
  }
}

Note: there is no closing '?>' tag!

Go to admin/build/modules, enable the module.

martinwillb’s picture

OK got everything done. So now this will allow me to use %uid or %username in the "File system path setting" in the File System menu options?

mattyoung’s picture

Change this line to whatever path you want:

$file->filepath = "files/$user->uid/"; // Files will be stored in an user folder
martinwillb’s picture

Is this all I have to do. Do I need to make any changes in any admin settings? Should I see any options in reference to this module in my menu settings and admin settings?

martinwillb’s picture

So ->filepath = "files/$user-> is the example path to get to the user id folder uid? Where do I use the %uid?

mattyoung’s picture

No need for %uid or set any admin settings. The module is taking over by setting this variable:

$file->filepath = "files/$user->uid/"; // Files will be stored in an user folder

the expression $user->uid is the user id #, for user #3, the file will be stored in the directory files/3. I suggest you use something like this:

$file->filepath = "files/upload-files/$user->uid/"; // Files will be stored in an user folder

so every user will have a directory inside the files/upload-files directory. You can set this varialble to whatever value you like and the uploaded file will end up storing in that directory.

martinwillb’s picture

Is this suppose to work for multiple uploades? I'm performing multiple uploads, I have done what you stated in your previous posts and everything I upload, using the multple file upload options, goes to my sites/default/files/users path. This path is set in I have IMCE, IMCE SWFUpload, and SWFupload modules enabled.

mattyoung’s picture

>Is this suppose to work for multiple uploades?

yes it should work

>everything I upload, using the multple file upload options, goes to my sites/default/files/users

what's your code look like? The file should be stored in whatever the path you set there. This overrides anything you set in settings. Maybe you hook is not working, could be it's not being called. You need to debug this. Show me you code.

martinwillb’s picture

Here's the code below for the hook module. I want uploads to go into (example: /users/user1 if user on is logged in and /users/user2 if user2 is logged in.

I had to reinstall my Drupal and update everything again. I was having problems with the GUI displaying. There were no graphics. I got it back up and now I'm ready again. I have IMCE, IMCE SWFupload, SWFupload, and my hook module >hookswfupload.module< enabled.

<?php

/**

* Implementation of hook_swfupload().

*/

function MODULENAME_swfupload(&$file, $op, &$instance) {

switch ($op) {

case 'move_uploaded_file':

global $user;

$file->filepath = "files/users/$user->uid/";// Files will be stored in an user folder
break;

}

}

mattyoung’s picture

change

function MODULENAME_swfupload(&$file, $op, &$instance) {

to

function hookswfupload_swfupload(&$file, $op, &$instance) {
martinwillb’s picture

That was it!!!!! I was not calling the d#@&# module. I'm good to go now thanks!!! I just need to check the permissons for my user. as admin I can upload anything but my user can't

pearson.gn’s picture

One caution here. The file path may be something different like 'sites/default/files/$user->uid/' depending where the directory is located... i.e. the full file system path. I lost some time trying to figure out why my files didn't show up anywhere.

Great stuff. Thanks for the tips.

hoquangthaiholy’s picture

Simple Enable module PHP Filter and use php: return 'users/'.$user->name; to do this

skilip’s picture

Status: Active » Fixed
skilip’s picture

Version 6.2 allows you to use tokens in the (cck) field settings.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.