2012-04-10 56 views
0

我正在使用我的游戏网站,但为了让Facebook API正常工作,我必须制作一个新的脚本,可以根据游戏的ID。这是这个脚本试图做的,但我得到这个错误。从数据库中抓取图片,然后在url中写入文件

Warning: readfile(localhost/thumbs/6c9e40d35e8cf07e.png) [function.readfile]: failed to open stream: No such file or directory in B:\home\home\thumb.php on line 22 

我想知道你们中的任何一位能否帮助我。

<?php 
require_once "functions.php"; 
if(!isset($_REQUEST['t'])){ 
die("No ID Defined"); 
} 
$t = $_REQUEST['t']; 
$t = intval($t); 
connect(); 
$fetch = fetchdata("select * from `games` where id = $t"); 
$thumb = $fetch{'thumb'}; 
$file = "/thumbs/$thumb"; 
$filename = basename($file); 
$file_extension = strtolower(substr(strrchr($filename,"."),1)); 
switch($file_extension) { 
    case "gif": $ctype="image/gif"; break; 
    case "png": $ctype="image/png"; break; 
    case "jpeg": 
    case "jpg": $ctype="image/jpg"; break; 
    default: 
} 
header($ctype); 
readfile($_SERVER['HTTP_HOST'].'/thumbs/'.$thumb); 
?> 
+0

首先尝试'readfile('http://'。$ _SERVER ['HTTP_HOST']。'/ thumbs /'。拇指);'看看是否修复它。您需要使用图像的完整路径,或者在localhost前面指定“http://”。 – drew010 2012-04-10 22:04:24

回答

2

你不需要为PHP readfilehttphttps使用http将使slower加载这些图像的工作...其实....

readfile('/thumbs/'.$thumb); //or 
readfile(PATH_TO_LOCAL_DIR. '/thumbs/'.$thumb); 

应该正常工作。

这是在10日线

require_once "functions.php"; 
if (! isset ($_REQUEST ['t'])) { 
    die ("No ID Defined"); 
} 
connect(); 
$fetch = fetchdata (sprintf("select * from `games` where id = '%d'",$_REQUEST ['t'])); 
if(!is_file("thumbs/" . $fetch ['thumb'])) 
    die("Thum Does not exist"); 
header ("Content-type: ", image_type_to_mime_type (exif_imagetype ($fetch ['thumb']))); 
readfile ("thumbs/" . $fetch ['thumb']); 
+0

谢谢。很棒。 – Alice 2012-04-10 22:10:38

+0

这是因为你的内容类型是错误的..刚刚更新了你的脚本 – Baba 2012-04-10 22:17:17

+0

yerp的10行版本,我几乎立即意识到写这个评论后。 – Alice 2012-04-10 22:24:36

0

您需要实际设置的mime-type头这样的脚本的版本:

header(sprintf("Content-Type: %s", $ctype)); 

然后看到@Baba's answer

相关问题