如何使用通过POST请求提交的PHP接收图像(文件) [英] How to recieve an image (file) with PHP that was submitted over a POST request

查看:1082
本文介绍了如何使用通过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
**/ 
} 

这篇关于如何使用通过POST请求提交的PHP接收图像(文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆