0

我想从异地服务器下载文件。如果出现这种情况,我确实同意这样做:) 我拥有的是一个 cURL 脚本,它可以获取标题和文件名,但无论出于何种原因,我都无法将文件 dl 到我的服务器。

$downloadvUrl="$videos";
$file = "downloads";
$fileName=$downloadvUrl;
$header = array(
"Accept-Encoding: gzip,deflate",
"Accept-Charset: utf-8;q=0.7,*;q=0.7",
"Connection: close"
);
$file2 = dirname(__file__).'/'.videos.'.m4v';
$curlObj=curl_init();
curl_setopt($curlObj, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlObj, CURLOPT_HEADER, false);
curl_setopt($curlObj, CURLOPT_USERAGENT, $useragent);
curl_setopt($curlObj, CURLOPT_CONNECTTIMEOUT, 999);
curl_setopt($curlObj, CURLOPT_TIMEOUT, 9999);
curl_setopt($curlObj, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curlObj, CURLOPT_URL, $downloadvUrl);
$response = curl_exec($curlObj);
$return = false;
if(!curl_errno($curlObj)) {
file_put_contents($file2, $response);
}
4

2 回答 2

0

实际上,我通过使它更简单一些来使其工作。

<?php
$i = 0;
while(file_exists($target_path).$i) {
$i++;
}
$ch = curl_init("$matches[1]");
$fp = fopen("1.m4v", "w");
if (file_exists($fp)) {
    echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?> 

我怎样才能得到它,以便文件以不同的名称上传?

于 2012-06-03T23:23:42.333 回答
0

你可以加:

$fh = fopen('/tmp/foo', 'w'); 
curl_setopt($ch, CURLOPT_FILE, $fh);

将 /tmp/foo 更改为您希望将其保存到的文件。

于 2012-06-04T01:21:07.487 回答