Why is is_readable() lying? - php

I loop through all files on my system, then do this check before the hash_file() call, since it kept giving me errors such as hash_file(): Read of 8192 bytes failed with errno=13 Permission denied or Failed to open stream: Resource temporarily unavailable::
if (is_readable($path))
hash_file($path, ...);
It still does it. is_readable() clearly doesn't check if it is readable at all, or else it wouldn't keep spitting out those errors even with this check.
How do I actually make sure that the file path in question can be read and won't result in any errors?

At first try to replace the is_readable function with the following:
function f_is_readable($path) {
$f=#fopen($path,"r");
if ( $f===false ) {
return false;
}
fclose($f);
return true;
}
If it works as you expected, we can assume that is_readable function is buggy for your PHP version.

Related

Warning: fopen(welcome.txt) [function.fopen]: failed to open stream:

how to remove this error is this possible to control this error?
<?php
$file=fopen("welcome.txt","r");
?>
I don't know exactly what you are looking for but you can add condition to check either file exists or not. maybe that'll solve your problem
<?php
if(!file_exists("welcome.txt")) {
die("File not found");
} else {
$file=fopen("welcome.txt","r");
}
?>
In the fopen() manual page we can read (emphasis mine):
Return Values
Returns a file pointer resource on success, or FALSE on failure
Errors/Exceptions
Upon failure, an E_WARNING is emitted.
So:
$file = fopen("welcome.txt","r");
if ($file) {
// Everything's fine
} else {
// Error happened
}
The same happened here.
I just deleted the ".txt" extension from the file and kept the one in the code and it worked.

Why is this function returning false?

I have the following code:
function lock() {
if($lock = #fopen('lock.txt', 'x')) {
fwrite($lock, getmypid());
fclose($lock);
return true;
}
else {
return false;
}
}
$response = lock();
When I run the code, the file lock.txt is created and the PID is put into the file. However, the function returns false. What in the world is going on?
I need X for fopen because I'm using this function for file locking and control
I took the # off and this is the error that I got:
fopen(lock.txt): failed to open stream: File exists in /xxx on line
22.
The problem is, I know for sure that the file does not exist -- I even went back and deleted it before I ran the code. The code creates the file but still returns false.
I checked to make sure no other code is creating the file. I even waited 30 secs to wait and see if the file reappeared -- it does not appear by itself, it only appears after I execute this code.
The PHP Manual states that for mode x:
Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
The function is returning false because #fopen('lock.txt', 'x') returns false (the file would already exist), which causes $lock = #fopen('lock.txt', 'x') to evaluate to false, triggering the branch to return false;.
fopen() mode X http://php.net/manual/en/function.fopen.php
Create and open for writing only; place the file pointer at the
beginning of the file. If the file already exists, the fopen() call
will fail by returning FALSE and generating an error of level
E_WARNING.
If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING.
You will see the warning if you don't use # error suppression.
You probably want mode W.
Open for writing only; place the file pointer at the beginning of the
file and truncate the file to zero length. If the file does not exist,
attempt to create it.

Setting PHP timeout for xml fopen

While accessing xml file with fopen that don’t exist we get
failed to open stream: HTTP request failed!
But it takes 30 seconds. How to return this error immediately or short after not finding a file.
Also, why when file is not found Apache scans lots of ports?
I think this is the way, You have to use fopen itself to check whether it exists or not,
function fileExists($path){
return (#fopen($path,"r")==true);
}
if(fileExists('http://example.com/test/file.xml'))
{
echo "exists";
}
else
{
echo "not_exist";
}

How to check if a file exists without error?

I like to do this:
if (file_exists( "path/a/b/c/file.txt" )) {
fopen("path/a/b/c/file.txt"); ----------> ERROR
do_this_if_file_exists();
}
else {
do_this_if_not_exists();
}
Unfortunately, I get the following error:
fopen(path/a/b/c/file.txt) [function.fopen]: failed to open stream: No such file or directory
What I'm doing wrong with file_exists?
In addition, when I call file exists with a path like: file_exists( "file.txt" ), that works well. I think the problem is the path (path/a/b/c/), but how to verify that without create the path first.
Thanks.
It is not a proper way to hide errors with #. All errors should be correctly handled. In other case debugging will be a pain.
Also, for the future you may use is_readable() function to make sure if the file is not only exists but also is readable, e.g. you have enough permissions.
You need to include error checking with fopen:
if (file_exists( "path/a/b/c/file.txt" ))
{
$fh=fopen("path/a/b/c/file.txt","r"); #or whatever mode you want...
if($fh!==false)
{
do_this_if_file_exists_and_can_be_opened();
}
else
{
die("Couldn't open the file. Sorry!\n");
}
}
else {
do_this_if_not_exists();
}
Check if given file exists and if it is realy a file:
if (file_exists( "path/a/b/c/file.txt" ) && is_file("path/a/b/c/file.txt")) {
$file_handler = #fopen("path/a/b/c/file.txt", MODE_IS_NOT_OPTIONAL)
if($file_handler !== false){
do_things_with_file_handler();
} else {
throw_some_error();
}
}
else {
do_this_if_not_exists();
}
Function file_exists() also can check if given directory exists and that shluod help You.
mode parameter in fopen function isn't optional as #Jack Maney pointed out. You have to use one of the mode possible values, full list.
Your problem is here:
fopen("path/a/b/c/file.txt");
Not only are you not assigning the return value to anything, but fopen() takes a required 2nd argument that you are not using.
You may turn off the error reporting by adding this line at the top of your php page
error_reporting(0);

PHP - Failed to open stream: no such file or directory - for existing file

I'm writing a PHP application and in my code i want to create create and return images to the browser. However, sometimes i'm getting some weird results where the image cannot be created since the file does not seem to exist.
Here is a sample error message I get and the code in a nutshell. I do know that the image exists, but still the method sometimes fails, and sometimes it succeeds, even for the same file.
The error:
Warning: imagecreatefrompng(path/to/image.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory in file test.php on line 301
The code:
if (file_exists($filename)) {
$image = imagecreatefrompng($filename);
}
I would greatly appreciate any hints or tips of what might be wrong and how I can improve the code to be more stabile.
I suggest you use is_readable
if (is_readable($filename)) {
$image = imagecreatefrompng($filename);
}
The file may "exist" but is the file accessible? what does file_exists actually do?
if it opens the file and then closes it make sure that the file is actualy closed and not locked before imagecreatedfrompng fires.
it would be a good idea to try catching the error in a loop and make 4 or 5 attempts before handing back a controlled error.
maybe try is_readable() or is_writable() instead?
Have you considered checking for the correct permissions? If the file cannot be read, but the directory can, you would get file_exists(...) = true, but would not be able to open a handle to the file.
Use is_readable() to check whatever you have permission to access that file.
You can try GD :
IF($img = #GETIMAGESIZE("testimage.gif")){
ECHO "image exists";
}ELSE{
ECHO "image does not exist";
}
bro check for white spaces in your filepath. I recently had this issue while i was tring to include a file from a module i was creating for an app. Other modules included well when called but one didnt. It turned out that there was a white space in the filepath. I suggest u try php trim() function. If this works holla.

Categories