如何使用php CURL发送文件
现在我上传文件到一个网站。它工作得很好。但是我想把文件上传到另一个网站。例如,如果我将文件上载到,同时我希望上载该文件 该文件是图像。所以我使用这样的代码如何使用php CURL发送文件,php,curl,Php,Curl,现在我上传文件到一个网站。它工作得很好。但是我想把文件上传到另一个网站。例如,如果我将文件上载到,同时我希望上载该文件 该文件是图像。所以我使用这样的代码 function SendFileToMobile($image_path, $target_url){ $file_name_with_full_path = realpath($image_path); $post = array('extra_info' => $image_path,'img'=>$file
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);
接受文件是这样的
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);
但不幸的是,它不起作用。它会像这样返回错误消息
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
如果有人有什么错误,请帮助我。消息很清楚,您是否已选中“上载最大文件大小”?谢谢。但该文件已上载到一个站点。如果大于upload_max_filesize,如何上载?消息很清楚,您是否选中了upload_max_filesize?谢谢。但该文件已上载到一个站点。如果它大于upload_max_filesize,如何上载?