Php copy multiple files from one directory to another
The rename function does this Show Image rename
If you want to keep the existing file on the same place you should use copy Image copy
Use Loop for multiple files as below: how to copy multiple files from one folder to another in phpToday i will show you how to copy multiple files from one folder to another or same folder.We can easily create a copy or duplicate file to original in PHP with PHP’s built in function called copy(). It requires two arguments to perform its operation, the source file to copy, and the destination file path. it will return TRUE on Success and FALSE on failure. below i write code for copy multiple files from one folder to another just copy and past and change source file folder path and destination file folder. PHP code for copy multiple files from one folder to anotherIf You have any question and problem in this code feel free, leave comment below! I hope you like this – SHARE WITH YOUR FRIENDSI am currently working on a side project, which needs to create a copy of a WordPress installation. Because I do not know the server environment where it is used by the users, I can neither expect that I can run Linux commands, nor that there is a long PHP So copying needs to be done via PHP and also work when, for example, the maximum execution time for scripts is 30 seconds. 30 seconds are not much for a script that should copy many files. Creating a list of the files and looping it without copying files can already take longer. Because of that, we need to do the copying in a way where we can save the current state when we are near the time limit, plan the next run, and exit. In the WordPress context, that is possible with WordPress cron events. I had issues finding a solution how to exclude already copied files from the next process so that they are not looped again. Symfony Finder component to the rescueRelatively fast I came across the Finder component of Symfony. The component allows finding directories and files by various rules and exclude other directories. So in a few steps, my solution looks like the following:
My current code for that (not yet finished, but it works and should be enough for inspiration. Besides the Finder component I also use the Filesystem component — the code also exists as a Gist).
To start the process, we need to run the following code (the class file needs to be included, too, for example, via the Composer autoloader):
Now the explanation of the most important parts:
The action that is called by the cron event looks like this:
We get the content of the status file, undo serialization of the object and run the And that is it, we have a process that even with a short execution time can copy large folders 🎉 If copying one file needs a long time, that could of course lead to a timeout error, because the check is done after copying and not in between. But it should be a good starting point. |