Solved

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

Posted on 2008-06-15
7
945 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:gingera
  • 3
  • 3
7 Comments
 
LVL 4

Accepted Solution

by:
afzz earned 250 total points
ID: 21787943
Try this
$src="/universal";
$filename="index.php";
$dest=array("/storage/A","/storage/B","/storage/C","/storage/D");
 
foreach($dest as $destdir){
if(file_exists($src."/".$filename)){
if(!file_exists($destdir."/".$filename)){
copy($src."/".$filename,$destdir."/".$filename);
}
else{
echo "File ".$filename." Exists at ".$destdir;
}
}
else{
echo "Source file does not exist";
}
}

Open in new window

0
 
LVL 3

Expert Comment

by:Xorlev
ID: 21787948
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

0
 

Author Comment

by:gingera
ID: 21813076
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.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 3

Assisted Solution

by:Xorlev
Xorlev earned 250 total points
ID: 21815149

<?php
$conn = mysql_connect('host', 'user', 'pass') or die('Failed to connect.');
mysql_select_db('dbname') or die('Failed to connect');
 
$query = mysql_query('SELECT directories FROM dirlist;');
 
while($result = mysql_fetch_assoc($query)) {
     $dest[] = $result['destination'];
}
 
// Other code
?>

Open in new window

0
 
LVL 3

Assisted Solution

by:Xorlev
Xorlev earned 250 total points
ID: 21815158
SELECT directories FROM dirlist;

to

SELECT destination FROM dirlist;

Or whatever you need it to be. $result['columnname'] just needs to match it.
0
 

Author Comment

by:gingera
ID: 21840329
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");
0
 

Author Closing Comment

by:gingera
ID: 31467311
THANKS!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hasing a url 16 28
How would I do a website like this? 5 57
mysqli insert query problems 4 22
Showing random records from database 10 37
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question