K
kfzd
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个用TIdHttp控件通过http协议上传文件的小程序,遇到
一个很怪异的问题。
如果后台用 IIS+ASP写,就没有问题。但如果是 Apache + PHP,
就不行,老是提示上传的文件为空。
-------------------
系统配置:Windows 2K Professional + sp3
Delphi 6 Enterprise + sp2
IIS 5.0
Apache: Version 1.3.29
PHP: Version 4.3.0
Indy: 9.0.14
-------------------
Delphi 代码如下
procedure TForm1.ButtonPHPClick(Sender: TObject);
var
Source: TMemoryStream ;
Response: TStringStream;
workPostContentstream : TStringStream ;
tep, filename: string;
mstr :TMemoryStream;
const
crLf = #$D#$A;
Sectboundary = '---------------------------7d4882c370382';
begin
filename := Trim(FilenameEdit1.Text);
if FileExists(filename) then
begin
Response := TStringStream.Create('');
Source := TMemoryStream.Create;
workPostContentstream := TStringStream.Create('');
source.LoadFromFile(filename);
IDHTTP1.ProtocolVersion := pv1_1 ;
//IdHTTP1.IOHandler := IdSSLIOHandlerSocket1;
IDHTTP1.Request.ContentType := 'multipart/form-data;boundary=' + Sectboundary;
try
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(format('Content-Disposition: form-data; name="%s" ',['MAX_FILE_SIZE'])); //输入upload file的form name 和 filename
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString('1000000');
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(format('Content-Disposition: form-data; name="%s" ; filename="%s"',['myfile', filename])); //输入upload file的form name 和 filename
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString('Content-Type: text/plain');
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.CopyFrom(source, source.Size);
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(format('Content-Disposition: form-data; name="%s" ',['submit'])); //输入upload file的form name 和 filename
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString('上传');
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行
IDHTTP1.Post(EditPHPURL.Text, workPostContentstream, Response);
tep := Response.DataString;
MemoHttp.Lines.Text := tep;
except
Application.MessageBox('网络连接错误,传送失败!','错误',MB_ICONERROR+MB_OK);
workPostContentstream.Free;
Response.Free;
source.Free;
exit;
end;
workPostContentstream.Free;
Response.Free;
source.Free;
end;
end;
----------------------------------
后台的PHP代码如下:
<?php
//phpinfo();
if($myfile != "none" && $myfile != "") { //有了上传文件了
//设置超时限制时间,缺省时间为 30秒,设置为0时为不限时
$time_limit=60;
set_time_limit($time_limit); //
//把文件内容读到字符串中
$aa = "upload/".basename("$myfile_name");
copy($myfile, "$aa");
unlink($myfile);
echo $aa;
echo "上传成功--- ";
}
else {
echo "你没有上传任何文件";
}
?>
--------------------
asp代码用的是“稻香老农”的上传组件。
--------------
已经搞了一天了,请高手急救!!!
一个很怪异的问题。
如果后台用 IIS+ASP写,就没有问题。但如果是 Apache + PHP,
就不行,老是提示上传的文件为空。
-------------------
系统配置:Windows 2K Professional + sp3
Delphi 6 Enterprise + sp2
IIS 5.0
Apache: Version 1.3.29
PHP: Version 4.3.0
Indy: 9.0.14
-------------------
Delphi 代码如下
procedure TForm1.ButtonPHPClick(Sender: TObject);
var
Source: TMemoryStream ;
Response: TStringStream;
workPostContentstream : TStringStream ;
tep, filename: string;
mstr :TMemoryStream;
const
crLf = #$D#$A;
Sectboundary = '---------------------------7d4882c370382';
begin
filename := Trim(FilenameEdit1.Text);
if FileExists(filename) then
begin
Response := TStringStream.Create('');
Source := TMemoryStream.Create;
workPostContentstream := TStringStream.Create('');
source.LoadFromFile(filename);
IDHTTP1.ProtocolVersion := pv1_1 ;
//IdHTTP1.IOHandler := IdSSLIOHandlerSocket1;
IDHTTP1.Request.ContentType := 'multipart/form-data;boundary=' + Sectboundary;
try
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(format('Content-Disposition: form-data; name="%s" ',['MAX_FILE_SIZE'])); //输入upload file的form name 和 filename
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString('1000000');
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(format('Content-Disposition: form-data; name="%s" ; filename="%s"',['myfile', filename])); //输入upload file的form name 和 filename
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString('Content-Type: text/plain');
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.CopyFrom(source, source.Size);
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行
workPostContentstream.WriteString(format('Content-Disposition: form-data; name="%s" ',['submit'])); //输入upload file的form name 和 filename
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString('上传');
workPostContentstream.WriteString(crLf);
workPostContentstream.WriteString(Sectboundary); //分隔符
workPostContentstream.WriteString(crLf); // 换行
IDHTTP1.Post(EditPHPURL.Text, workPostContentstream, Response);
tep := Response.DataString;
MemoHttp.Lines.Text := tep;
except
Application.MessageBox('网络连接错误,传送失败!','错误',MB_ICONERROR+MB_OK);
workPostContentstream.Free;
Response.Free;
source.Free;
exit;
end;
workPostContentstream.Free;
Response.Free;
source.Free;
end;
end;
----------------------------------
后台的PHP代码如下:
<?php
//phpinfo();
if($myfile != "none" && $myfile != "") { //有了上传文件了
//设置超时限制时间,缺省时间为 30秒,设置为0时为不限时
$time_limit=60;
set_time_limit($time_limit); //
//把文件内容读到字符串中
$aa = "upload/".basename("$myfile_name");
copy($myfile, "$aa");
unlink($myfile);
echo $aa;
echo "上传成功--- ";
}
else {
echo "你没有上传任何文件";
}
?>
--------------------
asp代码用的是“稻香老农”的上传组件。
--------------
已经搞了一天了,请高手急救!!!