How to download a file to server PHP?

跟風遠走 提交于 2019-12-20 04:18:12

问题


Is it possible with PHP script to have the script download a file on a remote server to my web server?

I have my own webserver and domain. I want to put a php script on that domain, that will download a file from a remote server onto my server's filesystem. Is this possible?

-Jim


回答1:


Sure, you'll need write permissions somewhere on the file system (where you want to save this file), file_get_contents can take a URL as its argument, you just need to write the resulting string to a new file

I'd personally do this by calling out to the shell and invoking wget or similar if i were on a linux box.




回答2:


Yes, with file_get_contents(), fgets(), or readfile(), depending on your server configuration.

Create a file locally and dump the data from any of those functions, into your new local file.




回答3:


I thinkk this is what you are refering to:

<?php
$file = 'http://example.com/example.txt';
$newfile = 'example.txt.bak';

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

Any good?

Best of luck!




回答4:


You could also use CURL if you need more control over the request type/headers



来源:https://stackoverflow.com/questions/3938504/how-to-download-a-file-to-server-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!