File problems - open_basedir error


#1

Hi all. I am working on a PHP script that is supposed to create a new file and write content to it. I set up the directory, set the privileges, and then ran the script. Things worked fine when the file existed, but when the script had to write to a file that didn’t exist - and should have created a new one - I got three error messages:

“Warning: open_basedir restriction in effect. File is in wrong directory [directory path of the php file]”

“Warning: fopen(” [path to file I was trying to create", “w”) - Operation not permitted in [ directory path of the php file] on line 12"

“Warning: fwrite(): supplied argument is not a valid File-Handle resource in [ directory path of the php file]”.

The knowledge base docs said that Dreamhost’s security measures prevent scripts from writing to files outside the home directory, but my script was writing to a file a few directories into the home directory.

Is it possible to do this kind of operation on Dreamhost, or is there another way I can use?

Thanks!

  • Chris

#2

Hmm, have you tired calling realpath() on the filename first? Have to watch out for DH’s filesystem setup.
/home/username/domain is actually /home/.mountpoint/username/domain etc

$file = ‘/home/username/domain/filename’;
fopen(realpath($file), ‘w’);

:cool: Perl / MySQL / HTML+CSS


#3

Weird…realpath() returns a blank when I call it. Here’s my code:

$filetocreate = “$DOCUMENT_ROOT/directory/testfile.php”;
$rp = realpath($filetocreate);
$fp = fopen(realpath($filetocreate), ‘w’);

It’s at this point that it tells me the following: “Warning : fopen(”", “w”) - No such file or directory."

Am I calling it wrong?

Thanks!

  • Chris

#4

Well, helps to read the PHP documentation at http://www.php.net/, so I wrote my reply when really tired so didn’t go into detail…

realpath() will return undef if the file passed to it does not exist. Which happens to be the case since you are trying to create it. What I meant to say was to pass the path to the directory to realpath(), something like this:$dir_path = '/home/username/domain'; $filename = 'something.html'; $file = realpath($dir_path) . "/$filename";This way you are getting the correct path to the directory, then just appending the filename.

I don’t code in PHP so I am not sure it will work but others have reported success in preventing open_basedir errors using realpath().

:cool: Perl / MySQL / HTML+CSS


#5

It works!

Thanks Atropos!!

Have a great holiday :slight_smile:

  • Chris