上传使用文件要求方法将PHP服务器从Android电子图像

问题描述:

所以我希望从用户的手机上传图片到使用文件要求方法的PHP服务器。我觉得这是更好的选择相比,POST作为。我见过样品code这极少数,但它总是使用POST和包括像头,字符串或民企。我只是想上传图片,没有别的。因此,这里是我的PHP:

So I'm looking to upload an image from the user's phone to a php server using the FILES request method. I think this is the better choice as compared to POST. I've seen a handful of sample code for this, but it always uses POST and includes things like headers, strings, or MPEs. I just want to upload the image, nothing else. So here is my php:

$uploadedFile = $_FILES['image']['name'];

$uploadedType = $_FILES['image']['type'];

$uploadedSize = $_FILES['image']['size'];

$temp = $_FILES['image']['tmp_name'];

$error = $_FILES['image']['error'];

if ($error > 0) {
    die("File could not be uploaded. $error");
}
else {
    if ($uploadedType != ("image/jpg" || "image/png") || $uploadedSize > 500000) {
        die("Sorry, png and jpgs are the only supported filetypes.");
    }
    else {
        move_uploaded_file($temp, "images/".$uploadedFile);
        echo "Upload Complete. ".$uploadedType;
    }

}

这适用于基于HTML的客户端。对于Android,我能够检索图片的路径,所以现在我正在做的方法将采取的路径作为参数,然后上传图片@给定的路径到Web服务器。

This works with an html-based client. For android, I am able to retrieve the image's path, so right now I'm making a method which will take the path as a parameter, and then upload the image @ the given path to the web server.

这会造成混乱的具体问题:

Specific questions which cause confusion:


  1. 为什么使用这个职位大家?我觉得FILES是我所知道的PHP是更好的选择。如果我错了,请你澄清。

  1. Why is everyone using POST for this? I feel that FILES is the better choice from what I know of PHP. If I'm wrong please clarify.

我的道路,所以我会做这样的事情。

I have the path, so I'd do something like

文件文件=新的文件(路径);

然后...

FileInputStream fis = new FileInputStream(file);

那么是什么在这里?对不起,有点新的这个。

But then what from here? Sorry, a bit new at this.

所以啊,我只需要在正确的方向,我认为一推,但大部分我见过的答案是不是真的想完成我的,或者至少我是这样认为。感谢您的时间。

So yea, I just need a push in the right direction I think, but most of the answers I've seen aren't really trying to accomplish what I am, or at least so I think. Thanks for your time.

$ _ FILES 变量的的从POST请求填充。请参阅 PHP文档 - 它甚至titeled帖子上传方法。

The $_FILES variable is populated from a POST request. See the php documentation - It's even titeled "Post upload methods".

一个简单的例子,让你开始:

A simplified example to get you started:

public void upload(File file) throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httpPost = new HttpPost();
    httpPost.setEntity(new FileEntity(file, "image/png"));
    client.execute(httpPost);
}