2009-11-07 10 views

Répondre

7

l'idée de base

<?php 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_VERBOSE, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)"); 
    curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL); 
    curl_setopt($ch, CURLOPT_POST, true); 
    // same as <input type="file" name="file_box"> 
    $post = array(
     "file_box"=>"@/path/to/myfile.jpg", 
     "username"=>"foobar", 
     "password"=>"secret", 
     "submit"=>"submit" 
    ); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch); 
?> 

vous pouvez avoir plus d'informations sur boucle here.

+0

Bonjour RageZ, Merci pour la réponse. Cependant, je n'utilise pas de tableau dans mon code. J'utilise comme ci-dessous: $ Post = Filefieldname = "" Maintenant, je ne sais pas quoi écrire dans ces "" afin que l'image peut être téléchargée. – Aditya

+0

vous devriez utiliser un tableau qui est la façon dont curl_setopt est faite, si vous n'utilisez pas array, vous devez encoder le fichier vous-même. Pourquoi êtes-vous incapable d'utiliser le tableau? – RageZ

+0

Les champs de navigation ne sont pas automatiquement remplis quand j'utilise le tableau donc j'ai utilisé une seule ligne comme $ post = "nom_de_champ = nom & mot_de_passe = mot de passe & nomfichier_fichier = & nom_bouton = Continuer"; Je me demandais donc si je pouvais remplacer les points d'interrogation ci-dessus par quelque chose qui chargerait l'image. – Aditya

1
<?php 

/* 
ini_set('display_errors',1); 
error_reporting(E_ALL); 
*/ 
include('_db.php'); 
include('_session.php'); 




$business_id = $session->business->id; 
$error = ""; 
$output = ""; 

if ($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/pjpeg" || $_FILES["file"]["type"] == "image/png") 
{ 
    if ($_FILES["file"]["error"] > 0) 
    { 
     $error = $_FILES["file"]["error"]; 
     echo "{error: '". $error ."', msg: ''}"; 
    } 
    else 
    { 
     //set POST variables 
     $url = 'http://img.mySite.com/'; 

     $fields = array(
        //assign filetype the file extension 
        'filetype'=>substr(strrchr($_FILES["file"]["name"], '.'), 1), 
        //give the file id a unique id 
        'fileid'=>$business_id . ":" . date('YmdGisu') .":". $_FILES["file"]["name"], 
        //read image data into a string using file get contents 
        'content'=>file_get_contents($_FILES['file']['tmp_name']) 
       ); 

     //open connection 
     $ch = curl_init(); 

     //set the url, number of POST vars, POST data 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_VERBOSE, 0); 
     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)"); 
     curl_setopt($ch,CURLOPT_URL,$url); 
     curl_setopt($ch,CURLOPT_POST,true); 
     curl_setopt($ch,CURLOPT_POSTFIELDS,$fields); 
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 

     //execute post 
     $output = curl_exec($ch); 
     if($output == false) 
      $error = "Fail."; 
     echo "{error: '". $error ."', msg: '" . $output . "'}"; 

     //close connection 
     curl_close($ch); 
    } 
    } 
    else 
    { 
    $error = "Incorrect File Format."; 
    echo "{error: '". $error ."', msg: ''}"; 
    } 
mysql_close($link); 

?> 
Questions connexes