user2638001
user2638001

Reputation: 21

foreach loop copy files directories

I'd like to copy files from a remote server with a similar structure to files in my server with the same structure.

include "../arrays.php";
foreach ($citycode as $city) {
$source = "http://www.remoteserver.com/data/{$city}/";
$dest = "/alltxts/{$city}/";

// EVERYTHING FROM HERE ONWARDS RUNS PERFECTLY, THE PROBLEM IS PROBABLY ABOVE.
function copyr($source, $dest) 
{ 
// Simple copy for a file 
if (is_file($source)) {
    chmod($dest, 777);
    return copy($source, $dest); 
} 

// Make destination directory 
if (!is_dir($dest)) { 
    mkdir($dest); 
}

chmod($dest, 777);

// Loop through the folder 
$dir = dir($source); 
while (false !== $entry = $dir->read()) { 
    // Skip pointers 
    if ($entry == '.' || $entry == '..') { 
        continue; 
    } 

    // Deep copy directories 
    if ($dest !== "$source/$entry") { 
        copyr("$source/$entry", "$dest/$entry"); 
     } 
    } 

// Clean up 
$dir->close(); 
return true; 
}
}

The code to copy the files works just fine when I use a specific $citycode as $city. However, when I use the array to catch all city names with one line, it doesn't work. Any ideas? I'd appreciate any help, thanks!

Upvotes: 2

Views: 394

Answers (1)

hek2mgl
hek2mgl

Reputation: 158100

You are searching for:

$source = "http://www.remoteserver.com/data/{$city}/";
$dest = "/alltxts/{$city}/";

file_put_contents($dest, file_get_contents($source));

Make sure that you have proper permissions to save files in /alltxts/'. Also the leading / looks weird to me. Do you mean something like alltexts/ (relative path) instead?

Upvotes: 1

Related Questions