duanjingwei7239
2015-01-31 10:08
浏览 63
已采纳

php - 用于动态脚本的file_get_contents()

I have a script that outputs an image.

Works fine

include('../myfolder/myImageScript.php'); // outputs image on page

Fails

echo file_get_contents('../myfolder/myImageScript.php'); // nothing displayed

I think this fails because in php a script, in my case myImageScript.php, isn't executed when called via the file_get_contents() function, but is when called using a include() the script is executed.

I am struggling to get a zip function to work due to the empty output of file_get_contents().

the file i'm trying to call via file_get_contents() is:

myImageScript.php

$imgstr = "data:image/jpeg;base64,/9j/........... rest of string";
if (!preg_match('/data:([^;]*);base64,(.*)/', $imgstr, $matches)) {
die("error");
}

// Decode the data
$content = base64_decode($matches[2]);

// Output the correct HTTP headers
header('Content-Type: '.$matches[1]);
//header("Content-Type: image/jpeg"); // tried this made no difference

// Output the actual image data
echo $content;

Any help would be greatly appreciated.

图片转代码服务由CSDN问答提供 功能建议

我有一个输出图像的脚本。

工作正常

 <代码>包括( '../ MyFolder文件/ myImageScript.php');  //在页面输出图像
   
 
 

失败

  echo file_get_contents('../ myfolder / myImageScript.php  “);  //什么都没有显示
   
 
 

我认为这是失败的,因为在 php 一个脚本,在我的例子中 myImageScript.php file_get_contents()函数调用时不执行,但是当使用 include()调用时,脚本被执行。 \ n

由于 file_get_contents()的空输出,我很难让 zip 函数工作。

我试图通过 file_get_contents()调用的文件是:

myImageScript.php

  $ imgstr  =“data:image / jpeg; base64,/ 9j / ...........其余字符串”; 
if(!preg_match('/ data:([^;] *); base64,(  。*)/',$ imgstr,$ matches)){
die(“error”); 
} 
 
 //解码数据
 $ content = base64_decode($ matches [2]); 
  
 //输出正确的HTTP标头
header('Content-Type:'。$ matches [1]); 
 // header(“Content-Type:image / jpeg”);  //试过这个没有区别
 
 //输出实际的图像数据
echo $ content; 
   
 
 

任何帮助都将不胜感激。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufu2496 2015-01-31 11:20
    已采纳

    Something like this should work, BUT you need to enable ZipArchive http://php.net/manual/en/class.ziparchive.php (should not be a problem)

        <?php
        $imgstr = "data:image/gif;base64,R0lGODlhyAAiALMAAFONvX2pzbPN4p6/2tTi7mibxYiw0d/q86nG3r7U5l2UwZO31unx98nb6nOiyf///yH5BAUUAA8ALAAAAADIACIAAAT/8MlJq7046827/2AojmRpnmiqriwGvG/Qjklg28es73wHxz0P4gcgBI9IHVGWzAx/xqZ0KlpSLU9Y9MrtVqzeBwFBJjPCaC44zW4HD4TzZI0h2OUjON7EsMd1fXcrfnsfgYUSeoYLPwoLZ3QTDAgORAoGWxQHNzYSBAY/BQ0XNZw5mgMBRACOpxSpnLE3qKqWC64hk5WNmBebnA8MjC8KFAygMAUCErA2CZoKq6wHkQ8C0dIxhQRED8OrC1hEmQ+12QADFebnABTr0ukh1+wB20QMu0ASCdn16wgTDmCTNlDfhG/sFODi9iMLvAoOi6hj92LZhHfZ3FEEYNEDwnMK/ykwhDEATAN2C/5d3PiDiYSIrALkg6EAz0hiFDNFJKeqgIEyM1nhwShNo0+glhBhgKlA5qqaE25KY1KAYkGAYlYVSEAgQdU1DFbFe3DgKwysWcHZ+QjAAIWdFQaMgkjk2b4ySLtNkCvuh90NYYmMLUsErVRiC8o8OLmkAYF5hZkRKYCHgVmDAiJJLeZpVUdrq/DA7XB5rAV+gkn/MJ0hc8sKm6OuclDoo8tgBQFgffd335p3cykEjSK1gIXLEl+Oq9OgTIKZtymg/hHuAoHmZJ6/5gDcwvDOyysEDS7B9VkJoSsEhuEyN6KSPyxKrf4qsnIoFQ4syL0qum8i9AW0H/9F/l3gngXwwSAfEQ5csIoFUmH1oAVrTEhXQ+Cdd6GGD4z230b+TQdDgB8S6INeG76AlVSsoYeibBg+cOAX2z1g4Vv2sYggER15uFliZFwWnUAAQmhLGUKe+MMFEa1oH40/FMKYht1RMKVB7+AiwTvEMehdeB2CicwLlAlXI1m5kSjBmACUOQF0HWRpAZcZqngBbxWwqZtkZz4QlEsJvkDiejDIcRh5h4kG5pPBrEHkDw06GKMEhAJwGxx+uBIoAIOmlxaH9TWCh4h2fgqDAWcc019AqwTHwDtu1UmMRQnkdpuHRU6gZ3uWOOaHILmuScc6LlFDhKuwwgiqsjQNgAD/UWgFZaKuq/w0AHIAuHIYReR5+A4C12HkEksSfRvuqiuxR4GebSFw7SraMqoRuXvK2t+Z+JDb22bsxDqBh+YRVCO5RgT81JnEGiNtNvvKKwl/IzJKql8ORadqQuSZis7CANCWYnIScOyAiJHayFIUIpM8r0GUstsrbA4HhC2nJi9LwDuihKkuhEQpgAAiEQpjyc99aWHMppz2gSLBlCL9iFQrW2pdz0TDPCkGCRgQjU9GVPpZQAkgIICWHfQhABkNkM1svQxg9wcJfWSn1AlxI5DA3COYjbbaLJBKzhQRuiF4Cn8nMiMXgQ+uOAkBFDDA2wxABkPJiMe8+OUaECVNLMZUJI755xtoHmwXnoNuugUQp4bGLzf0dvrriy2wsAMD4A377YJjSgDfD0QAADs=";
    if (!preg_match('/data:([^;]*);base64,(.*)/', $imgstr, $matches)) {
    die("error");
    
    $content = base64_decode($matches[2]);
    $zip = new ZipArchive;
    $filename  = tempnam("/tmp", "testmeZip");
    $res = $zip->open($filename, ZipArchive::CREATE);
    if ($res === TRUE) {
        $zip->addFromString('test.gif', $content);//you can use $matches to figure out extension
        $zip->close();
        echo 'ok';
    } else {
        echo 'failed';
    }
    
    
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"test.zip\"");
    header("Content-Transfer-Encoding: binary");
    // make sure the file size isn't cached
    clearstatcache();
    header("Content-Length: ".filesize($filename));
    // output the file
    readfile($filename);
    
    点赞 打赏 评论

相关推荐 更多相似问题