Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts

    Image upload, change directory path

    So this image uploader successfully uploads images to my website, but it's putting them in the same directory as the script is running from. I want it in the next folder named "pimages" which stands for Profile Images.

    maindirectory/pimages/file.jpg

    Also is there any way of changing the file name to a unique name? right now it keeps the same exact file name, and that is going to cause problems, I need a random but unique salt name.

    Thanks

    PHP Code:
     $upload_image_limit 1// How many images you want to upload at once?
    $upload_dir ""// default script location, use relative or absolute path
    $enable_thumbnails // set 0 to disable thumbnail creation

    ##################### THUMBNAIL CREATER FROM GIF / JPG / PNG

    function make_thumbnails($updir$img){

        
    $thumbnail_width    150;
        
    $thumbnail_height    150;
        
    $thumb_preword        "thumb_";

        
    $arr_image_details    GetImageSize("$updir"."$img");
        
    $original_width        $arr_image_details[0];
        
    $original_height    $arr_image_details[1];

        if( 
    $original_width $original_height ){
            
    $new_width    $thumbnail_width;
            
    $new_height    intval($original_height*$new_width/$original_width);
        } else {
            
    $new_height    $thumbnail_height;
            
    $new_width    intval($original_width*$new_height/$original_height);
        }

        
    $dest_x intval(($thumbnail_width $new_width) / 2);
        
    $dest_y intval(($thumbnail_height $new_height) / 2);



        if(
    $arr_image_details[2]==1) { $imgt "ImageGIF"$imgcreatefrom "ImageCreateFromGIF";  }
        if(
    $arr_image_details[2]==2) { $imgt "ImageJPEG"$imgcreatefrom "ImageCreateFromJPEG";  }
        if(
    $arr_image_details[2]==3) { $imgt "ImagePNG"$imgcreatefrom "ImageCreateFromPNG";  }


        if( 
    $imgt ) {
            
    $old_image    $imgcreatefrom("$updir"."$img");
            
    $new_image    imagecreatetruecolor($thumbnail_width$thumbnail_height);
            
    imageCopyResized($new_image,$old_image,$dest_x,
            
    $dest_y,0,0,$new_width,$new_height,$original_width,$original_height);
            
    $imgt($new_image,"$updir"."$thumb_preword"."$img");
        }

    }









    ################################# UPLOAD IMAGES

            
    foreach($_FILES as $k => $v){

                
    $img_type "";

                
    ### $htmo .= "$k => $v<hr />";     ### print_r($_FILES);

                
    if( !$_FILES[$k]['error'] && preg_match("#^image/#i"$_FILES[$k]['type']) && $_FILES[$k]['size'] < 500000){

                    
    $img_type = ($_FILES[$k]['type'] == "image/jpeg") ? ".jpg" $img_type ;
                    
    $img_type = ($_FILES[$k]['type'] == "image/gif") ? ".gif" $img_type ;
                    
    $img_type = ($_FILES[$k]['type'] == "image/png") ? ".png" $img_type ;

                    
    $img_rname $_FILES[$k]['name'];
                    
    $img_path $upload_dir.$img_rname;

                    
    copy$_FILES[$k]['tmp_name'], $img_path );
                    if(
    $enable_thumbnailsmake_thumbnails($upload_dir$img_rname);
                    
    $feedback .= "Image and thumbnail created $img_rname<br />";

                }
            }


            
    $form_img .= '<label>Image '.$i.': </label> <input type="file" name="uplimg'.$i.'"><br />';
            
    $htmo .= '
            <p>'
    .$feedback.'</p>
            <form method="post" enctype="multipart/form-data">
                '
    .$form_img.' <br />
                <input type="submit" value="Upload Images!" style="margin-left: 50px;" />

            </form>
            '
    ;
                    
    $page .= "$htmo"

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by myfayt View Post
    So this image uploader successfully uploads images to my website, but it's putting them in the same directory as the script is running from. I want it in the next folder named "pimages" which stands for Profile Images.

    maindirectory/pimages/file.jpg
    The part you're looking for is:
    PHP Code:
    $upload_dir $_SERVER['DOCUMENT_ROOT']."/pimages/"// default script location, use relative or absolute path 
    That should change your upload directory to something like /var/www/pimages/ or where ever this script is executing.

    Quote Originally Posted by myfayt View Post
    Also is there any way of changing the file name to a unique name? right now it keeps the same exact file name, and that is going to cause problems, I need a random but unique salt name.

    Thanks
    Yes, but be mindful of the implications. If you start assigning random names, you'll have to keep track of the filenames some way, whether by SQL or using scandir() to output a full folder. Whatever you do, you'll have to make a change to the $img_rname variable before the copy() statement. Be mindful about file extensions when editing, refer to this post: http://www.codingforums.com/showthread.php?t=237619

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    I tried doing this

    PHP Code:
    $upload_dir "/pimages"// default script location, use relative or absolute path 
    and it didn't change the path.

    As for the unique name, it would be stored in the database, and extracted to show in profiles. Meaning like h98hr3gf74gt7.jpg would be in a field name of something like "image1" in the database, and on the users profile, it'd extract their image 1 to display.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Storing them in the mysql is acceptable. So, all you need to do is generate a random name (could just be filename.rand().extension?) and then assign it to the database on copy success (after the copy function).

    As for your upload dir, changing $upload_dir to /pimages will mean your uploades files will stay in the same directory, but be called pimagesfilename.jpg. You need to add a / at the end of it (notice my example has it). It's because you're concatenating the image name right onto the upload_dir variable.

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Ok so this should work?

    PHP Code:
    $upload_dir "/pimages/"// default script location, use relative or absolute path 
    Also I can't figure out which variable is the file name, I am assuming it's $img?
    I've only used RAND for numbers, and I hear MT_RAND is better. But how would letters be added? Or do I do a strlen of 7 numbers?

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Random letters can be done in a few ways.
    PHP Code:
    $alphabet = array( 'a','b',...,'z' );
    $letter $alphabet[rand(0,25)];

    // or
    $alphabet 'abcdefghijklmnopqrstuvwxyz';
    $letter substr($alphabetrand(0,25), 1); 
    You could just put one of those in a for loop appending a string with the random letters, then add that string to the filename (which is down by copy() btw - $img_rname). Using mt_rand() is SUPPOSED to give a better random number, but I don't really think there's a noticable difference. rand() does fine, and is faster.

    And yes, that path should work.

  • #7
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Makes sense, would something like this work?

    PHP Code:
    $alphabet 'abcdefghijklmnopqrstuvwxyz';
    $letter substr($alphabetrand(0,25), 1);
    copy$_FILES[$k]['tmp_name']$letter$img_path ); 

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by myfayt View Post
    Makes sense, would something like this work?

    PHP Code:
    $alphabet 'abcdefghijklmnopqrstuvwxyz';
    $letter substr($alphabetrand(0,25), 1);
    copy$_FILES[$k]['tmp_name']$letter$img_path ); 
    No. It's copy(source, destination). So, changing the source will mean your code will not find the image. On top of that, to concatenate strings (append one string onto another) you need to use the . operator. Proper use would be $var.$letter

    But, remember what I said in a previous reply - you'll need to account for file extension when renaming your file. You can choose your prefered method in the thread I linked you to in post #4.

  • #9
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    I gotcha, so basically like this?

    PHP Code:
     copy$_FILES[$k]['tmp_name'], $img_path ); 
    $alphabet 'abcdefghijklmnopqrstuvwxyz';
    $letter substr($alphabetrand(0,25), 1); 
    $newname $_FILES[$k][$letter][$img_type];
    //Store $newname into database 

  • #10
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by myfayt View Post
    I gotcha, so basically like this?

    PHP Code:
     copy$_FILES[$k]['tmp_name'], $img_path ); 
    $alphabet 'abcdefghijklmnopqrstuvwxyz';
    $letter substr($alphabetrand(0,25), 1); 
    $newname $_FILES[$k][$letter][$img_type];
    //Store $newname into database 
    No. Did you even look at the post I referred you to? :P

    $_FILES[$k]['name'] is the filename.extension (imagename.jpg). So, you need to decide what you want to do to the filename to randomise it, and then create it following the post I linked you to before.

  • #11
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    I am trying my best, I am brand new to dealing with files. So bare with me.

    PHP Code:
    $img_rname $_FILES[$k]['name'];
                    
    $img_path $upload_dir.$img_rname
    That is what I need to change, right?

  • #12
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    I need to bump this because I still haven't gotten a fix yet.

  • #13
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    I keep telling you to check out the post I've linked you to, so I'll just post it in here.
    PHP Code:
    // Get the name of the file (including file extension). 
        
    $file12 $_FILES['site_url']['name'];  

        
    $file12 preg_replace('/\s/''_'$file12); 

        
    // Find extension position 
        
    $position strrpos($file12'.'); 
        
    $filename substr($file12,0,$position).$rand.substr($file12,$position); 
    So, change your code to this:
    PHP Code:
    $rand rand(conditions);
    $img_rname $_FILES[$k]['name'];
    $position strrpos($img_rname'.'); 
    $img_rname substr($img_rname,0,$position).$rand.substr($img_rname,$position);
    $img_path $upload_dir.$img_rname

  • #14
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    There was no link to anything in post #4

  • #15
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    It was actually post #2 I linked it to you in, so apologies for not telling you the right one. Regardless, you should have read it in post 2 anyway.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •