Connecting Tech Pros Worldwide Help | Site Map

is_writable() is lying to me

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 9th, 2005, 04:25 PM
Peter
Guest
 
Posts: n/a
Default is_writable() is lying to me

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, 04:55 PM
Justin Koivisto
Guest
 
Posts: n/a
Default 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, 05:35 PM
Peter Jay Salzman
Guest
 
Posts: n/a
Default 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, 07:45 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: is_writable() is lying to me

Should consider using tempnam() instead.

  #5  
Old November 9th, 2005, 09:05 PM
Peter Jay Salzman
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.