473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
Nov 9 '05 #1
4 1680
Peter wrote:
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?


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 - ju****@koivi.com
http://koivi.com
Nov 9 '05 #2
Justin Koivisto <ju****@koivi.com> wrote:

<?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
}
?>


Doh! How embarrassing! But thanks! :)

Pete
Nov 9 '05 #3
Should consider using tempnam() instead.

Nov 9 '05 #4
Chung Leong <ch***********@hotmail.com> wrote:
Should consider using tempnam() instead.


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

I got my script working too. :)

Pete
Nov 9 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Johnson Lau | last post by:
I'm running php 4.2.3 on apache 2 with FreeBSD. I find that "is_writable" only regonise files with full path. However, many scripts from the web didn't do this. How could I fix it? Thanks.
14
by: Good Man | last post by:
Hi There A client of mine has some banner ads placed on the net. Instead of relying on the sites the ads are posted on tell us how many clicks they are getting, we wanted to track the number...
70
by: Prashanth Ellina | last post by:
Hi, I have a feeling that OOP can be done in C also. I have used a structure to hold member variables and function pointers. The structure is used as a class to create new 'objects'. But I hit a...
2
by: z f | last post by:
trying to achieve windows authentication on the fly (without the file configured to deny anonymous access in IIS) i returned a 401 status if the Request.ServerVariables("auth_user") = "" this...
102
by: Xah Lee | last post by:
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality...
4
by: ceh | last post by:
Hi, I'm using c# vs 2005 win xp pro sp2. I create a cmd prompt with pInvoke via CreateProcessWithLogonW Taskmgr shows that cmd instance running as that user. I type set in that cmd window and it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.