【问题标题】:download apk file from given url to server using php使用php将apk文件从给定的url下载到服务器
【发布时间】:2016-05-09 16:53:37
【问题描述】:

我想从我使用过的远程服务器上的 url 下载 apk 文件

set_time_limit(0);
$file1 = fopen($destinationDir.$fileName, "w");
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_URL            => $url,
CURLOPT_BINARYTRANSFER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FILE           => $file1,
CURLOPT_TIMEOUT        => 1000,
CURLOPT_USERAGENT      => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT\
5.0)' ]);
$response = curl_exec($curl);
if($response === false) {
throw new \Exception('Curl error: ' . curl_error($curl));
}

但有时当文件被下载时,它是 0kb 文件,有时文件大小为 40 kb,而原始文件大小为 2 MB。当我通过浏览器访问给定的链接时,它会被下载但不使用 php。请向我建议任何替代或更正。 P.S 我是初学者,在我的大学项目中需要这个。提前谢谢你

【问题讨论】:

  • 尝试增加你的超时时间。 1s 可能太短了。
  • @cherryDT 它不起作用我增加了超时时间
  • @第 4242 位医生非常感谢您的指正

标签: php linux curl download


【解决方案1】:

使用此代码从 url 下载文件。

您只需将您的网址和路径传递到您要下载的位置。

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_VERBOSE, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_AUTOREFERER, false);
  curl_setopt($ch, CURLOPT_REFERER, "https://example.com");
  curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true );
  $result = curl_exec($ch);
  curl_close($ch);

 // the following lines write the contents to a file in the same directory 
 (provided permissions etc)
 $output_filename = "folderame/filename";
 $fp = fopen($output_filename, 'w');
 fwrite($fp, $result);
 fclose($fp);
}

这对我来说很好。

【讨论】:

    猜你喜欢
    • 2012-10-14
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多