Reputation: 1411
I'm converting an image upload to base64, but I'm also trying to not have to store the image on my server unfortunately this code stores the file on my server is there a way to have it delete the file after it it encoded to base64?
Here is my code..
if(isset($_FILES['t1']['name'])){
$file = rand(0, 10000000).$_FILES['t1']['name'];
if (move_uploaded_file($_FILES['t1']['tmp_name'], $file)) {
if($fp = fopen($file,"rb", 0))
{
$picture = fread($fp,filesize($file));
fclose($fp);
// base64 encode the binary data, then break it
// into chunks according to RFC 2045 semantics
$base64 = base64_encode($picture);
$tag1 = '<img src="data:image/png;base64,'.$base64.'" alt="" />';
$css = 'url(data:image/png;base64,'.str_replace("\n", "", $base64).'); ';
}
}
}
Upvotes: 2
Views: 1568
Reputation: 4168
if(isset($_FILES['t1']['name'])){
$file = rand(0, 10000000).$_FILES['t1']['name'];
if (move_uploaded_file($_FILES['t1']['tmp_name'], $file)) {
$fileCon = file_get_contents($file) ;
// base64 encode the binary data, then break it
// into chunks according to RFC 2045 semantics
$base64 = base64_encode($fileCon);
$tag1 = '<img src="data:image/png;base64,'.$base64.'" alt="" />';
$css = 'url(data:image/png;base64,'.str_replace("\n", "", $base64).'); ';
unlink($file);
}
if you not moving file from temp file will remove auto or if moving it you can unlink
good luck
Upvotes: 0
Reputation: 1112
You can use $_FILES['t1']['tmp_name'] like a normal file.
So you should be able to do this:
if(isset($_FILES['t1']['name'])){
$file = $_FILES['t1']['tmp_name'];
if($fp = fopen($file,"rb", 0))
{
$picture = fread($fp,filesize($file));
fclose($fp);
// base64 encode the binary data, then break it
// into chunks according to RFC 2045 semantics
$base64 = base64_encode($picture);
$tag1 = '<img src="data:image/png;base64,'.$base64.'" alt="" />';
$css = 'url(data:image/png;base64,'.str_replace("\n", "", $base64).'); ';
}
}
If that's no good, you can use @cryptic's solution and just unlink() the file.
Upvotes: 0
Reputation: 15045
Use https://www.php.net/manual/en/function.unlink.php
so
....
$css = 'url(data:image/png;base64,'.str_replace("\n", "", $base64).'); ';
unlink($file);
Upvotes: 4