使用file_put_contents上传期间损坏的文件

时间:2013-07-10 10:50:15

标签: php

我尝试使用file_put_contents在目录中上传图像,文件已上传,但已损坏,我无法打开它。这是我的代码:

define('UploadDir','../Dir/images');
$path = "image.png";
$data="..."; //image base64 string
$file = UploadDir ."/". $path;
$success = file_put_contents($file, $data);
echo $success ? $file : 'Unable to save the file.';

2 个答案:

答案 0 :(得分:4)

  

$数据= “...”; // image base64 string

假设$data包含注释所说的内容,如果在图像中放置base64字符串,它显然不是有效图像 - 它将是包含base64编码字符串的文件。

有效(或有机会有效)在将字符串写入文件之前解码字符串:

$data="..."; //image base64 string
$file = UploadDir ."/". $path;
$success = file_put_contents($file, base64_decode($data));

答案 1 :(得分:0)

目录是../Dir/images chmoded to 777吗?有时,如果没有足够的权限,php将不允许您访问目录。 尝试查看它是否具有足够的权限,如果没有,则将其设置为777并尝试使用。

相关问题