0

I've got a function set up that decrypt AES encrypted images into its original files. For making a long story short, you would call it like this: DecryptFile($pathtofile)

This would create a file on folder tmp/ with the decrypted image and will return the path, for beeing able to insert the image via <img src="path">. I just need to show it on the current execution of the script, and delete it as soon as posible.

What I tried to do is unlink("path") for deleting the file at the end of the script, but if I do this there isn't enough time for the browser to load up the image, and anything will show up.

I checked out if I could manage with the tmpfile() function, but it seems that is suited for temporary download handling, as I can't think about a way of showing any image with <img> using this function.

Any ideas out there guys?

1 Answers1

2

One possible solution would be to store the image data directly in the tag with a data URI. But then if someone were to copy down the source code they would essentially have the image, likewise if something was caching your page content... The again I suppose thats no different that "Save image as" or doing a full page download. But it would save you from having to mess with copy/mv/symlink/unlink.

<?php

$decrypted = DecryptFile($pathtofile);
$data = base64_encode(file_get_contents($decrypted));
$info = getimagesize($decrypted);
$image = sprintf('data:%s;base64,%s', $info['mime'], $data);
?>

<img src="<?php echo $image; ?>" />
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • That's really useful! There is no problem with the user beeing able to download it, or having it on the cache, as long as decrypted data is stored locally. The problem is with server-side security, I need decrypted files to stay there as short as posible. – Cornezuelo del Centeno Mar 13 '15 at 12:26
  • By the way, `mime_content_type` is deprecated. I tried to use `finfo` instead with `$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $decryptmp)` but it also throws an error about undefined function >. – Cornezuelo del Centeno Mar 13 '15 at 12:27
  • Ahh id dint even look. You can use `getimagesize()` if you have GD installed. You can make it return an array with the image type in it. If you dont have that either you will need some way to conclude the mime type. Updated my answer to that effect. – prodigitalson Mar 13 '15 at 13:00
  • I managed to sort it out, I wouldn't be able to do it without you pointing me into the right direction, thank you! What I did is store all MIME types and the extension asociated in an array, and then with `array_search` and the extension of the file, I can fetch the MIME type for the file :) – Cornezuelo del Centeno Mar 13 '15 at 13:07