简体   繁体   English

如何通过file_get_content或curl正确获取文件

[英]How to get correctly a file via file_get_content or curl

I tried to download a file via Icecat. 我试图通过Icecat下载文件。 The file need an access to be downloaded. 该文件需要访问权限才能下载。

My problem is : 我的问题是:

  • the file_get_content does'nt download the file. file_get_content不会下载文件。 My directory is on 777 and the path is correct. 我的目录在777上,并且路径正确。
  • if I insert the document inside the directory, the file is not unzipped. 如果我将文档插入目录中,则文件未解压缩。

     public function getIceCatFile() { set_time_limit (0); $url = 'https://data.Icecat.biz/export/freexml/EN/daily.index.xml.gz'; $context = stream_context_create(array('http' => array( 'header' => "Authorization: Basic " . base64_encode($this->username . ":" . $this->password) ))); if ($this->checkDirectoryIceCat() === true) { // does'nt download the file inside the server directory file_get_contents($url, true, $context); $file = $this->IceCatDirectory . 'daily.index.xml.gz'; if (is_file($file)) { $zip = new \\ZipArchive; // error failed same if I include the file inside the directory $icecat_file = $zip->open($this->IceCatDirectory . 'files.index.xml.gz'); if ($icecat_file === true) { $zip->extractTo($icecat_file); $zip->close(); echo 'file downloaded and unzipped'; } else { echo 'failed'; } } else { echo 'error no file found in ' . $file; } } } 

Make a try like this to download file that has username and password authentication using PHP Curl , 尝试这样下载使用PHP Curl 用户名密码验证的文件,

$localfile = fopen($file_store_locally, 'wb'); // open with write enable
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $localfile);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "username:password"); // see here for auth
curl_exec($ch);
curl_close($ch);
fclose($localfile); //store file data to local file

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

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