简体   繁体   English

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

[英]Upload an image from android to PHP server

I am uploading an image from android add to PHP server. 我正在将图像从android add上传到PHP服务器。 I have made a "postFile()" from android app. 我已经从Android应用程序中制作了一个“ postFile()”。 And a PHP code, I am getting an error for permissions . 和一个PHP代码,我得到一个权限错误 Using XAMPP on MacOS , what permission should I change, or is there a problem with code. 在MacOS上使用XAMPP ,我应该更改什么权限,或者代码有问题。 Please help me out. 请帮帮我。

  1. PHP code 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.";
    }
}
?>
  1. postFile() 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();
            }
        }
  1. LOGCAT 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.

To fix it, Run the recursive command to ensure that the Apache service has read/write permissions. 要对其进行修复,请运行递归命令以确保Apache服务具有读/写权限。 You should give permission like below. 您应按以下方式授予许可。

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

Here might be path can be differ. 这可能是路径可能不同。 You just need to take care for actual Site directory path. 您只需要注意实际的站点目录路径。

So i solved my problem, changed my php code to following: 所以我解决了我的问题,将我的PHP代码更改为以下内容:

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

It will save image to project folder. 它将图像保存到项目文件夹。

For uploading data from xampp to android, you need to permit permissions to that folder for example all your images are suppose under upload/ dir the you need to chnage permssion as 777 for that particular folder : 为了将数据从xampp上传到android,您需要允许该文件夹的权限,例如,所有图像都假定位于upload / dir下,因此您需要将特定文件夹的权限更改为777:

This is how you need to chnage permssion under MAC 这就是您需要在MAC下更改权限的方式

If you are using ftp: 如果您使用的是ftp:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM