利用curl来模拟表单提交上传图片

发布时间:2014-01-08 21:39:43 阅读:1322次

submit.php 

<?php

header("content-type:text/html;charset=utf-8");

//http://huangliangfeixu.blog.163.com/blog/static/18974706220112942628354/

$file = './img.jpg'; //要上传的文件

$url  = 'http://localhost:8088/login/curl/upload/upload.php';//target url

$fields['file'] = '@'.$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.php

<?php

print_r($_FILES);

$now=time();

$uploadfile="uploadfile/".$now.".jpg";

if(move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile))

{

echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile'></center>";

}

else

{

echo"传输失败!";

?>

相同目录下的图片文件img.jpg以及文件夹uploadfile


通过表单的形式来上传图片,最终action文件再借助curl

form.php
<form method=post name=myform action="upload.php" enctype="multipart/form-data">
<input type="file" name="userfile">
<input type="submit" value="上传图片" name="file">
</form>
upload.php
<?php
    $file=$_FILES["file"]["tmp_name"];
    $filetype=$_FILES["file"]["type"];
    $url="http://localhost/uploadnew.php";
    $ch = curl_init();
    //$fields['file'] = '@'.$file;
    $fields['file'] = '@'.$file.';type='.$filetype;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//输出内容为字符串
    $content=curl_exec($ch);
    curl_close($ch);
    print_r($content);  
?>

http://localhost/uploadnew.php
<?php
//phpinfo();
function randomkeys($length)
{
 $pattern='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
 for($i=0;$i<$length;$i++)
 {
   $key .= $pattern{mt_rand(0,35)};    //生成php随机数
 }
 return $key;
}
$rand=randomkeys(8);
//print_r($_FILES);
$tmp_name=$_FILES["file"]["tmp_name"];
$img_type=$_FILES["file"]["type"];
//echo "img_type=>".$img_type;
$year=date("Y");
$month=date("m");
$day=date("d");
if(!file_exists($year)){
        mkdir($year);
}
if(!file_exists($year."/".$month.$day)){
        mkdir($year."/".$month.$day);
}
$allowtype=array("image/jpg","image/jpeg","image/png","image/x-png","image/gif");
$phpcheck=@getimagesize($tmp_name);
if(!is_array($phpcheck)){
        echo "not img";
        exit;
}
if(!in_array($img_type,$allowtype)){
        echo "not allow img";
        exit;
}
$resignname=md5($rand.time().$tmp_name);
if($img_type=="image/jpg"||$img_type=="image/jpeg"){
        $ext=".jpg";
}
if($img_type=="image/png"||$img_type=="image/x-png"){
        $ext=".png";
}
if($img_type=="image/gif"){
        $ext=".gif";
}
$newname=$resignname.$ext;
$newname=substr($newname,16);
$file_dest=$year."/".$month.$day."/".$newname;
if(move_uploaded_file($tmp_name,$file_dest)){
        echo "http://localhost/".$year."/".$month.$day."/".$newname;
}else{
        echo "error";
}
?>

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:利用curl来模拟表单提交上传图片 出自老鄢博客 | 欢迎分享