Reputation: 103527
How to copy files from one folder to another folder in php and delete them in source folder ?
I have two folder $incoming_file_path = /home/xmlcontainer
and $processing_file_path = home/process_file
. I am looping through all files in $incoming_file_path = home/xmlcontainer
and than copying it into $processing_file_path = home/process_file
.
After executing below code I am not able to copy the content of files but just the name of the files also am not able to delete files which I have copies to destination folder from source folder using unlink
, I am surely using unlink in wrong way and would certainly appreciate any guidance on it.
Code
foreach( glob($incoming_file_path.'/*')as $key => $value ) {
copy($incoming_file_path.$value,$processing_file_path.$value);
unlink($incoming_file_path.$value);
}
Upvotes: 2
Views: 1324
Reputation: 443
First of all, just use rename if you intend to move the files.
Your loop should look like this:
foreach (glob($incoming_file_path . '/*') as $value) {
rename($value, $processing_file_path . '/' . basename($value))
}
Inability to read file contents and/or delete a file is often a simple permissions problem. Check if the owner of your PHP process can read and write to both $incoming_file_path
and $processing_file_path
. You may want to run chmod -R u+rwx
(or chmod -R o+rwx
) on both $incoming_file_path
and $processing_file_path
.
Upvotes: 2