douweng7083 2014-12-03 06:44
浏览 35
已采纳

php base64_encode和glob无法运行

Have this function that encodes content of images file to base64.

function data_uri($file_to_get_contents, $mime) {  
$contents = file_get_contents( '../images/'. $file_to_get_contents );
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}

Actual location (url) of images file is this

$val_img = '../images/2014-12-03/13-1-b5780ffc85f5f29d5ce43d1f4e38003f.gif';

I need to access echo image using such url (without .gif, .jpg etc.).

$val_img = '../images/2014-12-03/13-1-b5780ffc85f5f29d5ce43d1f4e38003f';

Decided to use glob. And use code below.

$val_img = glob($val_img. '×.*');

Tried also

$val_img = glob( '×'. $val_img. '*' );

With this see empty array

echo '<pre>', print_r($val_img, true), '</pre> $val_img <br/>';

And with this

echo '<img src='. data_uri($val_img , "../images"). ' alt="Image" >';

See error like Warning: file_get_contents(../images/Array) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: No such file or directory

Error informs that in this $contents = file_get_contents( '../images/'. $file_to_get_contents ); line is error.

But seems something incorrect with this $val_img = glob($val_img. '×.*');

What would be correct code?

Here is my code that works

function data_uri($file_to_get_contents, $mime) {  
$contents = file_get_contents( '../images/'. $file_to_get_contents );
$base64 = base64_encode($contents); 
return ('data:' . $mime . ';base64,' . $base64);
}

$val_img = '../images/2014-12-03/13-1-b5780ffc85f5f29d5ce43d1f4e38003f';

$val_img = glob( $val_img. '*' );

echo '<img src='. data_uri($val_img[0] , "../images"). ' alt="Image" >';
  • 写回答

1条回答 默认 最新

  • douduoquan2824 2014-12-03 06:50
    关注

    The problem is that you are passing an array to your function data_uri.

    glob returns an array. That means that $val_img is an array, not a string. But you are treating it as a string when you pass it to data_uri as the parameter $file_to_get_contents. Specifically, you use it as a string in this line:

    $contents = file_get_contents( '../images/'. $file_to_get_contents );
    

    Easiest way to fix this: change your call to data_uri, like so:

    echo '<img src='. data_uri($val_img[0] , "../images"). ' alt="Image" >';
    

    Another option is to loop over the array, like this:

    foreach($val_img as $one_img) {
        echo '<img src='. data_uri($one_img , "../images"). ' alt="Image" >';
    }
    

    Edit: You also have a logic error in your code. There's no need for the × character (whatever that is); your file names don't contain one. Just do $val_img = glob($val_img. '.*');


    Side note: I have found that the best way to prevent these kinds of errors is to use a good IDE (I personally prefer PHPStorm) and add PHPDoc blocks to all your functions describing the parameter types. Then, if you try to pass an array when your function expects a string, you will get a warning from the IDE. Catching stuff like this at development time makes for much cleaner code and helps you avoid releasing errors into the wild.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程