将图像从 android 上传到 PHP 服务器

Posted

技术标签:

【中文标题】将图像从 android 上传到 PHP 服务器【英文标题】:Upload an image from android to PHP server 【发布时间】:2015-03-29 14:47:33 【问题描述】:

我正在将图像从 android add 上传到 php 服务器。我从 android 应用程序制作了一个“postFile()”。还有一个 PHP 代码,我收到 权限错误。 在 MacOS 上使用 XAMPP,我应该更改什么权限,或者代码有问题。 请帮帮我。

    PHP 代码

-

<?php
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image

    $check = getimagesize($_FILES["file"]["tmp_name"]);
    if($check !== false) 
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
     else 
        echo "File is not an image.";
        $uploadOk = 0;
    

// Check if file already exists
if (file_exists($target_file)) 
    echo "Sorry, file already exists.";
    $uploadOk = 0;

// Check file size
if ($_FILES["file"]["size"] > 500000) 
    echo "Sorry, your file is too large.";
    $uploadOk = 0;

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) 
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
 else 
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) 
        echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
     else 
        echo "Sorry, there was an error uploading your file.";
    

?>
    postFile()

-

private void postFile()
            try
                String postReceiverUrl = "http://10.0.2.2/testing/getimage.php";
                Log.v(TAG, "postURL: " + postReceiverUrl);

                // new HttpClient
                HttpClient httpClient = new DefaultHttpClient();

                // post header
                HttpPost httpPost = new HttpPost(postReceiverUrl);

                File file = new File(realPath);
                file.getAbsolutePath();
                FileBody fileBody = new FileBody(file);

                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("file", fileBody);
                httpPost.setEntity(reqEntity);

                // execute HTTP post request
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity resEntity = response.getEntity();

                if (resEntity != null) 

                    String responseStr = EntityUtils.toString(resEntity).trim();
                    Log.v(TAG, "Response: " +  responseStr);
  

             catch (NullPointerException e) 
                e.printStackTrace();
             catch (Exception e) 
                e.printStackTrace();
            
        
    LOGCAT

-

01-29 18:46:17.925: V/MainActivity.java(32696): postURL: http://10.0.2.2/testing/getimage.php
01-29 18:46:18.623: V/MainActivity.java(32696): Response: File is an image - image/jpeg.<br />
01-29 18:46:18.623: V/MainActivity.java(32696): <b>Warning</b>:  move_uploaded_file(images/maxresdefault.jpg): failed to open stream: Permission denied in <b>/Applications/XAMPP/xamppfiles/htdocs/testing/getimage.php</b> on line <b>38</b><br />
01-29 18:46:18.623: V/MainActivity.java(32696): <br />
01-29 18:46:18.623: V/MainActivity.java(32696): <b>Warning</b>:  move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpb7Xmt1' to 'images/maxresdefault.jpg' in <b>/Applications/XAMPP/xamppfiles/htdocs/testing/getimage.php</b> on line <b>38</b><br />
01-29 18:46:18.623: V/MainActivity.java(32696): Sorry, there was an error uploading your file.

【问题讨论】:

【参考方案1】:

要修复它,请运行递归命令以确保 Apache 服务具有读/写权限。您应该给予如下许可。

sudo chmod -R 777 /Site_Root_Directory/<rest_path>/

这里的路径可能不同。您只需要注意实际的站点目录路径。

【讨论】:

【参考方案2】:

所以我解决了我的问题,将我的 php 代码更改为以下:

<?php
if($_FILES)
    $file = $_FILES['file'];
    $fileContents = file_get_contents($file["tmp_name"]);
    file_put_contents ("text.jpeg", $fileContents); 

?>

它将图像保存到项目文件夹。

【讨论】:

【参考方案3】:

要将数据从 xampp 上传到 android,您需要允许对该文件夹的权限,例如,假设您的所有图像都在 upload/ 目录下,您需要将该特定文件夹的权限更改为 777:

这就是您需要在MAC 下更改权限的方式

如果您使用的是 ftp:

【讨论】:

以上是关于将图像从 android 上传到 PHP 服务器的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 asynctask 将图像从 android 上传到 php

从android应用程序上传多个图像文件到php服务器

Android:无法使用 php 脚本将图像上传到远程 MySql DB

Android将图像从图库上传到服务器[关闭]

将图像从 iOS 上传到 PHP

将图像从 Android(使用 Android 异步 Http 客户端)上传到 rails 服务器(使用回形针)