我正在开发一个程序,我需要上传一个' log.txt'到网络服务器..由http
我已经搜索并了解到: -
Delphi XE:
procedure TForm1.Button1Click(Sender: TObject);
begin
idhttp1.Post('http://127.0.0.1/GET2.php', 'C:\Users\Someone\Desktop\log.txt')
end;
php: -
<?php
$uploaddir = "uploads/";
$uploadfile = $uploaddir . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
echo "The file has been uploaded successfully";
}
else
{
echo "There was an error uploading the file";
}
?>
但没有上传的文件
感谢您的帮助
答案 0 :(得分:13)
阅读PHP文档。
您的PHP代码正在使用$_FILES
,它希望客户端以MIME colorbutton
格式发布HTML网络表单:
Handling file uploads&gt; POST method uploads
PHP能够从任何 RFC-1867 兼容的浏览器接收文件上传。
RFC 1867 Form-based File Upload in HTML
但是,这种格式不是您的应用程序实际发布的内容。它只是按原样发布原始文件内容,没有MIME元数据来描述文件。这就是你的PHP代码无法正常工作的原因。
要使用multipart/form-data
发布multipart/form-data
HTML网络表单,您必须使用TIdHTTP.Post()
类,例如:
TIdMultipartFormDataStream
或者,PHP还支持HTTP uses
..., IdHTTP, IdMultipartFormDataStream;
procedure TForm1.Button1Click(Sender: TObject);
var
PostData: TIdMultipartFormDataStream;
begin
PostData := TIdMultipartFormDataStream.Create;
try
PostData.AddFile('file', 'C:\Users\Someone\Desktop\log.txt');
idhttp1.Post('http://127.0.0.1/GET2.php', PostData)
finally
PostData.Free;
end;
end;
文件上传请求:
Handling file uploads&gt; PUT method support
PUT
procedure TForm1.Button1Click(Sender: TObject);
var
FS: TFileStream;
begin
FS := TFileStream.Create('C:\Users\Someone\Desktop\log.txt', fmOpenRead or fmShareDenyWrite);
try
idhttp1.Put('http://127.0.0.1/log.txt', FS);
finally
FS.Free;
end;
end;