2

我正在将图像上传转换为base64,但我也试图不必将图像存储在我的服务器上不幸的是,这段代码将文件存储在我的服务器上是否有办法让它在编码后删除文件到基数64?

这是我的代码..

    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).'); ';
        }
    }
}
4

3 回答 3

4

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

所以

....
$css = 'url(data:image/png;base64,'.str_replace("\n", "", $base64).'); ';
unlink($file);
于 2012-11-30T15:15:49.630 回答
0
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);
    }

如果您不从临时文件移动文件将删除自动或者如果移动它您可以取消链接

祝你好运

于 2012-11-30T15:17:53.033 回答
0

您可以像使用普通文件一样使用 $_FILES['t1']['tmp_name'] 。

所以你应该能够做到这一点:

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).'); ';
    }
}

如果这不好,您可以使用@cryptic 的解决方案并取消链接()文件。

于 2012-11-30T15:17:54.827 回答