HOW to upload to two places?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MOzgaf
    New Member
    • Dec 2010
    • 13

    HOW to upload to two places?

    this is what i have so far but it uploads to the first location but not second.
    Code:
    $newname = dirname(__FILE__).'/upload'.$myvar;
    	  $newcopy = dirname(__FILE__).'/copy'.$today.$myvar;
          //Check if the file with the same name is already exists on the server
          if (!file_exists($newname)) {
            //Attempt to move the uploaded file to it's new place
            if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
               echo "It's done! The file has been saved as: ".$newname;
            } else {
               echo "Error: A problem occurred during file upload!<br>";
            }
    		if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newcopy))) {
               echo "It's done! The file has been saved as: ".$newcopy;
            } else {
               echo "Error: A problem occurred during file upload!";
            }
    where the second location prints the file name with a date.
    thanks in advance.
  • Thew
    New Member
    • Aug 2010
    • 69

    #2
    Try this:

    Code:
    <?php
    
    	$newname = dirname(__FILE__).'/upload'.$myvar;
        $newcopy = dirname(__FILE__).'/copy'.$today.$myvar;
    	
        
        if (!file_exists($newname)) {
    		if(!file_exists($newcopy)) {
    			if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname)) 
    			{
    				echo 'File 1 has been uploaded...<br/>';
    				
    					if (move_uploaded_file ($_FILES['uploaded_file']['tmp_name'],$newcopy))
    					{
    						echo 'File 2 too! Lets party! <br/>';
    					}
    					else
    					{
    						echo 'but file 2 wasn t? <br/>';
    					}
    			}
    			else
    			{
    				'File 1 wasn t uploaded so File 2 neither.<br/>';
    			}
    		}
    		else
    		{
    			echo 'File 2 already exists! <br/>';
    		}
    	}
    	else
    	{
    		echo 'File 1 already exists! <br/>';
    	}
    	?>
    What does it display?

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      I think you both miss the concept of "move".

      Once you move it once, it is no longer there!

      You need to COPY the file if you need another 'copy'.


      Dan

      Comment

      • MOzgaf
        New Member
        • Dec 2010
        • 13

        #4
        Thank you thew the code works fine. I used the copy()function as well dlite922. thanks

        Comment

        Working...