2017-10-16 1 views
0

J'ai essayé de télécharger un fichier via Icecat. Le fichier a besoin d'un accès pour être téléchargé.Comment obtenir correctement un fichier via file_get_content ou curl

Mon problème est:

  • le file_get_content Does'nt télécharger le fichier. Mon répertoire est sur 777 et le chemin est correct.
  • Si j'insère le document dans le répertoire, le fichier n'est pas décompressé.

    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; 
         } 
         } 
        } 
    

Répondre

0

Faire un essai comme celui-ci pour télécharger le fichier qui a nom d'utilisateur etmot de passe l'authentification en utilisant 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