• Resolved Kai2810

    (@kai2810)


    Hi,

    First off I like to thank Mark for making this great plugin available to all of us for free!

    I have been trying to get the upload widget to store an uploaded pic into a particular directory. For example I want all uploaded pics to be stored in the /gallery folder.

    However the plugin will create another directory based on the post ID i think, and place the uploaded pic into that sub folder.

    I toyed around with the widget settings, but can’t get it working…

    Any advice?

    Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter Kai2810

    (@kai2810)

    hi…anyone has any advice for this?

    I’m quite desperate to get this going, any advice will be MUCH appreciated!

    You’ll have to hack the code to get it use a different directory format. It uses post_ID so if there are two files uploaded with the same name on different posts, they wont’ conflict, so you’ll have to keep that in mind.

    Look in the file upload/tdomf-upload-functions.php, around line 551:

    $postdir = $options['path'].DIRECTORY_SEPARATOR.$post_ID;
    tdomf_recursive_mkdir($postdir,TDOMF_UPLOAD_PERMS);
    $newpath = $postdir.DIRECTORY_SEPARATOR.$theirfiles[$i]['name'];

    The $newpath variable stores the path where the file will be uploaded to, so you can modify it how you like, though I recommend prefix it with the $post_ID to prevent conflicts.

    Thread Starter Kai2810

    (@kai2810)

    oh…conflict between similar file names…didn’t think about that.

    Is it possible to do a IF-ELSE statement? Say if the file uploaded conflicts with another file with a similar name, it will add a running number behind the new file’s name?

    Much like how Windows handles duplicate file names?

    Of course you could. Just use php’s file_exists.

    $count = 0;
    while(file_exists($newpath)) {
        $newpath = $postdir.DIRECTORY_SEPARATOR.$theirfiles[$i]['name'].$count;
        $count++;
    }

    I would, instead, put the $post_ID as a prefix to the filename. That should avoid all conflicts too.

    $newpath = $postdir.DIRECTORY_SEPARATOR.$post_ID.'_'.$theirfiles[$i]['name'];

    Thread Starter Kai2810

    (@kai2810)

    Hey Mark, I tried modifying the code as you suggested, into the following.

    // move file
          $postdir = $options['path'];
          tdomf_recursive_mkdir($postdir,TDOMF_UPLOAD_PERMS);
          $count = 0;
    while(file_exists($newpath)) {
    $newpath = $postdir.DIRECTORY_SEPARATOR.$post_ID.'_'.$theirfiles[$i]['name'];
    }

    I gave me the following errors.

    Warning: rename(/home/********/public_html/********/wp-content/gallery/food/tmp/1/admin/P1030298.JPG,) [function.rename]: No such file or directory in /home/********/public_html/********/wp-content/plugins/tdo-mini-forms/include/tdomf-upload-functions.php on line 557

    Warning: Cannot modify header information – headers already sent by (output started at /home/********/public_html/********/wp-content/plugins/tdo-mini-forms/include/tdomf-upload-functions.php:557) in /home/********/public_html/********/wp-content/plugins/tdo-mini-forms/tdomf-form-post.php on line 242

    Anything I did wrong?

    You didn’t initialize the $newpath variable before entering the while loop. So it never enters the content in the loop. Also, if it did enter the loop, you could hit an infinite loop if there so happens to be a file name with the same name. I would do something like this:

    // move file
    
    // directory to store uploads
    $postdir = $options['path'];
    // create this directory if it doesn't exist
    tdomf_recursive_mkdir($postdir,TDOMF_UPLOAD_PERMS);
    
    // now calculate the initial file name
    
    $filename = $theirfiles[$i]['name'];
    $newpath = $postdir.DIRECTORY_SEPARATOR.$filename;
    
    // is that filename in use and if it is, prepend post ID to it
    
    if(file_exists($newpath)) {
       $filename = $post_ID.'_'.$theirfiles[$i]['name'];
       $newpath = $postdir.DIRECTORY_SEPARATOR.$filename;
    }
    
    // if conflict still exists, prepend a counter
    
    $count = 0;
    while(file_exists($newpath)) {
       $count++;
       $newpath = $postdir.DIRECTORY_SEPARATOR.$count.'_'.$filename;
    }
    Thread Starter Kai2810

    (@kai2810)

    It works perfectly!

    LOL I was looking through my files using Filezilla after my own modification, and found a pic file with a 0 after the .jpg filename, rendering the pic a .jpg0 file.

    Realized I placed the postID directly after the $filename…

    Looks like my coding skills have a long way to go!

    Thanks again Mark!

    Is there any way this can be combined with using NextGen Gallery or any other gallery plugin? Say, the gallery plugin takes the images from the folder specified for the upload from TDO mini forms?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Plugin: TDO Mini Forms] Help…Can’t get pic to upload to correct directory….’ is closed to new replies.