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

I set 777 permission to img folder but I get this error:

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

Why ?

xRobot
  • 25,579
  • 69
  • 184
  • 304

1 Answers1

2

The error says it all:

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

The error usually appears when your PHP script doesn't have sufficient permissions to create a file.

Here, you're using an absolute URL while saving the image:

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

If you want to use an absolute URL, you'll have to include the full path, like so:

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

Alternatively, if the images are in the same directory as the script, you can just use relative paths:

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

According to @OrangePill below:

It'd be better to use $_SERVER["DOCUMENT_ROOT"] in your scripts for better maintainability.

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

Hope this helps!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Alternative to absolute url would be to use `$_SERVER["DOCUMENT_ROOT"]."/phpthumb/img/new_thumb.jpg"`, this will keep it working between local and live. The last instance is going to try to save it relative to where the request started, not nessessarily where this code is being called if you want it relative to where it is called it should be `__DIR__."/my_new_image.jpg"` – Orangepill Jul 16 '13 at 18:22