When I run my website on localhost with Xampp, I haven't this bug but on my ovh server this doesn't work.
I explain, I have a ajax function who wait a filename for inject him in balise . I call my controller who genere the filename and echo him. So I have a var php who containe a string like "main-2765b74ec.jpg".
I need echo this var for inject him in my balise. On localhost this work perfectly, but in my ovh server the echo print just "main-2765b74ec".
So that is my question, why echo don't give me the complete string like in my localhost env.
Maybe a problem with ovh ?
EDIT: Same problem with print()
php call with ajax :
public function tmp($request, $response) {
$type = $request->getParam('name');
$uploadedFile = $request->getUploadedFiles()['img'];
$directory = dirname(dirname(__DIR__)) . '/public/img/tmp';
if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
$filename = $this->moveUploadedFile($directory, $uploadedFile, $type);
echo $filename;
}
}
above : If I do a var_dump this return the complete string with the extension and if I do a var_dump follow by an echo, the echo return the complete string but I must do more traitement in my js for hide all the var_dump part.
php who genere name :
public function moveUploadedFile($directory, UploadedFile $uploadedFile, $type = 'any') {
$extension = strtolower(pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION));
$basename = bin2hex(random_bytes(8));
$filename = sprintf('%s.%0.8s', $basename, $extension);
if ($type == 'main') {
$filename = $type.'-'.$filename;
if ($dh = opendir($directory)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..' && explode('-', $file)[0] == 'main') {
unlink($directory.'/'.$file);
}
}
closedir($dh);
}
} else if ($type != 'any') {
$filename = $type;
}
$uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $filename);
return $filename;
}
ajax :
function uploadFile(file) {
let id = $(file).attr('id').substr(5)
if (id != ('tmp')) {
$('#form_'+id).submit()
} else {
let formData = new FormData()
let params = $('#form_tmp').serializeArray()
formData.append('img', $(file)[0].files[0])
formData.append(params[0].name, params[0].value)
$('#label').html("Uploading...").css({'background-color': 'transparent', 'cursor': 'wait'})
$('#file_tmp').prop("disabled", true)
// for (var pair of formData.entries()) { console.log(pair[0]+ ', ' + pair[1]) }
$.ajax({
url: '../image/tmp',
method: 'post',
data: formData,
contentType: false, //'text/plain'
processData: false
}).done((filename) => {
closeOverlay()
if (params[0].value == 'main') {
$('.container-main-img').html('<img src="../img/tmp/'+filename+'" id="tmpImgMain">').css({'height':'auto', 'border': 'none'})
} else {
$('.img-list').append(`<div class="img-block">
<div class="toolbar img">
<div>
<button class="btn-tmp-img-up"><i class="icon-up-dir"></i></button>
<button class="btn-tmp-img-down"><i class="icon-down-dir"></i></button>
</div>
<button class="btn-tmp-img-delete"><i class="icon-cancel"></i></button>
</div>
<img src="../img/tmp/`+filename+`">
</div>`)
ajustBtnTool()
}
})
}
}
When returning from a function called by Ajax, you must use 'exit' or 'die()', so add either one of these straight after your line 'echo $filename;'.