copy files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anfetienne
    Contributor
    • Feb 2009
    • 424

    copy files

    Hi,

    I am using the following copy code to move all files from 1 folder to another but i always get an error....can someone please tell me where i am going wrong with this piece of coding.

    here is the code:

    Code:
    <?
    copy($tempFLDR, $path) or die ;
    ?>
    here is the error message:

    Warning: copy(upload/imgtemp/1369012657/IMG00038.jpg) [function.copy]: failed to open stream: No such file or directory in /home/vecre0/public_html/test/8/addimgck.php on line 86
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by anfetienne
    Hi,

    I am using the following copy code to move all files from 1 folder to another but i always get an error....can someone please tell me where i am going wrong with this piece of coding.

    here is the code:

    Code:
    <?
    copy($tempFLDR, $path) or die ;
    ?>
    here is the error message:

    Warning: copy(upload/imgtemp/1369012657/IMG00038.jpg) [function.copy]: failed to open stream: No such file or directory in /home/vecre0/public_html/test/8/addimgck.php on line 86
    Considering the error complains about "No such file or directory", I would be inclined to ask:

    1. Does the .jpg file exist at the path shown.
    2. Does the destination path (in the $path variable) exist?

    Regards,

    Jeff

    Comment

    • anfetienne
      Contributor
      • Feb 2009
      • 424

      #3
      yes it does exist.....the file names are

      1b.jpg
      2b.jpg
      3b.jpg etc etc and the location does exist. All the files upload perfectly fine but it just wont copy over

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by anfetienne
        yes it does exist.....the file names are

        1b.jpg
        2b.jpg
        3b.jpg etc etc and the location does exist. All the files upload perfectly fine but it just wont copy over
        To tell you the truth, we are going to have to see the actual code you are using because that error says different. Can you post it here please?

        Regards,

        Jeff

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          IMG00038.jpg is not the same as 1b.jpg. There's not enough detail here for us to give you an answer. What does $tempFLDR contain? The name suggests it's a folder.. What does $path contain? Again, the name suggests a path to a directory not a file.

          Comment

          • anfetienne
            Contributor
            • Feb 2009
            • 424

            #6
            here is the full coding to the file

            Code:
            <!doctype html public "-//w3c//dtd html 3.2//en">
            <html>
            <head>
            <title>Multiple image upload script from plus2net.com</title>
            </head>
            <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
            <?
            
            $random_digit = $_POST['ID'];
            $returnURL = $_POST['returnURL'];
            
            $imgtemp=mkdir("upload/imgtemp/$random_digit", 0777); 
            
            
            ?>
            
            
            
            <?
            while(list($key,$value) = each($_FILES['images']['name']))
            		{
            			if(!empty($value))
            			{
            				$filename = $value;
            					$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
            
            					$add = "upload/$random_digit/$filename";
            					$tempFLDR = "upload/imgtemp/$random_digit/$filename";
                                   //echo $_FILES['images']['type'][$key];
            			     // echo "<br>";
            					copy($_FILES['images']['tmp_name'][$key], $add);
            					chmod("$add",0777);
            					copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
            					chmod("$tempFLDR",0777);
            					
            $folderPath="upload/$random_digit/";
            			}
            		}
            ?>
            
            <?
            //fill in details about the path
            $path="upload/$random_digit/";
            
            //variable used for the name of each file
            $i=1;
            
            $od = opendir($path);
            while (false !== ($filename = readdir($od)))
            {
            	//you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
            	if($filename != '.' && $filename != '..' && !is_dir($path.$filename))
            	{
            		//we give files a name - here we use increasing numbers for jpg files:
            		if(rename($path.$filename, $path.$i++.'.jpg'))
            		echo 'Renamed file '.$filename.'<br />';
            	}
            }
            
            closedir($od);
            
            ?>
            
            <?
            //fill in details about the path
            $pathT="upload/imgtemp/$random_digit/";
            
            //variable used for the name of each file
            $i=1;
            
            $odT = opendir($pathT);
            while (false !== ($filename = readdir($odT)))
            {
            	//you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
            	if($filename != '.' && $filename != '..' && !is_dir($pathT.$filename))
            	{
            		//we give files a name - here we use increasing numbers for jpg files:
            		if(rename($pathT.$filename, $pathT.$i++.'b.jpg'))
            		echo 'Renamed file '.$filename.'<br />';
            	}
            }
            closedir($odT);
            ?>
            
            <?
            copy($tempFLDR, $path) or die ;
            ?>
            <?
            $username="vecre0_tempU";
            $password="auction";
            $database="vecre0_templates";
            
            
            // OPEN CONNECTION ---> 
            $connection=mysql_connect("localhost","$username", "$password") or die("Unable to connect!");
            
            mysql_select_db("$database") or die("Unable to select database!");
            
            
            //  EXECUTE QUERY ---> 
            $query="INSERT INTO paths (
            
            			folderPath)
            
                    VALUES(		
                        '".$folderPath."')";
            //////-----> 
            $result=mysql_query($query) or die("Error in query:".mysql_error()); 
            //if ($result) 
                //echo mysql_affected_rows()." row inserted into the database effectively."; 
            
            //  CLOSE CONNECTION ---> 
            mysql_close($connection); 
            ?>
            
            
            
            
            
            
            </body>
            
            </html>

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              $path does not point to a file. It points to a folder. It's hard to help me help you because I have no clue what is happening in your code. There doesn't seem to be any logic to it. Looks like you do things more than necessary.

              If you could explain what you want to happen, we may be able to help you rewrite it.

              Comment

              • anfetienne
                Contributor
                • Feb 2009
                • 424

                #8
                the code below brings in the (ID) random_digit from a previous page and the return url. it creates the temp folder using the random_digit as its end.

                it creates the main folder for the random_digit and uploads the images to the main folder.....it then copies them to the temp folder. $folderPath is used only for the sql db.

                Code:
                <?
                
                $random_digit = $_POST['ID'];
                $returnURL = $_POST['returnURL'];
                
                $imgtemp=mkdir("upload/imgtemp/$random_digit", 0777); 
                
                
                ?>
                
                
                
                <?
                while(list($key,$value) = each($_FILES['images']['name']))
                		{
                			if(!empty($value))
                			{
                				$filename = $value;
                					$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
                
                					$add = "upload/$random_digit/$filename";
                					$tempFLDR = "upload/imgtemp/$random_digit/$filename";
                                       //echo $_FILES['images']['type'][$key];
                			     // echo "<br>";
                					copy($_FILES['images']['tmp_name'][$key], $add);
                					chmod("$add",0777);
                					copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
                					chmod("$tempFLDR",0777);
                					
                $folderPath="upload/$random_digit/";
                			}
                		}
                ?>
                the code below opens up the main folder and renames the images within by incrementing them i.e. 1.jpg, 2.jpg, 3.jpg etc

                then in the 2nd part the temp folder is then opens up and all images within are renamed by incrementing them but as these are for thumbnails they are renamed to 1b.jpg, 2b.jpg, 3b.jpg etc

                Code:
                <?
                //fill in details about the path
                $path="upload/$random_digit/";
                
                //variable used for the name of each file
                $i=1;
                
                $od = opendir($path);
                while (false !== ($filename = readdir($od)))
                {
                	//you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
                	if($filename != '.' && $filename != '..' && !is_dir($path.$filename))
                	{
                		//we give files a name - here we use increasing numbers for jpg files:
                		if(rename($path.$filename, $path.$i++.'.jpg'))
                		echo 'Renamed file '.$filename.'<br />';
                	}
                }
                
                closedir($od);
                
                ?>
                
                <?
                //fill in details about the path
                $pathT="upload/imgtemp/$random_digit/";
                
                //variable used for the name of each file
                $i=1;
                
                $odT = opendir($pathT);
                while (false !== ($filename = readdir($odT)))
                {
                	//you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
                	if($filename != '.' && $filename != '..' && !is_dir($pathT.$filename))
                	{
                		//we give files a name - here we use increasing numbers for jpg files:
                		if(rename($pathT.$filename, $pathT.$i++.'b.jpg'))
                		echo 'Renamed file '.$filename.'<br />';
                	}
                }
                closedir($odT);
                ?>
                after this id just want to resize the images within the temp folder and copy them back to the main folder. once copied the temp folder for that ID will be deleted. The only problem i have is resizing them and copying them. I haven't tried to resize them as yet but from what you are saying i need $path ending with $filename instead of just pointing to a directory

                Comment

                • anfetienne
                  Contributor
                  • Feb 2009
                  • 424

                  #9
                  everything in my coding works perfectly fine its just copying the files back to the main folder from the temp folder that is my main issue at this point....i have not tried any resizing codes just yet

                  Comment

                  • numberwhun
                    Recognized Expert Moderator Specialist
                    • May 2007
                    • 3467

                    #10
                    Originally posted by anfetienne
                    the code below brings in the (ID) random_digit from a previous page and the return url. it creates the temp folder using the random_digit as its end.

                    it creates the main folder for the random_digit and uploads the images to the main folder.....it then copies them to the temp folder. $folderPath is used only for the sql db.

                    Code:
                    <?
                    
                    $random_digit = $_POST['ID'];
                    $returnURL = $_POST['returnURL'];
                    
                    $imgtemp=mkdir("upload/imgtemp/$random_digit", 0777); 
                    
                    
                    ?>
                    
                    
                    
                    <?
                    while(list($key,$value) = each($_FILES['images']['name']))
                    		{
                    			if(!empty($value))
                    			{
                    				$filename = $value;
                    					$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
                    
                    					$add = "upload/$random_digit/$filename";
                    					$tempFLDR = "upload/imgtemp/$random_digit/$filename";
                                           //echo $_FILES['images']['type'][$key];
                    			     // echo "<br>";
                    					copy($_FILES['images']['tmp_name'][$key], $add);
                    					chmod("$add",0777);
                    					copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
                    					chmod("$tempFLDR",0777);
                    					
                    $folderPath="upload/$random_digit/";
                    			}
                    		}
                    ?>
                    the code below opens up the main folder and renames the images within by incrementing them i.e. 1.jpg, 2.jpg, 3.jpg etc

                    then in the 2nd part the temp folder is then opens up and all images within are renamed by incrementing them but as these are for thumbnails they are renamed to 1b.jpg, 2b.jpg, 3b.jpg etc

                    Code:
                    <?
                    //fill in details about the path
                    $path="upload/$random_digit/";
                    
                    //variable used for the name of each file
                    $i=1;
                    
                    $od = opendir($path);
                    while (false !== ($filename = readdir($od)))
                    {
                    	//you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
                    	if($filename != '.' && $filename != '..' && !is_dir($path.$filename))
                    	{
                    		//we give files a name - here we use increasing numbers for jpg files:
                    		if(rename($path.$filename, $path.$i++.'.jpg'))
                    		echo 'Renamed file '.$filename.'<br />';
                    	}
                    }
                    
                    closedir($od);
                    
                    ?>
                    
                    <?
                    //fill in details about the path
                    $pathT="upload/imgtemp/$random_digit/";
                    
                    //variable used for the name of each file
                    $i=1;
                    
                    $odT = opendir($pathT);
                    while (false !== ($filename = readdir($odT)))
                    {
                    	//you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
                    	if($filename != '.' && $filename != '..' && !is_dir($pathT.$filename))
                    	{
                    		//we give files a name - here we use increasing numbers for jpg files:
                    		if(rename($pathT.$filename, $pathT.$i++.'b.jpg'))
                    		echo 'Renamed file '.$filename.'<br />';
                    	}
                    }
                    closedir($odT);
                    ?>
                    after this id just want to resize the images within the temp folder and copy them back to the main folder. once copied the temp folder for that ID will be deleted. The only problem i have is resizing them and copying them. I haven't tried to resize them as yet but from what you are saying i need $path ending with $filename instead of just pointing to a directory
                    Here is one discrepancy that I see. On line 15 you have the following code to rename the files:

                    Code:
                    if(rename($path.$filename, $path.$i++.'.jpg'))
                    and as you say, you are naming the files 1b.jpg, 2b.jpg, etc. Yet in this rename step, there is no "b" following the "i++", so your files would be called 1.jpg, 2.jpg, etc.

                    Below all that though, you again do a rename with the following code:

                    Code:
                    if(rename($pathT.$filename, $pathT.$i++.'b.jpg'))
                    Are you needing to rename files twice? I have to agree with markus that you are doing a lot here and possibly too much. It feels like you are going 10 miles when you only need to go several hundred feet.

                    Regards,

                    Jeff

                    Comment

                    • anfetienne
                      Contributor
                      • Feb 2009
                      • 424

                      #11
                      if you look clearly they relate to 2 different directories.... ..the 1st with $path is for the main folder....that renames the files to 1.jpg, 2.jpg, 3.jpg etc

                      the 2nd with $pathT is for the thumbnails that need creating...that renames them to 1b.jpg, 2b.jpg, 3b.jpg etc....these to coding run for two different directories

                      if i could have one code to do everything i need i would but it's impossible ive spent weeks researching how to do it and this is the only way that works.

                      But i am still working on it and i'm very close to getting the code right.

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        I still can't make sense of the code.

                        Why are you creating a temp directory? In case you didn't know, when you upload files ($_FILES array) they are stored temporarily in a predefined directory. You then have access to the files via $_FILES['file']['tmp_name']. Creating a new directory to temporarily store these images is pointless.

                        if i could have one code to do everything i need i would but it's impossible ive spent weeks researching how to do it and this is the only way that works.
                        You can do it all at once.

                        So, here's what you're (trying) to accomplish (as I see it):
                        1. Generating a directory (randomly named),
                        2. Moving the uploaded files to that folder,
                        3. and then creating a thumbnail of the file.

                        Is that correct? I'm sure we could lighten this could up and make it follow a logical path.

                        - Markus.

                        Comment

                        • anfetienne
                          Contributor
                          • Feb 2009
                          • 424

                          #13
                          yes that is exactly what im trying to do

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #14
                            Here is what I have put together:

                            Code:
                            /*
                            	A little programming tip:
                            		1. Stick to just 1 naming convention. You shift between
                            		using underscores and camelCase. Using one will eventually
                            		make it easier to read for you (and others, i.e. me and numberwhun).
                            		2. Document your code! Make comments for just about everything - 
                            		it'll help you (and others) a bunch in the future!
                            */
                            
                            // Turn error reporting on!
                            error_reporting(E_ALL);
                            
                            // Our random digit, used as the directory name - checks for post data.
                            $random_digit 	= ( isset ( $_POST['ID'] ) ) ? $_POST['ID'] : rand();
                            // Return URL - presumably for when the script ends? - checks for post data.
                            $return_url		= ( isset ( $_POST['returnURL'] ) ) ? $_POST['returnURL'] : "?error";
                            // Now let's set the path to the 'uploads/<random_digit>' dir - no trailing slash.
                            $upload_path	= "upload/{$random_digit}";
                            // And the upload path for the thumbnails dir.
                            $upload_path_t 	= $upload_path . "/t/";
                            // Try to create the directorys.
                            if ( mkdir( $upload_path, 0777 ) && mkdir( $upload_path_t, 0777 ) )
                            {
                            	// Directorys made successfully.
                            	// Now traverse the FILES array, adding one file to the
                            	// $upload_path dir, and one to the $upload_path_t (thumbnail) dir.
                            	foreach( $_FILES['files'] as $key => $file )
                            	{
                            		// Check for errors
                            		if ( $file['error'] == 0 )
                            		{
                            			// Make sure it's been uploaded through PHP.
                            			if ( is_uploaded_file ( $file['tmp_name'] ) )
                            			{
                            				// Move files to directorys.
                            				if ( move_uploaded_file( $file['tmp_name'], $upload_path ) && 
                            					 move_uploaded_file( $file['tmp_name'], $upload_path_t ) )
                            				{
                            					echo "Files successfully uploaded. key: {$key}";
                            				}
                            				// File not moved.
                            				else
                            				{
                            					echo "Problem moving files. key: {$key}";
                            				}
                            			}
                            			// Not uploaded file.
                            			else
                            			{
                            				echo "Files not uploaded through PHP. key: {$key}";
                            			}
                            		}
                            		// Error with file.
                            		else
                            		{
                            			echo "File errorno: {$file['error']}. key: {$key}";
                            		}
                            	}
                            }
                            // Directory not made.
                            else
                            {
                            	echo "Problem making dirs.";
                            }
                            You may have to make changes to it (the paths, etc). Instead of putting the thumbnails and originals in the same directory, separate them - makes things a lot easier.

                            The flow of the above is:
                            1. Create the directories (orig, and thumbnail),
                            2. traverse the files using a foreach() loop,
                            3. check the file for errors,
                            4. check the file was uploaded through php,
                            5. and then eventually attempt to move the files.


                            Hope this helps - I threw it together without testing it, so there may be some errors. Let us know.

                            - Markus.

                            Comment

                            • anfetienne
                              Contributor
                              • Feb 2009
                              • 424

                              #15
                              thanks marcus i will do

                              Comment

                              Working...