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.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    uploading jpg three times to three different locations

    Hello, i've searched the site and the web for this but i could not find anything.

    what i need is when i upload 1.jpg file onto the server using $_FILES[] i need at the same time to save the same file to the following folders:

    -photos/
    -photos/thumbs/
    -photos/big/

    here's my script so far (simplified):
    PHP Code:
       //defining variables
    $imagename "1.jpg";
    $source $_FILES['foto1']['tmp_name'];
    $target "../photos/".$imagename;

       
    // 1st time target path is just photos
    move_uploaded_file($source$target);
    imagejpeg($imagename$target100);

       
    // 2nd time target includes 'big'
    $target "../photos/big/".$imagename;
    move_uploaded_file($source$target);
    imagejpeg($imagename$target100);

       
    // 3rd time target includes 'thumbs'
    $target "../photos/thumbs/".$imagename;
    move_uploaded_file($source$target);
    imagejpeg($imagename$target100); 
    it simply doesnt work, i hope that was clear enough, thank you in advance!
    regards!
    Last edited by illusia.it; 03-19-2010 at 06:11 PM. Reason: didnt embed php

  • #2
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    still me, i mean it does save 1.jpg into photos/ but it doesnt save the same image into the other 2 folders!!! thanks again

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    // 2nd time target includes 'big'
    $target = "../photos/big/".$imagename;
    move_uploaded_file($source, $target);
    imagejpeg($imagename, $target, 100);

    // 3rd time target includes 'thumbs'
    $target = "../photos/thumbs/".$imagename;
    move_uploaded_file($source, $target);
    imagejpeg($imagename, $target, 100);
    You can't use move_uploaded_file() more than once to read from the same source, as the file will not be there after first move (it's not like a copy).

    Thus for the second and third block, use the target of initial block as the source and use the imagejpeg() to make new files(makes sure that it won't overwrite the existing one). After that use the functions copy() and unlink() (to delete after copying to the targets)
    Last edited by abduraooft; 03-19-2010 at 01:13 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    illusia.it (03-19-2010)

  • #4
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by abduraooft View Post
    You can't use move_uploaded_file() more than once to read from the same source, as the file will not be there after first move (it's not like a copy).

    Thus for the second and third block, use the target of initial block as the source and use the imagejpeg() to make new files(makes sure that it won't overwrite the existing one). After that use the functions copy() and unlink() (to delete after copying to the targets)

    Thank you very much, that is exactly what i came up with a moment ago, i figured it out, only it was a little more complicated because the images of each folder had to have a specific size, here is the code i came up with, hopig it will help someone (as non experienced as me):

    PHP Code:
    $counter 1;
    foreach(
    $_FILES as $key){ // loops as many times as the files are (9)
        
    if(!$_FILES['foto'.$counter.'']['error']){ // but saves the picture only as many as the files uploaded were
            
            
    $imagename $counter.".jpg";
            
    $source $_FILES['foto'.$counter.'']['tmp_name'];
            
    $target "../photos/carpenteria_strutture".$id."/big/".$imagename;
            
    $target2 "../photos/carpenteria_strutture".$id."/".$imagename;
            
    $target3 "../photos/carpenteria_strutture".$id."/thumbs/".$imagename;
            
            
    move_uploaded_file($source$target);
            
            
    $target "../photos/carpenteria_strutture".$id."/big/".$imagename;
            list(
    $width$height) = getimagesize($target);
            if(
    $height>$width){
                
    $modwidth=450;
            }else{
                
    $modwidth=800;
            }
            
    $diff $width $modwidth;
            
    $modheight $height $diff;
            
    $tn imagecreatetruecolor($modwidth$modheight);
            
    $image imagecreatefromjpeg($target);
            
    imagecopyresampled($tn$image0000$modwidth$modheight$width$height);
            
    imagejpeg($tn$target100);
            
            
    // copy files from big and paste them into ../ and thumbs/
            // then pick them up and resize them
            
    copy($target$target2);
            
    copy($target$target3);
            
            list(
    $width$height) = getimagesize($target2);
            if(
    $height>$width){
                
    $modwidth=101;
            }else{
                
    $modwidth=180;
            }
            
    $diff $width $modwidth;
            
    $modheight $height $diff;
            
    $tn imagecreatetruecolor($modwidth$modheight);
            
    $image imagecreatefromjpeg($target);
            
    imagecopyresampled($tn$image0000$modwidth$modheight$width$height);
            
    imagejpeg($tn$target2100);
            
            list(
    $width$height) = getimagesize($target3);
            if(
    $height>$width){
                
    $modwidth=32;
            }else{
                
    $modwidth=57;
            }
            
    $diff $width $modwidth;
            
    $modheight $height $diff;
            
    $tn imagecreatetruecolor($modwidth$modheight);
            
    $image imagecreatefromjpeg($target);
            
    imagecopyresampled($tn$image0000$modwidth$modheight$width$height);
            
    imagejpeg($tn$target3100);
            
        }
        
    $counter++; // counter keeps counting regardless (9 times)

    Last edited by illusia.it; 03-19-2010 at 06:12 PM. Reason: mistake in the script

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    hopig it will help someone (as non experienced as me):
    Nice! I'd recommend to edit your post and add [PHP][/PHP] tags arounf your php code.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    illusia.it (03-19-2010)

  • #6
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Nice! I'd recommend to edit your post and add [PHP][/PHP] tags arounf your php code.
    Yes, Thank you for that, it looks so much better now! however i could not find a way to edit my previous post but here is the code again

    PHP Code:

    $counter 
    1;
    foreach(
    $_FILES as $key){ // loops as many times as the files are (9)
    if(!$_FILES['foto'.$counter.'']['error']){ // but saves the picture only as many as the files uploaded were

    $imagename $counter.".jpg";
    $source $_FILES['foto'.$counter.'']['tmp_name'];
    $target "../photos/carpenteria_strutture".$id."/big/".$imagename;
    $target2 "../photos/carpenteria_strutture".$id."/".$imagename;
    $target3 "../photos/carpenteria_strutture".$id."/thumbs/".$imagename;

    move_uploaded_file($source$target);

    $target "../photos/carpenteria_strutture".$id."/big/".$imagename;
    list(
    $width$height) = getimagesize($target);
    if(
    $height>$width){
    $modwidth=450;
    }else{
    $modwidth=800;
    }
    $diff $width $modwidth;
    $modheight $height $diff;
    $tn imagecreatetruecolor($modwidth$modheight);
    $image imagecreatefromjpeg($target);
    imagecopyresampled($tn$image0000$modwidth$modheight$width$height);
    imagejpeg($tn$target100);

    // copy files from big and paste them into ../ and thumbs/
    // then pick them up and resize them
    copy($target$target2);
    copy($target$target3);

    list(
    $width$height) = getimagesize($target2);
    if(
    $height>$width){
    $modwidth=101;
    }else{
    $modwidth=180;
    }
    $diff $width $modwidth;
    $modheight $height $diff;
    $tn imagecreatetruecolor($modwidth$modheight);
    $image imagecreatefromjpeg($target);
    imagecopyresampled($tn$image0000$modwidth$modheight$width$height);
    imagejpeg($tn$target2100);

    list(
    $width$height) = getimagesize($target3);
    if(
    $height>$width){
    $modwidth=32;
    }else{
    $modwidth=57;
    }
    $diff $width $modwidth;
    $modheight $height $diff;
    $tn imagecreatetruecolor($modwidth$modheight);
    $image imagecreatefromjpeg($target);
    imagecopyresampled($tn$image0000$modwidth$modheight$width$height);
    imagejpeg($tn$target3100);

    }
    $counter++; // counter keeps counting regardless (9 times)

    Last edited by illusia.it; 03-19-2010 at 06:13 PM. Reason: mistake in the script

  • #7
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    oo one more thing.. i was finally able to edit the previous post, that's weird but i could have sworn the edit button was not visible, now i see it o well.

    hey and i was looking for a way to mark this thread as closed/resolved so that i will be able to do it in the future too.. thank you so much for your help, god bless!

  • #8
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    See #6 of http://www.codingforums.com/postguide.htm!

    PS: There's a "delete" button too, if you think one of your post is not required
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    illusia.it (03-19-2010)


  •  

    Tags for this Thread

    Posting Permissions

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