繁体   English   中英

使用php从给定的URL下载apk文件到服务器

[英]download apk file from given url to server using php

我想从我使用过的远程服务器上的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,有时文件大小为40kb,而原始文件大小为2MB。 当我通过浏览器转到给定的链接时,它将下载但不使用php。 请给我建议其中任何替代或更正。 PS我是一个初学者,在我的大学项目中需要这样做。 先感谢您

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

您只需将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);
}

对我来说很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM