网上一堆代码,没有一个真正实现curl传图片成功过的,这儿,我来个成功的,超级简单,扩展性强【广告一下】
上传端:
接收端:
上传端:
<?php
function upload_curl_pic()
{
$url = 'http://localhost//5-5-5//uploadfile.php'; //target url
$file = 'c:/21.jpg'; //要上传的文件
$fields['f'] = '@'.$file;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
curl_exec( $ch );
if ($error = curl_error($ch) ) {
die($error);
}
curl_close($ch);
}
upload_curl_pic();//可以扩展把图片地址传到函数中
?>
function upload_curl_pic()
{
$url = 'http://localhost//5-5-5//uploadfile.php'; //target url
$file = 'c:/21.jpg'; //要上传的文件
$fields['f'] = '@'.$file;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
curl_exec( $ch );
if ($error = curl_error($ch) ) {
die($error);
}
curl_close($ch);
}
upload_curl_pic();//可以扩展把图片地址传到函数中
?>
接收端:
<?php
$uploaddir = 'E:\\wwwroot\\www\\htdocs\\5-5-5\\pic_all_here\\';
$uploadfile = $uploaddir . $_FILES['f']['name'];
if (move_uploaded_file($_FILES['f']['tmp_name'], $uploadfile))
{
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
echo 'Here is some more debugging info:';
}
?>
$uploaddir = 'E:\\wwwroot\\www\\htdocs\\5-5-5\\pic_all_here\\';
$uploadfile = $uploaddir . $_FILES['f']['name'];
if (move_uploaded_file($_FILES['f']['tmp_name'], $uploadfile))
{
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
echo 'Here is some more debugging info:';
}
?>
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1771/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2009-5-12 15:38
评论列表
2014-3-27 10:16 | lvv2
不错,直接拿来用了..
2012-9-26 10:00 | 8110103278
你好 假如我post其他数据时候最前面也是@这个符号。请问有啥好办法解决么
分页: 1/1 1