Friday, February 11, 2011

Jquery Uploadify: Rename Uploaded File

I have been searching for two days to find a solution which will let me upload employee image and rename it, I couldn't find any. Turns out it's pretty simple, I have to just pass the filename as query string and do a little modification in uploadify.php file.
  1. Passing the filename as part of the query string - followed by '?' mark include the filename with the paramater 'script'
    'script' : 'uploadify/uploadify.php?name=imagename'
    //don't worry about the extension
  2. Modify the following line
    $targetFile =  str_replace('//','/',$targetPath) . 
    $_FILES['Filedata']['name'];

    with this
    if(isset($_GET['name'])){
    $targetFile =  str_replace('//','/',$targetPath) . $_GET['name']. 
            substr($_FILES['Filedata']['name'],-4);
     } else{
        $targetFile =  str_replace('//','/',$targetPath) .
          $_FILES['Filedata']['name'];
     }
I hope this is helpful

7 comments:

  1. thanks for you for this solution you help me.

    ReplyDelete
  2. I tried to follow what you were saying, but when I upload a file (or the uploader does) it doesn't give them the option of renaming the file.

    If that is not an option, is there a way to have the system automatically name the file with the uploaders IP address?

    ReplyDelete
  3. I had been struggling with this for several weeks. I had not contemplated adding a parameter to the "script". Silly really, works a treat. Thanks

    ReplyDelete
  4. This is a good method, but if you are using onComplete after the file has been uploaded, it doesn't mention that the fileObj parameter in the callback contains the original filename. You need to use the response parameter instead.

    http://inglepingle.co.uk/2012/02/uploadify-how-to-rename-an-uploaded-file/

    ReplyDelete
  5. Here is the solution for the same, I posted on stackoverflow answers.

    http://stackoverflow.com/questions/10896715/rename-file-name-using-uploadify/16403822#16403822

    ReplyDelete