how can i copy folder1/*.* to folder2/ ? supposing i don't know the exact file names in folder1 .. i just want my script to copy over all the files that it can find in folder1 to folder2 Thanks
$source = './path/to/folder1/*.*'; function copy_files($file, $destination = './path/to/folder2/') { copy($file, $destination . basename($file)); } array_map('copy_files', glob($source)); PHP: Untested but should work.
I'm having a bit trouble with syntax here .. could you please show the correct syntax to use ? here's my final code if (!is_dir("$login")) { mkdir ("$login"); } $source = "templates/$template_ID/images/*.*"; function copy_files($file, $destination = '$login/images/') { copy($file, $destination . basename($file)); } array_map('copy_files', glob($source)); PHP: as you see, i use $login variable (which is already defined earlier) in the name of destination folder. Also i use $template_ID in source but it works fine. and here is what i get as a result: so, it doesn't change the "$login" to it's value , but instead uses "$login" as a string itself
Do it like this, variables are not interpolated inside single quotes: if (!is_dir("$login")) { mkdir ("$login"); } $source = "templates/$template_ID/images/*.*"; function copy_files($file, $destination = "$login/images/") { copy($file, $destination . basename($file)); } array_map('copy_files', glob($source)); PHP: PHP offers several types of quoting. Double quotes offer the most features, including variable interpolation and escape sequences like "\n" for a newline. With single quotes you get exactly what you put within the quotes, no special features.
I think i got the reason why this happens .. the $login variable is defined AFTER the function is defined by the compiler, so at the time when the function being created - the variable $login does not exist yet .. ok, then i guess i need another function instead of this, which will accept $login as an argument, and when the time comes to call the function, the $login will already be defined so, can anyone please help me to create such a function ? Thanks
Try this: if (!is_dir($login)) { mkdir ($login); } $source = "templates/$template_ID/images/*.*"; $destination = "$login/images/"; function copy_files($file) { global $destination; copy($file, $destination . basename($file)); } array_map('copy_files', glob($source)); PHP:
Thanks i actually have figured out to go another way like this $cmd = "cp -rf /path1/images path2"; system($cmd); it did all the job ))
That's good but it won't work on Windows. Using glob is cross-platform, hence the reason I thought it was a good find.