Com base num tutorial (http://masteringionic.com/blog/2018-01-23-using-the-html5-filereader-api-and-php-to-upload-images-from-ionic-framework-apps/) fiz uma aplicação para upload de imagens num servidor Apache, como está descrito no link anterior. No provider criado eu coloquei a URI da minha máquina na rede rodando Apache2 com o arquivo parse-upload.php (este que recebe a imagem e joga em outra pasta) funciona normalmente.
Quando subi o arquivo PHP para uma VPS Hostinger (Debian 8 e Apache2) a imagem não é carregada para o servidor, recebo o seguinte erro no console de debug da aplicação:
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
O arquivo e parse-upload e pastas estão OK na VPS:
O código do parse-upload é este:
<?php
header("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!'));
}
?>
Onde está o URI da pasta que está armazenando as imagens já mudei para
/images_upload
/images_upload/
images_upload/
Nada resolvido (e no servidor apache da minha máquina todos esses caminhos funcionam normalmente).