I have been working on a little PHP script that dynamically creates & saves images in a folder tmp
(which already exists!) from a base64 string. This is my save.php
file.
// 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 . '" />';
Strangely enough: It will neither create nor render the image on the page, because of the /tmp/
in my $compfile
variable. When I remove it, everything works like a charm and the image is being created in the same folder.
Unfortunately, I really want the image to be created in the /tmp/
folder. Before $compfile
was a randomly generated filename, and instead was called /tmp/img.png
I was able to save to create an image by the name img.png
and save it to the tmp
.
What am I missing here?
(Thank you for your time.)
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.