简体   繁体   English

用php转换为base64

[英]Conversion to base64 with php

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? 我正在将图像上传转换为base64,但是我也试图不必将图像存储在服务器上,不幸的是,此代码将文件存储在服务器上,有一种方法可以将其编码后的文件删除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).'); ';
        }
    }
}

Use http://us3.php.net/manual/en/function.unlink.php 使用http://us3.php.net/manual/en/function.unlink.php

so 所以

....
$css = 'url(data:image/png;base64,'.str_replace("\n", "", $base64).'); ';
unlink($file);
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 祝好运

You can use $_FILES['t1']['tmp_name'] like a normal file. 您可以像普通文件一样使用$ _FILES ['t1'] ['tmp_name']。

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. 如果那不好,您可以使用@cryptic的解决方案,只需取消链接()文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM