1

i have the following code in android

String uploadId = UUID.randomUUID().toString();
            //Creating a multi part request
            new MultipartUploadRequest(getContext(), uploadId, url_upload_cert)
                    .addFileToUpload(path, "pdf") //Adding file
                    .addParameter("name", name) //Adding text parameter to the request
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload();

and in php

<?php

//importing dbDetails file
require_once __DIR__ . '/db_connect.php';

//this is our upload folder
$upload_path = 'android/';

//Getting the server ip
$server_ip = gethostbyname(gethostname());

//creating the upload url
//$upload_url = 'http://'.$server_ip.'/pavo_project/'.$upload_path;
$upload_url='/src';

//response array
$response = array();



if($_SERVER['REQUEST_METHOD']=='POST'){

//checking the required parameters from the request
if(isset($_POST['name']) and isset($_FILES['jpg']['name'])){

    // connecting to db
    $db = new DB_CONNECT();

    //getting name from the request
    $name = $_POST['name'];

    //getting file info from the request
    $fileinfo = pathinfo($_FILES['jpg']['name']);

    //getting the file extension
    $extension = $fileinfo['extension'];

    //file url to store in the database
    $file_url = $upload_url . $name . '.' . $extension;

    //file path to upload in the server
    $file_path = $upload_url . $name . '.'. $extension;

    //trying to save the file in the directory
    try{
        //saving the file
        move_uploaded_file($_FILES['jpg']['tmp_name'],$file_path);
    }catch(Exception $e){
        $response['error']=true;
        $response['message']=$e->getMessage();
    } 
    //closing the connection
   // mysqli_close($db);
}else{
    $response['error']=true;
    $response['message']='Please choose a file';
}

//displaying the response
echo json_encode($response);
}
?>

when i run the android code it runs succesfully with no error but when i check the image in the src folder its not there The same things happens when i test the script using postman Am using wamp server on windows . what might be the reason causing this?

Jeff
  • 6,895
  • 1
  • 15
  • 33

1 Answers1

1

Check the writing permissions of your folder.

For example in Linux, a level above your folder you can user the following command:

sudo chmod -R 777 <your-folder-name>

And that might let the php code save the file inside of it, because it will grant the permissions to write/read/delete to every user.

Also you can check the php.ini file, and check if the line:

file_uploads = On

Has the On value

If that doesn't work you can check for a default php.ini file and try replacing your php.ini file with that.

Erick Elizondo
  • 188
  • 3
  • 13
  • Since am in windows where can I find the file php.ini please ? – Bernie gach Oct 09 '18 at 18:35
  • @Berniegach it depends of the server that you're using, for example if you use XAMPP with a default instalation: you should find the file in C:\xampp\php\php.ini. Also you could run the code '' on a php file, as shown in this question: https://stackoverflow.com/questions/13984456/where-is-php-ini to see where is your file on the row ('Loaded configuration file'). – Erick Elizondo Oct 09 '18 at 18:43
  • The file_uploads variable in php.ini is on – Bernie gach Oct 09 '18 at 18:49
  • Try checking the permissions of your upload folder ''android/' and setting write and reading for everyone. – Erick Elizondo Oct 09 '18 at 18:55
  • Am changing the write property to ticked in security options of file property but the attribute on General keeps saying Read only – Bernie gach Oct 09 '18 at 19:04
  • Check if you're using an administrator account before setting the write property also you can try running this command in the console(running the console as administrator) `icacls "" /grant Everyone:M` – Erick Elizondo Oct 09 '18 at 19:10
  • Thanks for the help but it hasn't solved the issue . – Bernie gach Oct 09 '18 at 19:24
  • Thanks for your help but it hasn't solved the issue . Ill try running android studio and postman in linux and see if it helps – Bernie gach Oct 09 '18 at 19:25