繁体   English   中英

如何使用curl和php将文件上传到另一个文件?

[英]How to upload file to another by using curl and php?

$ch = curl_init();
$data = array('name' => 'img', 'file' => $_FILES["img"]["tmp_name"].".png");
curl_setopt($ch, CURLOPT_URL, 'http://www.postto.me/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;

并获得回应?

sender.php

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// true to return the transfer as a string of the return value
// of 'curl_exec' instead of outputting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/receiver.php');
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
  'euro' => '@eurodance.pls',
  'flush' => '@flush_next.png',
  'first_name' => 'Vadim'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
print_r($response);

receiver.php

if(isset($_FILES)){
  $temp_file_name = $_FILES['euro']['tmp_name'];
  $original_file_name = $_FILES['euro']['name'];

  // Find file extention
  $ext = explode ('.', $original_file_name);
  $ext = $ext [count ($ext) - 1];

  // Remove the extention from the original file name
  $file_name = str_replace ($ext, '', $original_file_name);

  $new_name = '_'.$file_name . $ext;
//echo $file_name ." ". $ext;

  if (move_uploaded_file ($temp_file_name, $new_name)) {
      echo "success";
   } else {
      echo "error";
    }

}
if(isset($_FILES)){
  $temp_file_name = $_FILES['flush']['tmp_name'];
  $original_file_name = $_FILES['flush']['name'];

  // Find file extention
  $ext = explode ('.', $original_file_name);
  $ext = $ext [count ($ext) - 1];

  // Remove the extention from the original file name
  $file_name = str_replace ($ext, '', $original_file_name);

  $new_name = '_'.$file_name . $ext;
//echo $file_name ." ". $ext;

  if (move_uploaded_file ($temp_file_name, $new_name)) {
      echo "success";
   } else {
      echo "error";
    }

}

如您所见,它支持多个文件上传。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM