Search code examples
phptextservertransfer

Transfer files from server to server from urls in a text file


Hi I want to transfer files from one server to another from urls that are kept in a text file. I'm using a shared linux hosting. Currently this code is working for transfering files from a single url. But I want to load many urls from a text file.

/* Source File URL */
$remote_file_url = 'http://origin-server-url/files.mp4';

/* New file name and path for this file */
$local_file = 'files.mp4';

/* Copy the file from source url to server */
$copy = copy( $remote_file_url, $local_file );

/* Add notice for success/failure */
if( !$copy ) {
echo "Doh! failed to copy $file...\n";
}
else{
echo "WOOT! success to copy $file...\n";
}

And the file name and extension should be masked from each url. can somebody help.


Solution

  • This is a very simple example how you could do this.

        $remoteFileUrls = [
            'File 1' => 'http://origin-server-url/files.mp4',
            'File 2' => 'http://origin-server-url/files.mp4',
            'File 3' => 'http://origin-server-url/files.mp4',
            'File 4' => 'http://origin-server-url/files.mp4'
        ];
    
        $localFileUrls = [
            'File 1' => 'files.mp4',
            'File 2' => 'files.mp4',
            'File 3' => 'files.mp4',
            'File 4' => 'files.mp4'
        ];
    
        foreach($remoteFileUrls as $key => $remoteUrl){
            /* Copy the file from source url to server */
            $copy = copy( $remoteUrl, $localFileUrls[$key] );
    
            /* Add notice for success/failure */
            if( $copy ) {
                echo "Success\n";
            }
            else{
                echo "Failed\n";
            }
        }