求教:使用PHP的函数fpassthru()显示图片详解 新学PHP,可能没问到点子上,大概意思是这样的:wamp环境,zend框架+smarty模板1.<img src="controller/action/参数1/参数值" /> 然后在action里边用fpassthru传递一个图片来显示 是否可行? 怎么写?2. 不用img标签 用fpassthru 可以传递图片到前台么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 本帖最后由 PhpNewnew 于 2012-03-02 11:55:12 编辑 可以说是为了防盗链吧 次一点说,就是想隐藏真实地址......有实例,只是我照着写 显示不出来 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'); } } ...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的} apache 下防盗链可以用rewrite_module,用这些输出文件头的方法来处理只会让服务器负担加重.http://sealbird.iteye.com/blog/976891 请教下, 我这个代码哪里有错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"因其本身有错无法显示 注释掉 header("Content-Length: " . filesize($imgpath));header("Content-Type: image/jpg");后,只能是图片数据流 注释掉以后还不行我地址栏输入 http://localhost/article/readimg 理论上可以显示这个图片么? 还是说我用错了? 那你把 header("Content-Type: image/jpg"); 也注释掉 NetBeans的FTP上传太慢了. 关于phpinfo()中Configuration File Path的问题 flash动态相册如何嵌入到论坛中? DIV 竖排 不喜欢新版的CSDN·又搞一堆的弱智游戏! 在php.ini配置问题?在线等! session多的话会不会容易丢失 谁用过smarty? 二维数组排序的问题 求一个网址截取的正则替换表达式 APMServ5.2.6 做服务器环境如何?安全吗? Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given i
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');
}
}
//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的
}
请教下, 我这个代码哪里有错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"因其本身有错无法显示
后,只能是图片数据流
注释掉以后还不行我地址栏输入 http://localhost/article/readimg 理论上可以显示这个图片么? 还是说我用错了?