我试图下载存在于不同服务器上的文件并将其移至我的新服务器。
我已经尝试了
$file = file_get_contents("https://exampleurl.com/file/download.txt");
file_put_contents("C:/directory/to/report/data.csv", $file);
以及
$remote_file_url = "https://exampleurl.com/file/download.txt";
$local_file = C:/directory/to/report/data.csv;
$copy = copy( $remote_file_url, $local_file );
但文件永远不会完成,它会切断文件的末尾。 当我直接从网址下载文件时,它每次都完成。
我正在寻找确保文件完全下载的方法
答案 0 :(得分:0)
通常使用卷曲请求file_get_contents超时会导致此类错误,并且只会加载部分内容。试试这个:
$ch=curl_init("https://exampleurl.com/file/download.txt"); //create a c url session
$timeout=300; //set time limit here depends upon the operations being done on the remote server currently its 5mins
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //set timeout for request
$content = curl_exec($ch);//execute request
if($content)
{
$localfile = fopen("path to local file", "w"); //create or open a existing file
if(fwrite($localfile , $content)) //write data received
echo "success";
else
echo "unable to write file";
fclose($localfile ); //close the file
}
else //error occured while executing the request on the remote server
echo "Request Failed";