2012-08-14 5 views
0

Je télécharge un fichier sur le serveur distant. Le code:Téléchargement de fichier cURL PHP Ajout d'informations au fichier

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, ZDURL.$url); 
    curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY); 

    $params = array('file_name' => '@'.$temp_file); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/plain")); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt($ch, CURLINFO_HEADER_OUT, 0); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    $result = curl_exec($ch); 
    $error = curl_error($ch); 
    curl_close($ch); 

Mais dans le haut du fichier téléchargé apparaît informations:

------------------------------cfbe90a606af 
Content-Disposition: form-data; name="file_name"; filename="C:\Program Files\xampp\tmp\phpF576.tmp" 
Content-Type: application/octet-stream 

images ont un format incorrect en raison de ce texte d'addition

+0

'Content-Type: text/plain' n'a pas de sens ici étant donné que vous parlez d'images (et en général lors du passage d'un tableau à' CURLOPT_POSTFIELDS'). Qu'est-ce que vous essayez de faire, imiter une soumission de formulaire, poster une image brute ou quoi? – DaveRandom

+0

POSTE une image brute. J'utilise cet exemple http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html –

Répondre

0

Il semble que l'URL que vous envoyez des données à attend seulement un fichier dans le corps de la requête, plutôt qu'une soumission de formulaire. La meilleure façon d'y parvenir est avec CURLOPT_INFILE. Essayez ce code:

Essayez cette

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, ZDURL.$url); 
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY); 

// You need to detect the correct content type for the file you are sending. 
// Although based on the current problem you have, it looks like the server 
// ignores this anyway 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg")); 

// Send the raw file in the body with no other data 
$fp = fopen($temp_file, 'r'); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLINFO_HEADER_OUT, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

$result = curl_exec($ch); 
$error = curl_error($ch); 

curl_close($ch); 
fclose($fp); 
+0

Merci! Code de travail: –