0

I'm having a problem with writing a new image file from image-data (dataurl). Images lower than 1 mb is no problem with file_put_contents. But if the file is bigger than 1 mb. The function does not work.

Procedure

  1. A DataUrl was sent to print.php via ajax with 1436190 in length.
  2. print.php is get the var $_POST['image'] and $_POST['filename']
  3. print.php save file to hdd with file_put_contents($photo, $data);

Here's the print.php script:

<?php
header('Content-Type: image/jpeg');
ini_set('memory_limit', '128M');
$data=$_POST['image'];
if(!$_POST['filename']){
    exit("error");
}
list($dir,$folder,$file)=explode("/",$_POST['filename']);
$ext=strtolower(pathinfo($_POST['filename'], PATHINFO_EXTENSION));

$file=basename($file,".".$ext);
$data = base64_decode($data);

$imgRes = imagecreatefromstring($data);
if($imgRes !== false && imagejpeg($imgRes, $file,"100") === true)
    if(!is_dir($dir.'/'.$folder.'/new/')){
        mkdir($dir.'/'.$folder.'/new/',0777);
    }
    $photo=$dir.'/'.$folder.'/new/'.$file.'-'.time().".".$ext;//full url

    $saved=file_put_contents($photo, $data);
    if($saved){     
        echo "ok";
    }else{
        echo "error";
    }
imagedestroy($imgRes);
?>

I've tried to figure this out. But it's not working. Nothing shows in console. Or is there any smarter way to achieve this than file_put_contents?

4

1 回答 1

0

首先: “他的函数返回写入文件的字节数,失败时返回 FALSE。” 使用 -> if($saved !== FALSE) OK else error

第二: file_put_contents — 将字符串写入文件。然后你需要使用 -> fwrite() - 二进制安全文件写入。

http://php.net/manual/en/function.file-put-contents.php

http://php.net/manual/en/function.fwrite.php

祝你好运!

于 2016-11-10T12:11:12.567 回答