Can not open uploaded file in server

2019-08-24 10:21发布

Background :

User upload image & click on "save" button.

I saved the image in server successfully. I gave 777 permisson to folder....

enter image description here

Issue :

I tried to open image, it don't display properly : Image url

enter image description here

Html

<button class ="save" onclick="test()">Save image to server</button>

Script :

function test(){
var canvas  = document.getElementById("0");
var dataURL = canvas.toDataURL();  // THE BASE 64 DATA
var dataFileName = document.getElementById('fileup').value.replace(/.*(\/|\\)/, ''); // GET THE FILE NAME THAT USER CHOSE
var dataFileType = dataFileName.split('.').pop();

$.ajax({
  type: "POST",
  url: "tamaker.php",
  data: { 
     imgBase64: dataURL,
     imgFileName: dataFileName,
     imgFileType: dataFileType
  }
}).done(function(o, imgFileName) {

  console.log(o);  
  var response = JSON.parse(o);
  console.log(response);

  $('body').prepend('<img src="' + dataFileName+ '" style="height: 200px; width: auto;">');

});
}

save.php

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if( isset($_POST['imgBase64']) && isset($_POST['imgFileName']) && isset($_POST['imgFileType']) ){

    $fname = filter_input(INPUT_POST, 'imgFileName'); // THE FILENAME THE USER CHOSE IS RECEIVED VIA POST
    $img = filter_input(INPUT_POST, 'imgBase64');  // THE BASE64 ENCODING RECEIVED VIA POST
    $imgtype = filter_input(INPUT_POST, 'imgFileType');  // THE FILE TYPE / EXTENSION IS RECEIVED VIA POST

    // STRIP OFF THE BEGINNING OF THE BASE64 DATA, BUT DEPENDS ON THE IMAGE TYPE.  
    // I COULD HAVE SIMPLIFIED THIS BUT USED IF STATEMENTS.
    if ( $imgtype === 'png'){
        $img = str_replace('data:image/png;base64,', '', $img);
    };
    if ( $imgtype === 'jpg' || $imgtype === 'jpeg'){
        $img = str_replace('data:image/jpeg;base64,', '', $img);
    };
    if ( $imgtype === 'gif'){
        $img = str_replace('data:image/gif;base64,', '', $img);
    };

    // REPLACE ALL SPACES IN THE IMAGE DATA WITH PLUS SYMBOL
    $img = str_replace(' ', '+', $img); 
    // CONVERT THE DATA FROM BASE64 ENCODING
    $img = base64_decode($img); 

    // SAVE THE FILE WITH NAME SYNTAX OF:  /images/clientlogos/[ACCOUNT ID]_[FILE NAME]
    file_put_contents('/var/www/html/ecom1/site/test/screen/'.$fname, $img);

    echo "Image has been saved successfully!<p>";
}
?>

Here is Full html code in pastebin

2条回答
2楼-- · 2019-08-24 10:40

You should try to change the ownership of the file/directory to www-data.

To change it only for the file use:

chown www-data:www-data YOURFILE

To change all files in a directory use the following command instead:

chown -R www-data:www-data YOURDIRECTORY/

This worked for me many times.

查看更多
Viruses.
3楼-- · 2019-08-24 10:51

Try updating your apache version to 2.4.

It did the trick for this fellow: https://stackoverflow.com/a/33033946/11722161

查看更多
登录 后发表回答