I’m struggling with this code, the last items repeats itself as many times as there are files in the source folder. Basically the same file is being moved into multiple files with different names while all the other files are not being processed.
I figured out it has something to do with the renaming of the files cause commenting out those two lines makes it work,
<?php
$source = "photos/";
$destination = "photos2/";
$files = glob(''.$source.'*.{jpg,png,gif}', GLOB_BRACE);
print_r($files);
foreach($files as $file) {
$filename = basename($file); //separating filename from full path.
$temp = explode(".", $filename); //splitting name from extension.
$newfilename = round(microtime(true)) . '.' . end($temp); //making new filename + adding extension.
copy($source.$filename, $destination.$filename); //Copy file to the new directory.
echo "<p>! ".$filename." has been copied successfully</p>";
}
?>
When doing print_r($files) I get this. I will update the code above. The copying works, but I would like to rename the before being copied. Not sure how to do that
And the old and new filenames are correct, and different for each iteration? Does microtime() change often enough to use for this? If they are, I can’t see how it could be copying the last file into multiple destination files.
Is there a better way to rename it ? There are four files in source folder and I’m getting the same pic into the destination folder with different names.
<?php
$source = "photos/";
$destination = "photos2/";
$files = glob(''.$source.'*.{jpg,png,gif}', GLOB_BRACE);
//print_r($files);
foreach($files as $file) {
$filename = basename($file); //separating filename from full path.
$temp = explode(".", $filename); //splitting name from extension.
$newfilename = round(microtime(true)) . '.' . end($temp); //making new filename + adding extension.
copy($source.$filename, $destination.$newfilename); //Copy file to the new directory.
echo "<p>! ".$filename." has been renamed(".$newfilename.") and copied successfully</p>";
echo "<img src='photos2/".$newfilename."' />";
}
?>
OK, I assume the filenames are correct, and different for each iteration of the loop.
I can’t say whether there is a better way to rename - what you have seems to be reasonable. My comment about rename as opposed to copy is that I consider a rename operation to result in the original file having a new name (and perhaps location), and the file with the old name no longer existing.
Also note that you’re not checking the return from copy(), so your message " and copied successfully" is making a bit of an assumption.
Lol yeah I know. I was having an if there. I will try using the rename again. I did and it gave me the same result, and I had to add new files to the source folder all the time so I thought I test it with copy lol
And does it say that it’s been renamed, or that it failed to copy? Can you show the before and after filenames in case that helps anyone figure it out?
in each iteration it prints the old filename and shows the new filename. Problem is the new filename is the same on all four.
! 14720603_755647627921045_8690706843580257074_n.jpg has been renamed(1477855395.jpg) and copied successfully
! 14731257_10157721423060038_6695484612693187684_n.jpg has been renamed(1477855395.jpg) and copied successfully
When checking the destination folder now there are just one file there. So I guess each loop overwrites the previous file.
And is that just because the microtime() value doesn’t change often enough to make it suitable for use as the destination? How about just adding a counter variable, increment it each time you go through the foreach loop, and add it on the end of the microtime to differentiate each one?