Use copy() in a foreach loop to copy multiple files php
<p>I'm trying to copy multiple files from one domain on a web server to another using copy() and looping through a list of files, but it's only copying the last file on the list.</p>

<p>Here is the contents of files-list.txt:</p>

<pre><code>/templates/template.php
/admin/admin.css
/admin/codeSnippets.php
/admin/editPage.php
/admin/index.php
/admin/functions.php
/admin/style.php
/admin/editPost.php
/admin/createPage.php
/admin/createPost.php
/admin/configuration.php
</code></pre>

<p>This script runs on the website that I'm trying to copy the files to. Here's the script:</p>

<pre><code>$filesList = file_get_contents("http://copyfromhere.com/copythesefiles/files-list.txt");
$filesArray = explode("\n", $filesList);

foreach($filesArray as $file) {
    $filename = trim('http://copyfromhere.com/copythesefiles' . $file);

    $dest = "destFolder" . $file;

    if(!@copy($filename, $dest))
    {
        $errors= error_get_last();
        echo "COPY ERROR: ".$errors['type'];
        echo "&lt;br /&gt;\n".$errors['message'];
    } else {
        echo "$filename copied to $dest from remote!&lt;br/&gt;";
    }
}
</code></pre>

<p>I get the affirmative message for each and every file individually just as I should, but when I check the directory, only the last file from files-list.txt is there. I've tried changing the order, so I know the problem lies with the script, not any individual file.</p>

<p>The output from the echo statements looks something like this:</p>

<pre><code>http://copyfromhere.com/copythesefiles/admin/admin.css copied to updates/admin/editPage.php from remote!
http://copyfromhere.com/copythesefiles/admin/admin.css copied to updates/admin/editPost.php from remote!
http://copyfromhere.com/copythesefiles/admin/admin.css copied to updates/admin/index.php from remote!
</code></pre>

<p>Etc</p>