Link to home
Create AccountLog in
Avatar of gingera
gingera

asked on

PHP Linux: How to copy a file to multiple directories?

PHP Linux


Hello,

How do I copy a file to multiple directories (either through command line or through a PHP script)?

For example...

I want to copy a specific file, index.php in /universal/

to these directories

/storage/A
/storage/B
/storage/C
/storage/D

How do I achieve that?
ASKER CERTIFIED SOLUTION
Avatar of afzz
afzz

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
If you're looking for a single command, there isn't one as far as I know, but you can just use four copy commands.

I hope this is what you're looking for.

Command line:
cp /universal/index.php /storage/A
cp /universal/index.php /storage/B
cp /universal/index.php /storage/C
cp /universal/index.php /storage/D
 
Or PHP:
copy('/universal/index.php', '/storage/A');
copy('/universal/index.php', '/storage/B');
copy('/universal/index.php', '/storage/C');
copy('/universal/index.php', '/storage/D');

Open in new window

Avatar of gingera
gingera

ASKER

Hi afzz, your suggested solution sounds feasible.

The file has to be copied to hundreds of directories. Could I use a MySQL database to feed this line:

$dest=array("/storage/A","/storage/B","/storage/C","/storage/D");

If so, how? (I am a newbie). Thanks.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of gingera

ASKER

Thanks afzz and Xorlev! Your combined suggestion worked OK to copy 1 file.

What if I want to copy multiple files at the same time? Can I use an array for $filelist as well? If so, how do I change afzz's code to accommodate the array?

e.g.
$filelist = array("index1.php","index2.php","index3.php","index4.php","index5.php","index6.php");
Avatar of gingera

ASKER

THANKS!