Can't write to directory

Alles, was den Apache betrifft, kann hier besprochen werden.

Can't write to directory

Postby timstring » 13. February 2013 23:30

Here's a snip of code I have in a PHP script:

Code: Select all
passthru("whoami"); 
$tims_path = "localhost/DispatchReports/DispatchReportsFolder";


if ( !file_exists($tims_path) ) {
  mkdir ($tims_path, 0777);
 }

 file_put_contents ($tims_path.'/test.txt', 'Hello File');
if (is_dir($tims_path)) {
  die( "timstring, you cannot use fopen on a directory path." );
}

 //if the path is a directory and you want to put a new file there, you'll need to first check if it is writable:
if (!is_writable($tims_path)) {
 die("timstring, " . $tims_path . " is not writable so you will not be able to write anything to it");
}


         $FileName = $tims_path . $System . ' ' . $EndMO . '.csv';
            $fh = fopen($FileName, "w");



Mkdir will not create the assigned folder. If I create the folder in Finder, the if(is_writable()) returns a negative. PHP can read and write to the parent folder. I have tried many many many combinations of permissions, all to no avail. Any ideas?
"If builders built buildings the way that programmers write programs, the first woodpecker to come along would destroy civilization."
User avatar
timstring
 
Posts: 22
Joined: 17. September 2010 02:16

Re: Can't write to directory

Postby Nobbie » 14. February 2013 12:37

If you read the user comments on is_writable() on www.php.net, you will find that this is a very buggy function which only checks for some permissions, but does not provide a full check (via Kernel functions), if a file or directly really can or cannot be accessed.

You should simply try to fopen() (with mode 'w' or 'c') a file and if this returns FALSE, then you really cannot create a file. If it returns a valid handle instead, everything is fine.
Nobbie
 
Posts: 6584
Joined: 09. March 2008 13:04

Re: Can't write to directory

Postby timstring » 16. February 2013 22:42

Doesn't matter if I have' is_writable()' or not. is_writable() returns an error, and when I comment the statement out, the script simply does nothing with it comes to 'fopen()'.

Let me take a step backward. This will work:

Code: Select all
$FileName = $System . ' ' . $EndMO . '.csv';
            $fh = fopen($FileName, "w");


However, it won't work if I stick a path to '$FileName', and I have tried several that point to the same directory I'm trying to write to.

If I run a 'whoami' request in my script, it returns the owner as '_www'. So, I have '_www' as the directory owner and the group '_www' as having write permissions. I also have the permissions set up so that I can write directly to the directory. This is odd, because in my httpd.conf file, I have 'tim' as the user and 'admin' as the group. Those, too, have write permissions to the directory. The strange thing is that I can drop files in the folder from the Finder. Exsmel has no trouble manipulating files, either.

What is going on? I am at my wit's end. Skip the jokes ;).
"If builders built buildings the way that programmers write programs, the first woodpecker to come along would destroy civilization."
User avatar
timstring
 
Posts: 22
Joined: 17. September 2010 02:16

Re: Can't write to directory

Postby Nobbie » 16. February 2013 22:55

timstring wrote:However, it won't work if I stick a path to '$FileName', and I have tried several that point to the same directory I'm trying to write to.


Sorry, i dont understand, what you mean by that. What does it mean "stick a path to '$Filename'"?
Nobbie
 
Posts: 6584
Joined: 09. March 2008 13:04

Re: Can't write to directory

Postby timstring » 17. February 2013 01:11

a Path? localhost/dispatchreports/dispatchreportsfolder/, and several other tries. /dispatchreportsfolder/ is the directory I'm aiming for. As I say, I can create a file in /dispatchreports/, because it is the root of my site, and I don't have to use it in the fopen() statement.
"If builders built buildings the way that programmers write programs, the first woodpecker to come along would destroy civilization."
User avatar
timstring
 
Posts: 22
Joined: 17. September 2010 02:16

Re: Can't write to directory

Postby timstring » 17. February 2013 01:40

Eureka, I've got it! I used 'chdir(dispatchreportsfolder)' and fopen() wrote to that folder.
"If builders built buildings the way that programmers write programs, the first woodpecker to come along would destroy civilization."
User avatar
timstring
 
Posts: 22
Joined: 17. September 2010 02:16


Return to Apache

Who is online

Users browsing this forum: No registered users and 4 guests