2012-04-10 34 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

相關問題