Best way to get a file from remote server and copy to local server using php [closed]
Asked Answered
V

3

7

Let's say there is a file on a remote server that can be downloaded without any restrictions, ie. you can put the direct link to the file in your browser and it downloads the file, for example http://www.remotesite.com/video.avi will prompt your browser to download that file. Using php, what is the best way to grab that file and upload it to my local server without the file being downloaded to my PC at all, which is what happens with phpBB if you put a url in the file upload form? An example of the code needed would also be appreciated. Thanks

Verbena answered 13/5, 2013 at 8:48 Comment(1)
php.net/manual/en/function.file-get-contents.phpKaterine
V
25

Just use copy

$source = "http://www.remotesite.com/video.avi";
$dest = "video.avi";
copy($source, $dest);
Vidrine answered 13/5, 2013 at 9:2 Comment(1)
sounds cool than file_get and file_put :)Katerine
K
4
$remote_file_contents = file_get_contents('http://remote_url/file/with.extension');
//Get the contents

$local_file_path = 'your/local/path/to/the/file/with.extension';

file_put_contents($local_file_path, $remote_file_contents);
//save the contents of the remote file
Katerine answered 13/5, 2013 at 8:56 Comment(1)
php.net/manual/en/function.file-put-contents.phpKaterine
O
2

You can read and write the file without browser download

<?php 

$file = 'http://www.remotesite.com/video.avi';

// read the file from remote location
$current = file_get_contents($file);

// create new file name
$name = "path/to/folder/newname.avi";

// Write the contents back to the file
file_put_contents($file, $current);
Obstreperous answered 13/5, 2013 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.