1

I am uploading an image from android add to PHP server. I have made a "postFile()" from android app. And a PHP code, I am getting an error for permissions. Using XAMPP on MacOS, what permission should I change, or is there a problem with code. Please help me out.

  1. PHP code

-

<?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()

-

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

-

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.
0

3 Answers 3

1

To fix it, Run the recursive command to ensure that the Apache service has read/write permissions. 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.

Sign up to request clarification or add additional context in comments.

Comments

1

So i solved my problem, changed my php code to following:

<?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.

Comments

0

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 :

This is how you need to chnage permssion under MAC

If you are using ftp:

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.