日期:2014-05-17  浏览次数:20519 次

为何用Delphi程序写的POST不能上传图片?
我用Delphi写了个POST,想实现自动上传的功能,但是就算我把整个提封包完全模拟下来,也不能成功提交图片,求高手帮忙解答!

以下是PHP的代码
PHP code

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<?php  
//上传文件类型列表  
$uptypes=array(  
    'image/jpg',  
    'image/jpeg',  
    'image/png',  
    'image/pjpeg',  
    'image/gif',  
    'image/bmp',  
    'image/x-png' 
); 
  
$max_file_size=2000000;     //上传文件大小限制, 单位BYTE  
$destination_folder="uploadimg/"; //上传文件路径   
$imgpreview=0;      //是否生成预览图(1为生成,其他为不生成);  
$imgpreviewsize=1/1;    //缩略图比例  
?>  
  
<html>  
<head>  
<title>FileUpload</title>  
<style type="text/css">  
<!--  
body  
{  
     font-size: 9pt;  
}  
input  
{  
     background-color: #66CCFF;  
     border: 1px inset #CCCCCC;  
}  
-->  
</style>  
</head> 
  
<body>  
<form enctype="multipart/form-data" method="post" name="upform">  
  上传文件:  
  <input name="upfile" type="file">  
  <input type="submit" value="上传"> 
<?php  
if ($_SERVER['REQUEST_METHOD'] == 'POST')  
{  
    if (!is_uploaded_file($_FILES["upfile"][tmp_name]))  
    //是否存在文件  
    {  
         echo "图片不存在!";  
         exit;  
    } 
  
    $file = $_FILES["upfile"];  
    if($max_file_size < $file["size"])  
    //检查文件大小  
    {  
        echo "文件太大!";  
        exit;  
    } 
  
    if(!in_array($file["type"], $uptypes))  
    //检查文件类型  
    {  
        echo "文件类型不符!".$file["type"];  
        exit;  
    } 
  
    if(!file_exists($destination_folder))  
    {  
        mkdir($destination_folder);  
    } 
  
    $filename=$file["tmp_name"];  
    $image_size = getimagesize($filename);  
    $pinfo=pathinfo($file["name"]);  
    $ftype=$pinfo['extension'];  
    $destination = $destination_folder.time().".".$ftype;  
    if (file_exists($destination) && $overwrite != true)  
    {  
        echo "同名文件已经存在了";  
        exit;  
    } 
  
    if(!move_uploaded_file ($filename, $destination))  
    {  
        echo "移动文件出错";  
        exit;  
    } 
  
    $pinfo=pathinfo($destination);  
    $fname=$pinfo[basename];  
    echo " <font color=red>已经成功上传</font><br>";  
  
    if($imgpreview==1)  
    {  
    echo "<br>图片预览:<br>";  
    echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);  
    echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">";  
    }  
}  
?> 
  <br>  
</form> 
</body>  
</html>




以下是正常网页提交,和Delphi写的程序POST提交的截包
XML code

POST /aa.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, */*
Referer: http://116.254.216.83/aa.php
Accept-Language: zh-cn
Content-Type: multipart/form-data; boundary=---------------------------7dc38a1d60e20
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 116.254.216.83
Content-Length: 37420
Connection: Keep-Alive
Cache-Control: no-cache

-----------------------------7dc38a1d60e20
Content-Disposition: form-data; name="upfile"; filename="C:\Documents and Settings\Administrator\妗岄潰\Test\789_3.jpg"
Content-Type: image/pjpeg

?

==============