dpqmu84646 2019-07-17 09:44
浏览 113

无法在服务器中打开上传的文件

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条回答 默认 最新

  • drgm51600 2019-07-17 09:57
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建