Si vous êtes amené à envoyer un fichier via cUrl en php, voici de quoi vous mettre sur la piste !
Le fichier permettant l’envoi :
curl_postfile.php
<?php
/**
* curl_postfile.php
* Copyright (C) MazeSloup - mazesloup [at] gmail [dot] com
* http://mazesloup.fr
* http://blog.mazesloup.fr/index.php/post/2010/10/27/php-envoyer-un-fichier-avec-curl
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Fonction qui envoie le fichier $My_File vers $CURL_POSTURL
* @param $My_File Nom et chemin du fichier
* @param $CURL_POSTURL Adresse de la page qui doit recevoir et gérer les opérations
* @return $response Retourne les informations concernant le traitement de l'exécution
*/
function curl_upload_file($My_File, $CURL_POSTURL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $CURL_POSTURL);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
'My_File' => '@'.$My_File,
'Another_Field' => 'Yes!'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
return $response;
}
/**
* Le fichier test.txt est un document se trouvant dans le même dossier que les fichiers .php
* L'Url est une Url locale dont le domaine est configuré sur le serveur local http://test/
*/
curl_upload_file('./test.txt','http://test/curl_postfile_receive.php');
?>
Le fichier permettant la réception
curl_postfile_receive.php
<?php
/**
* curl_postfile_receive.php
* Copyright (C) MazeSloup - mazesloup [at] gmail [dot] com
* http://mazesloup.fr
* http://blog.mazesloup.fr/index.php/post/2010/10/27/php-envoyer-un-fichier-avec-curl
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Chemin où sera enregistré le fichier
*/
$FILE_FOLDER='/tmp';
/*
* Vérification de l'existence de $_FILES qui contient les informations sur le fichier envoyé
*/
if(isset($_FILES)){
/*
* Vérification que le fichier provient bien du serveur
*/
if(is_uploaded_file($_FILES['My_File']['tmp_name'])){
/*
* Déplacement du fichier au bon endroit avec son nom d'origine
*/
move_uploaded_file($_FILES['My_File']['tmp_name'],$FILE_FOLDER.'/'.$_FILES['My_File']['name']);
}
}
?>
Cela ne représente qu’une partie de la gestion de cUrl, mais une bonne base si vous avez besoin d’envoyer des fichiers de cette manière.