473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

copy files

anfetienne
424 256MB
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:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. copy($tempFLDR, $path) or die ;
  3. ?>
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
Mar 4 '09 #1
19 2509
numberwhun
3,509 Expert Mod 2GB
@anfetienne
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
Mar 5 '09 #2
anfetienne
424 256MB
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
Mar 5 '09 #3
numberwhun
3,509 Expert Mod 2GB
@anfetienne
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
Mar 5 '09 #4
Markus
6,050 Expert 4TB
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.
Mar 5 '09 #5
anfetienne
424 256MB
here is the full coding to the file

Expand|Select|Wrap|Line Numbers
  1. <!doctype html public "-//w3c//dtd html 3.2//en">
  2. <html>
  3. <head>
  4. <title>Multiple image upload script from plus2net.com</title>
  5. </head>
  6. <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
  7. <?
  8.  
  9. $random_digit = $_POST['ID'];
  10. $returnURL = $_POST['returnURL'];
  11.  
  12. $imgtemp=mkdir("upload/imgtemp/$random_digit", 0777); 
  13.  
  14.  
  15. ?>
  16.  
  17.  
  18.  
  19. <?
  20. while(list($key,$value) = each($_FILES['images']['name']))
  21.         {
  22.             if(!empty($value))
  23.             {
  24.                 $filename = $value;
  25.                     $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
  26.  
  27.                     $add = "upload/$random_digit/$filename";
  28.                     $tempFLDR = "upload/imgtemp/$random_digit/$filename";
  29.                        //echo $_FILES['images']['type'][$key];
  30.                  // echo "<br>";
  31.                     copy($_FILES['images']['tmp_name'][$key], $add);
  32.                     chmod("$add",0777);
  33.                     copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
  34.                     chmod("$tempFLDR",0777);
  35.  
  36. $folderPath="upload/$random_digit/";
  37.             }
  38.         }
  39. ?>
  40.  
  41. <?
  42. //fill in details about the path
  43. $path="upload/$random_digit/";
  44.  
  45. //variable used for the name of each file
  46. $i=1;
  47.  
  48. $od = opendir($path);
  49. while (false !== ($filename = readdir($od)))
  50. {
  51.     //you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
  52.     if($filename != '.' && $filename != '..' && !is_dir($path.$filename))
  53.     {
  54.         //we give files a name - here we use increasing numbers for jpg files:
  55.         if(rename($path.$filename, $path.$i++.'.jpg'))
  56.         echo 'Renamed file '.$filename.'<br />';
  57.     }
  58. }
  59.  
  60. closedir($od);
  61.  
  62. ?>
  63.  
  64. <?
  65. //fill in details about the path
  66. $pathT="upload/imgtemp/$random_digit/";
  67.  
  68. //variable used for the name of each file
  69. $i=1;
  70.  
  71. $odT = opendir($pathT);
  72. while (false !== ($filename = readdir($odT)))
  73. {
  74.     //you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
  75.     if($filename != '.' && $filename != '..' && !is_dir($pathT.$filename))
  76.     {
  77.         //we give files a name - here we use increasing numbers for jpg files:
  78.         if(rename($pathT.$filename, $pathT.$i++.'b.jpg'))
  79.         echo 'Renamed file '.$filename.'<br />';
  80.     }
  81. }
  82. closedir($odT);
  83. ?>
  84.  
  85. <?
  86. copy($tempFLDR, $path) or die ;
  87. ?>
  88. <?
  89. $username="vecre0_tempU";
  90. $password="auction";
  91. $database="vecre0_templates";
  92.  
  93.  
  94. // OPEN CONNECTION ---> 
  95. $connection=mysql_connect("localhost","$username", "$password") or die("Unable to connect!");
  96.  
  97. mysql_select_db("$database") or die("Unable to select database!");
  98.  
  99.  
  100. //  EXECUTE QUERY ---> 
  101. $query="INSERT INTO paths (
  102.  
  103.             folderPath)
  104.  
  105.         VALUES(        
  106.             '".$folderPath."')";
  107. //////-----> 
  108. $result=mysql_query($query) or die("Error in query:".mysql_error()); 
  109. //if ($result) 
  110.     //echo mysql_affected_rows()." row inserted into the database effectively."; 
  111.  
  112. //  CLOSE CONNECTION ---> 
  113. mysql_close($connection); 
  114. ?>
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. </body>
  122.  
  123. </html>
  124.  
Mar 5 '09 #6
Markus
6,050 Expert 4TB
$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.
Mar 5 '09 #7
anfetienne
424 256MB
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.

Expand|Select|Wrap|Line Numbers
  1. <?
  2.  
  3. $random_digit = $_POST['ID'];
  4. $returnURL = $_POST['returnURL'];
  5.  
  6. $imgtemp=mkdir("upload/imgtemp/$random_digit", 0777); 
  7.  
  8.  
  9. ?>
  10.  
  11.  
  12.  
  13. <?
  14. while(list($key,$value) = each($_FILES['images']['name']))
  15.         {
  16.             if(!empty($value))
  17.             {
  18.                 $filename = $value;
  19.                     $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
  20.  
  21.                     $add = "upload/$random_digit/$filename";
  22.                     $tempFLDR = "upload/imgtemp/$random_digit/$filename";
  23.                        //echo $_FILES['images']['type'][$key];
  24.                  // echo "<br>";
  25.                     copy($_FILES['images']['tmp_name'][$key], $add);
  26.                     chmod("$add",0777);
  27.                     copy($_FILES['images']['tmp_name'][$key], $tempFLDR);
  28.                     chmod("$tempFLDR",0777);
  29.  
  30. $folderPath="upload/$random_digit/";
  31.             }
  32.         }
  33. ?>
  34.  
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

Expand|Select|Wrap|Line Numbers
  1. <?
  2. //fill in details about the path
  3. $path="upload/$random_digit/";
  4.  
  5. //variable used for the name of each file
  6. $i=1;
  7.  
  8. $od = opendir($path);
  9. while (false !== ($filename = readdir($od)))
  10. {
  11.     //you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
  12.     if($filename != '.' && $filename != '..' && !is_dir($path.$filename))
  13.     {
  14.         //we give files a name - here we use increasing numbers for jpg files:
  15.         if(rename($path.$filename, $path.$i++.'.jpg'))
  16.         echo 'Renamed file '.$filename.'<br />';
  17.     }
  18. }
  19.  
  20. closedir($od);
  21.  
  22. ?>
  23.  
  24. <?
  25. //fill in details about the path
  26. $pathT="upload/imgtemp/$random_digit/";
  27.  
  28. //variable used for the name of each file
  29. $i=1;
  30.  
  31. $odT = opendir($pathT);
  32. while (false !== ($filename = readdir($odT)))
  33. {
  34.     //you can add any type of filenames you wish to skip (for instance Thumbs.db on windows)
  35.     if($filename != '.' && $filename != '..' && !is_dir($pathT.$filename))
  36.     {
  37.         //we give files a name - here we use increasing numbers for jpg files:
  38.         if(rename($pathT.$filename, $pathT.$i++.'b.jpg'))
  39.         echo 'Renamed file '.$filename.'<br />';
  40.     }
  41. }
  42. closedir($odT);
  43. ?>
  44.  
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
Mar 5 '09 #8
anfetienne
424 256MB
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
Mar 5 '09 #9
numberwhun
3,509 Expert Mod 2GB
@anfetienne
Here is one discrepancy that I see. On line 15 you have the following code to rename the files:

Expand|Select|Wrap|Line Numbers
  1. if(rename($path.$filename, $path.$i++.'.jpg'))
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. if(rename($pathT.$filename, $pathT.$i++.'b.jpg'))
  2.  
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
Mar 5 '09 #10
anfetienne
424 256MB
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.
Mar 5 '09 #11
Markus
6,050 Expert 4TB
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.
Mar 5 '09 #12
anfetienne
424 256MB
yes that is exactly what im trying to do
Mar 5 '09 #13
Markus
6,050 Expert 4TB
Here is what I have put together:

Expand|Select|Wrap|Line Numbers
  1. /*
  2.     A little programming tip:
  3.         1. Stick to just 1 naming convention. You shift between
  4.         using underscores and camelCase. Using one will eventually
  5.         make it easier to read for you (and others, i.e. me and numberwhun).
  6.         2. Document your code! Make comments for just about everything - 
  7.         it'll help you (and others) a bunch in the future!
  8. */
  9.  
  10. // Turn error reporting on!
  11. error_reporting(E_ALL);
  12.  
  13. // Our random digit, used as the directory name - checks for post data.
  14. $random_digit     = ( isset ( $_POST['ID'] ) ) ? $_POST['ID'] : rand();
  15. // Return URL - presumably for when the script ends? - checks for post data.
  16. $return_url        = ( isset ( $_POST['returnURL'] ) ) ? $_POST['returnURL'] : "?error";
  17. // Now let's set the path to the 'uploads/<random_digit>' dir - no trailing slash.
  18. $upload_path    = "upload/{$random_digit}";
  19. // And the upload path for the thumbnails dir.
  20. $upload_path_t     = $upload_path . "/t/";
  21. // Try to create the directorys.
  22. if ( mkdir( $upload_path, 0777 ) && mkdir( $upload_path_t, 0777 ) )
  23. {
  24.     // Directorys made successfully.
  25.     // Now traverse the FILES array, adding one file to the
  26.     // $upload_path dir, and one to the $upload_path_t (thumbnail) dir.
  27.     foreach( $_FILES['files'] as $key => $file )
  28.     {
  29.         // Check for errors
  30.         if ( $file['error'] == 0 )
  31.         {
  32.             // Make sure it's been uploaded through PHP.
  33.             if ( is_uploaded_file ( $file['tmp_name'] ) )
  34.             {
  35.                 // Move files to directorys.
  36.                 if ( move_uploaded_file( $file['tmp_name'], $upload_path ) && 
  37.                      move_uploaded_file( $file['tmp_name'], $upload_path_t ) )
  38.                 {
  39.                     echo "Files successfully uploaded. key: {$key}";
  40.                 }
  41.                 // File not moved.
  42.                 else
  43.                 {
  44.                     echo "Problem moving files. key: {$key}";
  45.                 }
  46.             }
  47.             // Not uploaded file.
  48.             else
  49.             {
  50.                 echo "Files not uploaded through PHP. key: {$key}";
  51.             }
  52.         }
  53.         // Error with file.
  54.         else
  55.         {
  56.             echo "File errorno: {$file['error']}. key: {$key}";
  57.         }
  58.     }
  59. }
  60. // Directory not made.
  61. else
  62. {
  63.     echo "Problem making dirs.";
  64. }
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.
Mar 5 '09 #14
anfetienne
424 256MB
thanks marcus i will do
Mar 5 '09 #15
Markus
6,050 Expert 4TB
@anfetienne
Markus with a K. :)
Mar 5 '09 #16
anfetienne
424 256MB
can you tell me a way i can copy the thumbnails from the /t/ folder back to the main folder.....images are renamed so there is no need to keep them seperate.
Mar 6 '09 #17
Markus
6,050 Expert 4TB
@anfetienne
The best practice is to keep them separate. Having them all in the same place is messy and unorganized.
Mar 6 '09 #18
anfetienne
424 256MB
Hi Markus,

I have changed a lil piece of my coding and i have got the code to make two temporary folders, upload the images, rename them and move them to the main folder.

These images are used for a flash gallery that i bought. it is already coded to read the images as:

1.jpg (the main image)
1b.jpg (the thumbnail) etc etc

so saving me time in changing that coding as well my aim was to make php adapt to what i bought and i've achieved that with your help thanks a lot
Mar 7 '09 #19
Markus
6,050 Expert 4TB
@anfetienne
I still don't understand why you're using temporary folders, when you really don't have to. But, nevermind.

@anfetienne
Ok, that makes sense.

Glad we could be of some help,

Mark.
Mar 7 '09 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Mike | last post by:
I want to be able to copy a directory of files (all .HTM files) from a network drive to a local drive on the machine c:\HTMFiles , How can i do that? I tried File.Copy(source, dest) but i need...
6
by: Wayne Wengert | last post by:
I am using VSNET 2003 to build an ASP.NET/VB set of pages. There are currently about a dozen aspx pages. When I make even a minor change to one page I currently rebuild the solution, copy the...
3
by: Johnny | last post by:
Hi, I have created an ASP.NET application (let's call it FooBar) with VS.NET on my local machine, residing in http://localhost/FooBar. Deploying it to another folder on my machine works well...
2
by: Steve Franks | last post by:
The Copy Web tool provided with VS.NET 2005 is very convenient. However every once in a while it seems to think the files on the remote server have changed, which they have not. Then when I use...
5
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in...
8
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm...
1
by: dkmarni | last post by:
Hi, I am trying to do this perl script, but not able to complete it successfully. Here is the description what the script has to do.. Accept two and only two command line arguments. Again,...
13
by: jim | last post by:
Is there a way (using VB.Net or C#) to copy open or locked files? Thanks! jim
1
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to...
1
by: =?Utf-8?B?UmFkZW5rb19aZWM=?= | last post by:
I am using standard File.Copy(source,dest,true) method in C# and I have problem with copying large number of files. Here is my code: foreach (FileInfo file in files) {...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.