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 16
  1. #1
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts

    File Upload Script

    I have an upload script, (below), and i thought that it would upload the same file to two directories...

    Here goes, this is what i have:

    Code:

    Code:
    <?php
    $uploaddir="gallery"; 
    $uploaddirl="largegallery"; 
    if (is_uploaded_file($_FILES['file']['tmp_name']))
     { 
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']); 
    
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddirl.'/'.$_FILES['file']['name']);
     } 
    print "Image Uploaded Successfully" 
    ?>
    my assumption was that if i add another var with another directory and then moved the file twice it would save the file to 2 directories??

    After realising it doesent work, i thought maybe the tmp file is deleted as soon as its moved once? is there a way of saving it to 2 directories?

    Another thing, Instead of displaying a bit of text when the image is uploaded, i would like it to link the user to the album they have uploaded the image to?

    Thanks again

    Jon
    Last edited by jonnybinthemix; 05-08-2006 at 11:26 PM.
    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    mv, or move, takes a file from it's current location and moves it to the destination. It does not copy it.

    What you need is to copy the file.

    http://us2.php.net/manual/en/function.copy.php

    You can use this on the destination file after your first move.

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Code:
        // Simple copy for a file 
        if (is_file($source)) { 
            copy($source, $dest); 
        }

  • #4
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts
    hi guys, and thanks so much for you help...

    i have had a look at the page and the code very kindly writen by you litebearer and i have come up with the following, however, it doesent work

    Code:
    <?php
    
    $uploaddir="gallery";
    $dest="largegalley";
    $source="$_FILES['file']['tmp_name']"
    
    if (is_uploaded_file($_FILES['file']['tmp_name']))
    
    {
    if (if (is_file($source)) {
            copy($source, $dest);
        }
    
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
    }
    
    print "Image Uploaded Successfully"
    
    ?>
    I thought, (thinking i was being clever) that i would have to copy the file before i moved it, otherwise there would be nothing to copy! so i thought i should make the source var to the temp path and the dest var to the destination directory.

    However it looks like i was wrong, but am i on the right track??

    Thanks

    Jon
    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    try this...
    PHP Code:
    <?php 
    if ($_POST['upload']){ 
        
    $uploaddir "gallery/";
        
    $snddir "largegallery/";
        
    $original_name $_FILES['uploadfile']['name']; 
        
    $source $uploaddir $original_name;
        
    $dest $snddir $original_name;
        if(!
    move_uploaded_file($_FILES['uploadfile']['tmp_name'], $uploaddir $_FILES['uploadfile']['name'])){
            echo 
    "Error in uploading file";
            exit();
        }else{
            if(!
    copy($source,$dest)) {
                  echo 
    "Error in copying file";
                  exit();
            }
        }
    }else{ 
        
    ?> 
        <form enctype="multipart/form-data" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post"> 
        <input name="uploadfile" type="file"><br>
        <input name= "upload" type="submit" value="Upload File"> 
        </form> 
        <?php 

    ?>
    Lite...

  • #6
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts
    hi mate,

    I have been trying to implement your code into my script but for various reasons im sure, i cant seem to get it working.

    Would you be able to explain your code, because im sure i understand most of it, but im not sure on a couple of things.

    I tried the following, amongst many other variations, i tried your code complete and it doesent work for me, so i was thinking i could just take the theory of your code and implement it into mine and maybe with a bit of luck i could get it working
    however, heres my attempt!

    Code:
    <?php
    
    $uploaddir="gallery/";
    $snddir="largegallery/";
    $original_name="$_FILES['uploadfile']['name']";
    $source="$uploaddir . $original_name";
    $dest="snddir . $original_name";
    
    if( is_uploaded_file($_FILES['uploadfile']['tmp_name']));
    	{
    
    		move_uploaded_file($_FILES['uploadfile']['tmp_name'], $uploaddir . $_FILES['uploadfile']['name']);
    
    		copy( $source, $dest);
    	}
    
    	print " Image $original_name uploaded to the Random Images Gallery.";
    
    ?>
    i think i can see why it doesent work, and this is probably why you have the if() statement at the top, because the $original_name var cannot be set first because the path doesent exist until the move has been carried out!? just a hunch, so i tried using your if() statement at the top, but i don understand fully so im at a bit of a loss, if you could explain how it works, i will have moe chance of getting something working

    thanks

    Jon
    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #7
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts
    any takers?
    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Ok, let us review step by step.

    1. In order to upload a file you need 2 basic parts

    (A) a form for the user to identify the file to upload

    (B) a bit of code to process the form

    2. The above may be done using 2 separate files (a form file and a process file) OR as in our example it may be accomplished by merging the two processes into one file.

    3. The file needs to know which of the two processes it is supposed do at any given time, hence the IF. Essentially, the IF says "IF the user has filled out the form and pressed the submit, then process the form data; OTHERWISE show the form for the user to complete.

    4. The way you have your script setup, there needs to be two folders just beneath the folder that contains your script. Both of the folders need to be 'writeable'.

    5. Another issue you have is using quotes inappropriately. do this to your code:

    change these two lines from this

    PHP Code:
         $original_name="$_FILES['uploadfile']['name']";
         
    $source="$uploaddir . $original_name"
    to this:

    PHP Code:
         $original_name=$_FILES['uploadfile']['name'];
         
    $source=$uploaddir $original_name
    and change this line from this:

    PHP Code:
         $dest="snddir . $original_name"
    to this:

    PHP Code:
         $dest=$snddir $original_name
    6. Check to make sure that the name you assigned to the file in your form is the same as you use in processing - in my example I use uploadfile as the name.

    See it that helps.

    Lite...

  • #9
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Huraaaay!! lol

    Took me long enough to get there!! lol

    Worked absolutley diamond

    Got me thinking now, of how i can improve lol

    Thanks for your help

    Jon
    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #10
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts
    PHP Code:
    <?php

    $uploaddir
    ="gallery/";
    $snddir="largegallery/";
    $original_name=$_FILES['file']['name'];
    $source=$uploaddir $original_name;
    $dest=$snddir $original_name;

    if( 
    is_uploaded_file($_FILES['file']['tmp_name']));

        {    
    move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir $_FILES['file']['name']);
            
    copy($source$dest);
        }
        print 
    " Image $original_name uploaded to the Random Images Gallery.";
    ?>
    I have this, which works fantastic

    However, typical me, enough is never enough!

    I have tried putting some html at the end but as i thought it dident work, am i able to close the php and use html instead of the print() function at the bottom?

    I would like a message to pop up as it does, saying image.jpg has successfully uploaded to the random image gallery, but i want to be able to format the font and then have something saying please wait wait while we redirect you to the Random Gallery, then maybe it can wait 4 seconds or something and then redirect the user to the gallery.

    Is this possible, or am i aiming too high?

    thanks

    Jon
    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #11
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    First, on this line

    PHP Code:
    if( is_uploaded_file($_FILES['file']['tmp_name'])); 
    get rid of the semi-colon at the end of the line

    Next. to redirect, simply change this

    PHP Code:
        print " Image $original_name uploaded to the Random Images Gallery.";
    ?> 
    to this
    PHP Code:
        print " Image $original_name uploaded to the Random Images Gallery.";
      echo 
    "<B><I>please wait while we redirect you to the Random Gallery</i></b>";

    ?> 
    <META http-equiv="refresh" content="2; URL=new-page-blah-blah"> 
    the 2 represents 2 seconds, change to suit your needs

  • #12
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts
    hi mate i have this:

    PHP Code:
        print "Image $original_name uploaded to the Random Images Gallery.";
          echo 
    "<br><br><B><I>Please wait while we redirect you to the Random Gallery</i></b>";

        
    ?>
    <META http-equiv="refresh" content="4; URL="http://www.fartingfish.com/random.php"> 
    It refreshes every 4 seconds, but refreshes to the same page, not to the link

    any ideas?

    jon
    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #13
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    what is the name of the script file AND what is the name of the file to which you want the user sent?

  • #14
    Regular Coder
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    147
    Thanks
    6
    Thanked 2 Times in 2 Posts
    the script is called upload.php and i want the user to be redirected to the random.php page

    "You only live once, play it right, and once is enough" - me :)
    "When i die only god can take my soul" - me again! :P

  • #15
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Your quotes are off.

    change this
    PHP Code:
    <META http-equiv="refresh" content="4; URL="http://www.fartingfish.com/random.php"> 
    to this
    PHP Code:
    <META http-equiv="refresh" content="4; URL=http://www.fartingfish.com/random.php"


  •  
    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
    •