新学PHP,可能没问到点子上,大概意思是这样的:wamp环境,zend框架+smarty模板1.<img src="controller/action/参数1/参数值" /> 然后在action里边用fpassthru传递一个图片来显示   是否可行? 怎么写?2. 不用img标签 用fpassthru 可以传递图片到前台么?

解决方案 »

  1.   

    本帖最后由 PhpNewnew 于 2012-03-02 11:55:12 编辑
      

  2.   

    可以说是为了防盗链吧  次一点说,就是想隐藏真实地址......有实例,只是我照着写 显示不出来
     public function Read ($hashCode){
        
    $filePath = $this->_prefixPath.$this->_hashFilePath($hashCode); if (is_readable($filePath)){
            
    if (class_exists('finfo', false)) {
                  
    $mime = @finfo_open(FILEINFO_MIME_TYPE);

                 if (!empty($mime)) {
                     $contentType = finfo_file($mime, $filePath);
                 }             unset($mime);
             }         if (empty($contentType)){
             $contentType = 'application/octet-stream';
             }

    header('Content-Type: '.$contentType);
    header('Accept-Ranges: bytes');
    header("Pragma: cache"); 
    //header('Last-Modified: '.date('r',time()));
    header("Expires: " . date ("r", time()+31536000));
    header("Cache-Control: max-age=31536000");

    try{
    $fp = fopen($filePath, "rb" );
    fpassthru($fp);
    fclose($fp);
    }catch(Zend_Exception $error){
    throw new ZendEx_File_Hash_Exception("【图片文件】$error->getMessage()",'FIM0001');
    } }else{
    throw new ZendEx_File_Hash_Exception('【图片文件】无法找到指定的图片','FIM0002');
    }

       
        
        }
      

  3.   

    ...Action{
    //1可以直接输出图片内容例如用你说的fpassthru:// 以二进制格式打开文件
    $name = './img/ok.png'
    $fp = fopen($name, 'rb');// 发送合适的报头
    header("Content-Type: image/png");
    header("Content-Length: " . filesize($name));// 发送图片并终止脚本
    fpassthru($fp);
    exit;//2或者设置header头,直接获取图片内容输出。设置header头可以用php的,也可以用zendframework的
    }
      

  4.   

    apache 下防盗链可以用rewrite_module,用这些输出文件头的方法来处理只会让服务器负担加重.http://sealbird.iteye.com/blog/976891
      

  5.   


    请教下, 我这个代码哪里有错public function readimgAction()
    { $imgpath = "/ulimg/56fa51/93ee02f7ce.jpg";//真实存在的图片,地址无误
    $fp = fopen($imgpath, 'rb');

    header("Content-Type: image/jpg");
    header("Content-Length: " . filesize($imgpath));

    fpassthru($fp);
    fclose($fp);
    exit;

    }显示结果是 图像"http://localhost/article/readimg"因其本身有错无法显示
      

  6.   

    注释掉 header("Content-Length: " . filesize($imgpath));header("Content-Type: image/jpg");
    后,只能是图片数据流
      

  7.   


    注释掉以后还不行我地址栏输入 http://localhost/article/readimg  理论上可以显示这个图片么? 还是说我用错了?
      

  8.   

    那你把  header("Content-Type: image/jpg"); 也注释掉