2013-07-16 21 views
-1
require_once '../ThumbLib.inc.php'; 
$thumb = PhpThumbFactory::create('test.jpg'); 
$thumb->resize(100, 100)->save('/img/new_thumb.jpg'); 
$thumb->show(); 

我設置777權限IMG文件夾,但我得到這個錯誤:如何保存與phpthumb的拇指?

Fatal error: Uncaught exception 'RuntimeException' with message 'File not writeable: /img/new_thumb.jpg' in /home/xjohn/www.mysite.com/phpthumb/GdThumb.inc.php:662 Stack trace: #0 /home/xjohn/www.mysite.com/phpthumb/examples/resize_basic.php(31): GdThumb->save('/img/new_th...') #1 {main} thrown in /home/xjohn/www.mysite.com/phpthumb/GdThumb.inc.php on line 662 

爲什麼?

+0

我認爲這是因爲路徑錯了 - 也許它需要是'/ home/xjohn/www.mysite.com/img/new_thumb.jpg'?儘管你可能想添加一些能夠以編程方式實現的東西。 – andrewsi

+0

我嘗試了這個路徑,但我得到了相同的錯誤 – xRobot

+0

img文件夾的完整路徑是什麼?這是你應該使用的那個。 – andrewsi

回答

2

錯誤說明了一切:

Fatal error: Uncaught exception 'RuntimeException' with message 'File not writeable: 

的錯誤通常當你的PHP腳本沒有足夠的權限來創建一個文件中出現。

在這裏,您使用的是絕對URL,同時保存圖像:

$thumb->resize(100, 100)->save('/img/new_thumb.jpg'); 

如果要使用絕對URL,你必須包含完整的路徑,像這樣:

$new_image = '/home/xjohn/www.mysite.com/phpthumb/img/new_thumb.jpg/'; 
$thumb->resize(100, 100)->save($new_image); 

另外,如果圖像是在同一目錄下的腳本,你可以使用相對路徑:

$thumb->resize(100, 100)->save(__DIR__.'/my_new_image.jpg'); 

根據以下@OrangePill:

最好在腳本中使用$_SERVER["DOCUMENT_ROOT"]以提高可維護性。

$_SERVER["DOCUMENT_ROOT"]."/phpthumb/img/new_thumb.jpg" 

希望這會有所幫助!

+0

絕對網址的替代方法要使用'$ _SERVER [「DOCUMENT_ROOT」]。「/ phpthumb/img/new_thumb.jpg」,這將保持它在本地和活動之間工作。最後一個實例將嘗試保存它相對於請求開始的位置,如果你想讓它相對於它被調用的地方,那麼這個代碼被調用的地方並不是必須的,它應該是'__DIR __。'/ my_new_image.jpg「' – Orangepill

+0

@Orangepill:好主意。 –