如何使用php CURL发送文件

时间:2016-08-22 22:23:09

标签: php curl

现在我将文件上传到一个网站。它运作良好。但我想将文件上传到另一个站点。例如,如果我将文件上传到http://example.com,同时我要上传http://example2.com

该文件是图像。所以我使用这样的代码。

function SendFileToMobile($image_path, $target_url){

   $file_name_with_full_path = realpath($image_path);

   $post = array('extra_info' => $image_path,'img'=>$file_name_with_full_path);
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL,$target_url);
   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
   $result=curl_exec ($ch);
   curl_close ($ch);
}

接受文件是这样的。

$imagePath = "image/user/";

$allowedExts = array("gif", "jpeg", "jpg", "png", "GIF", "JPEG", "JPG", "PNG");
$temp = explode(".", $_FILES["img"]["name"]);
$extension = end($temp);

//Check write Access to Directory

if(!is_writable($imagePath)){
    $response = Array(
        "status" => 'error',
        "message" => 'Can`t upload File; no write Access',
        "path" => $imagePath
    );
    print json_encode($response);
    return;
}

if ( in_array($extension, $allowedExts))
  {
  if ($_FILES["img"]["error"] > 0)
    {
         $response = array(
            "status" => 'error',
            "message" => 'ERROR Return Code: '. $_FILES["img"]["error"],
        );          
    }
  else
    {

      $filename = $_FILES["img"]["tmp_name"];
      list($width, $height) = getimagesize( $filename );

      move_uploaded_file($filename,  $imagePath . $_FILES["img"]["name"]);

      $response = array(
        "status" => 'success',
        "url" => $imagePath.$_FILES["img"]["name"],
        "width" => $width,
        "height" => $height
      );

    }
  }
else
  {
   $response = array(
        "status" => 'error',
        "message" => 'something went wrong, most likely file is to large for upload. check upload_max_filesize, post_max_size and memory_limit in you php.ini',
    );
  }

  print json_encode($response);

但不幸的是它不起作用。它会返回错误消息。

  出了问题,很可能文件很大,无法上传。在php.ini中检查upload_max_filesize,post_max_size和memory_limit

如果有人出错,请帮助我。

0 个答案:

没有答案