Connecting Tech Pros Worldwide Help | Site Map

is_writable() is lying to me

  #1  
Old November 9th, 2005, 05:25 PM
Peter
Guest
 
Posts: n/a
Very perplexing. This function creates a png image of a random string (this
is to filter out blogging spam). It should be very straight forward.


function createImage( $text=0 )
{
$img_dir = '/tmp';

(snip!)

$filename = "$img_dir/" . randomString(12) . '.png';

if ( ! is_writable($filename) )
echo ("Can't write to $filename<br>");

ImagePNG( $image, $filename );
ImageDestroy( $image );

return array(
'filename' => $filename,
'passkey' => $text
);
}


When I call this function, it prints on the webpage, e.g.

Can't write to /tmp/TsP1gTUNwAOyQ.png

However, that file clearly exists:

$ ll /tmp/TsP1gTUNwAOyQ.png
-rw-r--r-- www-data www-data 200 2005-11-09 /tmp/TsP1gTUNwAOyQ.png

How can is_writable() be wrong?



In related wierdness, I use this function like so:

$retval = createImage();
$filename = $retval['filename'];
$passkey = $retval['passkey'];
echo "<img src=\"$filename\" />";

But the image doesn't show up in the browser. When I look at the page
source, the statement is there and looks correct:

<img src="/tmp/c2GfviA4r4b47.png" />

and I can even view the image with xv and gimp, however, the image is simply
not on the webpage.

I strongly suspect that these two oddities are related, but I'm running out
of ideas. Anyone have any suggestions?

Thanks,
Pete
  #2  
Old November 9th, 2005, 05:55 PM
Justin Koivisto
Guest
 
Posts: n/a

re: is_writable() is lying to me


Peter wrote:[color=blue]
> Very perplexing. This function creates a png image of a random string (this
> is to filter out blogging spam). It should be very straight forward.
>
>
> function createImage( $text=0 )
> {
> $img_dir = '/tmp';
>
> (snip!)
>
> $filename = "$img_dir/" . randomString(12) . '.png';
>
> if ( ! is_writable($filename) )
> echo ("Can't write to $filename<br>");
>
> ImagePNG( $image, $filename );
> ImageDestroy( $image );
>
> return array(
> 'filename' => $filename,
> 'passkey' => $text
> );
> }
>
>
> When I call this function, it prints on the webpage, e.g.
>
> Can't write to /tmp/TsP1gTUNwAOyQ.png
>
> However, that file clearly exists:
>
> $ ll /tmp/TsP1gTUNwAOyQ.png
> -rw-r--r-- www-data www-data 200 2005-11-09 /tmp/TsP1gTUNwAOyQ.png
>
> How can is_writable() be wrong?
>
>
>
> In related wierdness, I use this function like so:
>
> $retval = createImage();
> $filename = $retval['filename'];
> $passkey = $retval['passkey'];
> echo "<img src=\"$filename\" />";
>
> But the image doesn't show up in the browser. When I look at the page
> source, the statement is there and looks correct:
>
> <img src="/tmp/c2GfviA4r4b47.png" />
>
> and I can even view the image with xv and gimp, however, the image is simply
> not on the webpage.
>
> I strongly suspect that these two oddities are related, but I'm running out
> of ideas. Anyone have any suggestions?[/color]

The file isn't writable because it doesn't exist... you need to check
the directory...

<?php
if( (file_exists($filename)
&& is_writable($filename))
||
(file_exists(dirname($filename))
&& is_writable(dirname($filename)))
){
// can create or overwrite file
}else{
// cannot create or overwrite file
// directory may not exist, directory may
// now allow write permission, or the file
// exists and does not allow write permission
}
?>

--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
  #3  
Old November 9th, 2005, 06:35 PM
Peter Jay Salzman
Guest
 
Posts: n/a

re: is_writable() is lying to me


Justin Koivisto <justin@koivi.com> wrote:[color=blue]
>
> <?php
> if( (file_exists($filename)
> && is_writable($filename))
> ||
> (file_exists(dirname($filename))
> && is_writable(dirname($filename)))
> ){
> // can create or overwrite file
> }else{
> // cannot create or overwrite file
> // directory may not exist, directory may
> // now allow write permission, or the file
> // exists and does not allow write permission
> }
> ?>[/color]

Doh! How embarrassing! But thanks! :)

Pete
  #4  
Old November 9th, 2005, 08:45 PM
Chung Leong
Guest
 
Posts: n/a

re: is_writable() is lying to me


Should consider using tempnam() instead.

  #5  
Old November 9th, 2005, 10:05 PM
Peter Jay Salzman
Guest
 
Posts: n/a

re: is_writable() is lying to me


Chung Leong <chernyshevsky@hotmail.com> wrote:[color=blue]
> Should consider using tempnam() instead.
>[/color]

Damn. This is why I *love* PHP. Great suggestion! Thank you!

I got my script working too. :)

Pete
Closed Thread