0

我一直在研究一个小的 PHP 脚本,它可以tmp从 base64 字符串动态创建和保存图像在一个文件夹(已经存在!)中。这是我的save.php文件。

// get base-64 string from form
$filteredData = substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);

// decode string
$unencodedData = base64_decode($filteredData);

// create unique filename
$a = uniqid();

// name, location and file extension
$compfile = '/tmp/' . $a . '.png';

// save image
file_put_contents($compfile, $unencodedData);

// print image
echo '<img src="' . $compfile . '" />';

奇怪的是:它既不会在页面上创建也不会渲染图像,因为/tmp/在我的$compfile变量中。当我删除它时,一切都像魅力一样工作,并且图像正在同一个文件夹中创建。

不幸的是,我真的希望在/tmp/文件夹中创建图像。之前$compfile是一个随机生成的文件名,取而代之的是,/tmp/img.png我能够保存以通过名称创建图像img.png并将其保存到tmp.

我在这里想念什么?

(感谢您的时间。)

4

1 回答 1

2

Since I guess this was the solution I'll post it as answer.

I think you want "tmp/" . $a . ".png" instead of "/tmp/" . $a . ".png". It's good practice to just always use absolute paths, so: __DIR__ . "/tmp/" . $a . ".png". This takes away any confusion.

于 2014-04-07T21:51:52.227 回答