SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    How do I change the filename in my upload?

    I am resizing an uploaded image and I want to store it in a folder with a random code (called $random).
    But, I also want to change the filename of the uploaded file to the same random code.
    But, where in this script can I make this?

    I want the thumbnail image to be stored in the random code folder with the name $random.jpg

    PHP Code:
    define('UPLOADED_IMAGE_DESTINATION''../temp_photo/');
    define('THUMBNAIL_IMAGE_DESTINATION''../photo/'.$random.'/');

    function 
    process_image_upload($field)
    {
        
    $temp_image_path $_FILES[$field]['tmp_name'];
        
    $temp_image_name $_FILES[$field]['name'];
        list(, , 
    $temp_image_type) = getimagesize($temp_image_path);
        if (
    $temp_image_type === NULL) {
            return 
    false;
        }
        switch (
    $temp_image_type) {
            case 
    IMAGETYPE_GIF:
                break;
            case 
    IMAGETYPE_JPEG:
                break;
            case 
    IMAGETYPE_PNG:
                break;
            default:
                return 
    false;
        }
        
    $uploaded_image_path UPLOADED_IMAGE_DESTINATION $temp_image_name;
        
    move_uploaded_file($temp_image_path$uploaded_image_path);
        
    $thumbnail_image_path THUMBNAIL_IMAGE_DESTINATION preg_replace('{\\.[^\\.]+$}''.jpg'$temp_image_name);
        
    $result generate_image_thumbnail($uploaded_image_path$thumbnail_image_path);
        return 
    $result ? array($uploaded_image_path$thumbnail_image_path) : false;


  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $temp_image_name $_FILES[$field]['name']; 
    Change that to use $random . '.jpg';

    Granted you need to tell the function about $random either using global $random or passing it as an argument.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I changed this, but in return I only get .jpg in return.
    No filename at all. So, it doesn't seem to work.

    If I set $random to 1234 like this:

    PHP Code:
    $random '1234';

    define('UPLOADED_IMAGE_DESTINATION''../temp_photo/');
    define('THUMBNAIL_IMAGE_DESTINATION''../photo/'.$random.'/');

    function 
    process_image_upload($field)
    {
        
    $temp_image_path $_FILES[$field]['tmp_name'];
        
    $temp_image_name $random '.jpg';
        list(, , 
    $temp_image_type) = getimagesize($temp_image_path);
        if (
    $temp_image_type === NULL) {
            return 
    false;
        }
        switch (
    $temp_image_type) {
            case 
    IMAGETYPE_GIF:
                break;
            case 
    IMAGETYPE_JPEG:
                break;
            case 
    IMAGETYPE_PNG:
                break;
            default:
                return 
    false;
        }
        
    $uploaded_image_path UPLOADED_IMAGE_DESTINATION $temp_image_name;
        
    move_uploaded_file($temp_image_path$uploaded_image_path);
        
    $thumbnail_image_path THUMBNAIL_IMAGE_DESTINATION preg_replace('{\\.[^\\.]+$}''.jpg'$temp_image_name);
        
    $result generate_image_thumbnail($uploaded_image_path$thumbnail_image_path);
        return 
    $result ? array($uploaded_image_path$thumbnail_image_path) : false;


  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Well, it does, but as I previously mentioned your function doesn't have access to $random. You need to use global $random; (first line in your function) or pass $random to your function as an argument process_image_upload($field, $random)
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by cpradio View Post
    Granted you need to tell the function about $random either using global $random or passing it as an argument.
    As cpradio said, you need to tell the function that you are passing a new name into it.
    PHP Code:
    function process_image_upload($field$random
    Its called variable scope.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Nope, that doesn't work either...

    I must be stupid, but I think I'm doing it right...
    It's still a blank name and only .jpg

    Here is my code now:

    PHP Code:
    $random '1234';

    define('UPLOADED_IMAGE_DESTINATION''../temp_photo/');
    define('THUMBNAIL_IMAGE_DESTINATION''../photo/'.$random.'/');

    function 
    process_image_upload($field$random)
    {
        
    $temp_image_path $_FILES[$field]['tmp_name'];
        
    $temp_image_name $random '.jpg';
        list(, , 
    $temp_image_type) = getimagesize($temp_image_path);
        if (
    $temp_image_type === NULL) {
            return 
    false;
        }
        switch (
    $temp_image_type) {
            case 
    IMAGETYPE_GIF:
                break;
            case 
    IMAGETYPE_JPEG:
                break;
            case 
    IMAGETYPE_PNG:
                break;
            default:
                return 
    false;
        }
        
    $uploaded_image_path UPLOADED_IMAGE_DESTINATION $temp_image_name;
        
    move_uploaded_file($temp_image_path$uploaded_image_path);
        
    $thumbnail_image_path THUMBNAIL_IMAGE_DESTINATION preg_replace('{\\.[^\\.]+$}''.jpg'$temp_image_name);
        
    $result generate_image_thumbnail($uploaded_image_path$thumbnail_image_path);
        return 
    $result ? array($uploaded_image_path$thumbnail_image_path) : false;


  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Find where you are calling process_image_upload($field); and make sure to change it to process_image_upload($field, $random);

    You can't just change the function definition and assume everywhere you call that function will make use of the new definition.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, I still don't get it. There is no file name attached to the thumbnail.
    The only thing I have after the above code, is this:

    Is there anything I need to change there as well?

    One page is handling the form for the upload. Then the user ends up on this page and the resize and upload process is handled.
    And this is where I want the $random to replace the original filename. Then the image is supposed to end up in a folder with the $random as folder name and the image stored in that folder with the $random as file name with .jpg added.

    But, after doing what you wrote above, I still just get an image called .jpg

    PHP Code:
    $result process_image_upload('Image1');
    if (
    $result === false) {
        echo 
    '<br>An error occurred while processing upload';
    } else {
        echo 
    '<br>Uploaded image saved as ' $result[0];
        echo 
    '<br>Thumbnail image saved as ' $result[1];


  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,071
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    You need to change the following line:
    PHP Code:
    $result process_image_upload('Image1'); 
    to be
    PHP Code:
    $result process_image_upload('Image1'$random); 
    So you are passing the value to the function.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  10. #10
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Of course that had to be changed as well. Now it's working.
    I said I was stupid, right?

    Thanks a lot... sometimes my head is spinning the wrong way and I start thinking about other things.
    Or maybe too many projects running at the same time...


Bookmarks

Posting Permissions

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