如何使用通过POST请求提交的PHP接收图像(文件)
我有一个文件:success.jpg
I have a file: success.jpg
我想通过HTTP POST请求发送此文件,并将其放置在服务器上的公共目录中.
I would like to send this file over an HTTP POST request and have it land in a public directory on my server.
我有一个简单的HTML表单和PHP处理器,如果我是从浏览器上传的,则可以使用:php.net/manual/en/features.file-upload.post-method.php
I have a simple HTML form and PHP processor that work if I'm uploading from the browser: php.net/manual/en/features.file-upload.post-method.php
我试图完全放弃使用表单,而只是通过POST将数据传递到URL(例如,myimageserver.com/public/upload.php).
I'm trying to drop the use of a form altogether and just pass data over POST to a URL (e.g. myimageserver.com/public/upload.php).
似乎我可以使用PHP函数move_uploaded_file,甚至在这里谈论使用POST: http://php.net/manual/zh-CN/function.move-uploaded-file.php ,但是它没有提供可以接收和存储已上传文件的代码开机自检.
It seems that I can use the PHP function move_uploaded_file and it even talks about using POST here: http://php.net/manual/en/function.move-uploaded-file.php but it doesn't provide the code which can receive and store a file that has been uploaded with POST.
有人做过类似的事情吗?
Has anyone ever done something similar?
您应该阅读此示例 http ://www.w3schools.com/php/php_file_upload.asp
基本上这样做是这样的:
which basically does something like this:
<?php
$target_dir = "uploads/";
$target_dir = $target_dir . basename( $_FILES["uploadFile"]["name"]);
$uploadOk=1;
if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_dir)) {
echo "The file ". basename( $_FILES["uploadFile"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
键在$ _FILES全局数组上.
The key is on the $_FILES global array.
要在应用该示例之前检查是否有错误,可以使用以下示例:
To check if there were an error before appliying that example, you could use this example:
if ($_FILES['file']['uploadFile'] === UPLOAD_ERR_OK) {
/**
* Do the upload process mentioned above
**/
} else {
/**
* There were an error
**/
}