Upload image to server Apache2 - Ionic 3 + PHP

2

Based on a tutorial ( link ) I made an application to upload images on an Apache server, as described in the previous link. In the created provider I put the URI of my machine on the network running Apache2 with the file parse-upload.php (which receives the image and plays in another folder) works normally.

When I uploaded the PHP file to a Hostinger VPS (Debian 8 and Apache2) the image is not loaded to the server, I get the following error in the application's debug console:

HttpErrorResponse
error:
error: SyntaxError: Unexpected token { in JSON at position 52 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:8080/build/vendor.js:79316:51) at t.invokeTask (http://localhost:8080/build/polyfills.js:3:15660) at Object.onInvokeTask (http://localhost:8080/build/vendor.js:5125:33) at t.invokeTask (http://localhost:8080/build/polyfills.js:3:15581) at r.runTask (http://localhost:8080/build/polyfills.js:3:10834) at e.invokeTask [as invoke] (http://localhost:8080/build/polyfills.js:3:16794) at p (http://localhost:8080/build/polyfills.js:2:27648) at XMLHttpRequest.v (http://localhost:8080/build/polyfills.js:2:27893)
text: "{"Erro":"Error! The supplied data was NOT written "}{"Sucesso":"The file was successfully uploaded"}"
__proto__: Object
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://IP_DA_VPS/uploadimg/parse-upload.php"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://IP_DA_VPS/uploadimg/parse-upload.php"
__proto__: HttpResponseBase

The file and parse-upload and folders are OK in VPS:

Theparse-uploadcodeisthis:

<?phpheader("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');


   // Retrieve and decode the posted JSON data
   $posted        = file_get_contents("php://input");
   $obj           =  json_decode($posted);


   // Separate out the supplied keys/values
   $fileName      =  strip_tags($obj->name);
   $fileData      =  strip_tags($obj->file);


   // Format the supplied base64 data URI so that we remove the initial base64 identifier
   $uri           =  substr($fileData,strpos($fileData,",")+1);


   // Replace any spaces in the formatted base64 URI string with pluses to avoid corrupted file data
   $encodedData   = str_replace(' ','+',$uri);


   // Decode the formatted base64 URI string
   $decodedData   = base64_decode($encodedData);


   try {

      // Write the base64 URI data to a file in a sub-directory named uploads
      if(!file_put_contents('/images_upload' . $fileName, $decodedData))
      {
         // Uh-oh! Something went wrong - inform the user
         echo json_encode(array('Erro' => 'Error! The supplied data was NOT written '));
      }

      // Everything went well - inform the user :)
      echo json_encode(array('Sucesso' => 'The file was successfully uploaded'));

   }
   catch(Exception $e)
   {
      // Uh-oh! Something went wrong - inform the user
      echo json_encode(array('Falha' => 'Fail!'));
   }


?>

Where is the URI of the folder that is storing the images I have already changed to

  

/ images_upload

     

/ images_upload /

     

images_upload /

Nothing solved (and on the apache server on my machine all these paths work normally).

    
asked by anonymous 16.11.2018 / 18:15

1 answer

1

Changed the storage path of the images and installed PHP 5 in Debian VPS 8.

if(!file_put_contents('/images_upload' . $fileName, $decodedData))

It looks like this:

if(!file_put_contents('/images' . $fileName, $decodedData))

And to install PHP5 - >

  

apt-get install php5-common libapache2-mod-php5 php5-cli

And not least, I gave a CHMOD 777 in the folder that is receiving the images

    
16.11.2018 / 20:30