Copy file from remote server url to our own server

Published


Here is how we can Save a file from url using PHP.

<?php
$file = 'http://somewebsite.com/somefile.avi';
$newfile = 'video.avi';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>


Code Explained

<?php
$file = 'http://somewebsite.com/somefile.avi';  //Store the path to remote file to variable $file.
$newfile = 'video.avi'; //Declaring  where and in what name the remote file to be saved on our server.

if (!copy($file, $newfile)) { //Checking if there any errors in copying if do
    echo "failed to copy $file...\n"; //Displaying a message in page as "Copy failed".
}
//if there is no errors, the file will be copied when the page loading ends.
?>


PHP Function used

copy()

Comments

Post a Comment