tags:

views:

116

answers:

4

I am creating an image dynamically using php. The image is being created if I go in and make the file first. However, if I don't create the file manually then I get the following error.

Warning: imagegif() [function.imagegif]: Unable to open 'filename2.png' for writing in /www/vhosts/yourraceresults.com/htdocs/trial/TextToImage.inc.php on line 144

Example:

public function SaveTextAsPng($fontSize, $x, $y, $textContent, $fileName='image'){
 imagestring($this->image, $fontSize, $x, $y,  $textContent, $this->text_color);
 return imagepng($this->image, "/www/vhosts/yourraceresults.com/htdocs/admin/trial/images/".$fileName.".png");
}

$textToImage->SaveTextAsPng(10, 11,11, 'fakeinfo','filename2');

A: 

Do you have properly setted rights for output dir? I'm think your web server (and php with it) can't write to destination folder.

Alexey Sviridov
+1  A: 

It seems your web user (apache, www, ...) has not got write permissions in the folder you are trying to write to. Or, if the file already exists, it has not got write permissions for that file.

Edit: Changing the permissions

Supposing you are on a linux like system with an apache web-server, first you need to find out as which user your web-server is running. You need to get a command shell on the server (ssh) and then you can probably see the web-server user typing the following command:

$ ps aux | grep httpd

In the list you will see all running apache processes with the username in the first column.

Let´s say that the web-user is apache, you now need to give apache write permissions in that directory. You can do that by changing the group to apache and giving write permissions to that group or by changing the ownership of the directory to apache. Let´s say you want to change the group:

$ chgrp apache /www/vhosts/yourraceresults.com/htdocs/trial

Give the owner and the group users (apache) write permissions:

$ chmod 775 /www/vhosts/yourraceresults.com/htdocs/trial

Disclamer: this is a fast solution, but I don´t know much about the safety, comments are welcome!

jeroen
Okay, how do I change it, I am pretty weak in permissions.
Joe
Using FTP change the permissions to 755, or using SSH use chmod.
Henri Watson
A: 

It seems your script needs write permissions on that directory. An easy way to change these permissions is to login via FTP and change the permissions from your FTP client. Most (at least, of the few I've tried) FTP clients offer an option when you right-click a folder called "Permissions" or "File Attributes" (FileZilla) or something like that. It should show a dialog with different permissions checkboxes; make sure to give at least the Owner permission to write. If you are on a *nix host, you should be able to go to the directory and issue the command chmod 755 . to change the permissions.

EDIT: chmod 777 . will give you the most permissions, if 755 does not work.

NickAldwin
This probably won't work. In most sharing environments the unix user running the web server and the user providing the files are different. So you need to give full permissions, otherwise the web server user still doesn't have the right to write in the directory.
e-t172
+1  A: 

Either the output folder do not exist (in which case you have to create it), or you do not have write permissions to the output folder. To fix this:

  • If you're using a shell:

    chmod 777 /www/vhosts/yourraceresults.com/htdocs/trial

  • If you're using an FTP client:

    Right click on the "trial" folder, search for something called "chmod", "rights", "permissions" (perhaps in "properties"), then give it every permission or, alternatively, enter chmod value 777.

e-t172
I did the first one. Same error shows up.
Joe
/www/vhosts/yourraceresults.com/htdocs/trial is the directory where filename2.png is written to, right? If not, retry on the appropriate directory.
e-t172
I did it for the apporpriate directory as I changed the path to /www/vhosts/yourraceresults.com/htdocs/trial/images to not put anything at risk
Joe
Could it possibly be a chown issue? What should chown be set up as.
Joe
@Joe `ls -l` to show the owner and group
jimyi
From the code, it looks like the appropriate directory is /www/vhosts/yourraceresults.com/htdocs/admin/trial/images/ (you left out the admin dir)
jimyi
Thanks, it was a combination of two things. Got it figured out now.
Joe
I don´t know anything about the OP's hosting solution, but I would definitely not recommend changing the permissions to 777. That means writeable for **everybody**.
jeroen
Pretty much every hosting provider I've seen requires that for PHP scripts to be able to write to an FTP-uploaded folder. See my explanation above. From the point of view of the hosting provider there are two solutions to this problem: CGI/FastCGI or apache2-mpm-itk. The former has performance issues and the latter is often not considered mature enough to be used in production.
e-t172