[英]Download file from remote file server to local machine using curl php
I am trying to create a link to download a file from remote file server to local machine. 我正在尝试创建一个链接,以将文件从远程文件服务器下载到本地计算机。 Right now i am Curl code that i found in stackoverflow.
现在我是在stackoverflow中找到的Curl代码。 The code works with file path with localhost C drive.
该代码适用于localhost C驱动器的文件路径。 But fails with url to remote file.
但是失败,并带有指向远程文件的URL。 What is wrong with the URL?
URL有什么问题? The commented URL works.
注释的URL有效。 Code:
码:
$url = 'file://v1/cos/EA QA/Test Results/SS/4.6.1/CD06/SS 4.6.1 TestResults.xlsm';
// $url = 'file:///C:/xampp/htdocs/qrd/test/SS 4.6.1 TestResults.xlsm';
$path = 'C:/test/SS 4.6.1 TestResults.xlsm';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
if ($data == FALSE){
echo "Failed in 1.";
} else {
$file = fopen($path, "wb");
fputs($file, $data);
echo "hi1";
}
fclose($file);
It doesn't work because 它不起作用,因为
file://v1/cos/EA QA/
is not a valid path, looks like a network path. 不是有效路径,看起来像网络路径。 Try uploading the file to a webserver and test it or provide a proper
ip address
of the network. 尝试将文件上传到Web服务器并对其进行测试,或者提供网络的正确
ip address
。
Your remote server should have an ip address. 您的远程服务器应该有一个IP地址。 Try that like
http://192.159.23.2XX/TestResults.xlsm
as your URL 尝试像
http://192.159.23.2XX/TestResults.xlsm
这样的URL
ALternatively you can use file_get_contents 或者,您可以使用file_get_contents
$data = file_get_contents('C:/test/SS 4.6.1 TestResults.xlsm');
Or if you really wanna use CURL you got to have your file in a valid http protocol. 或者,如果您真的想使用CURL,则必须使用有效的http协议存储文件。
e.g $url = 'http://192.159.23.2XX//TestResults.xlsm';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.