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.
Related
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.
I create one function for delete entry in little file in txt format , the problem it´s when i go delete entry show me this message :
Warning: flock() expects parameter 1 to be resource, boolean given in
The Script Function :
<?php
function delete_entry($name_file_db,$id_entry)
{
$fil_del=file("".$name_file_db."");
$fd=fopen("".$name_file_db."","w");
if (flock($fd,LOCK_EX))
{
ftruncate($fd,0);
fputs($fd,"".$fil_del[0]."");
for($de=1;$de<sizeof($fil_del);$de++)
{
if($de=="".$id_entry."")
{
fputs($fd,"");
}
else
{
fputs($fd,"".$fil_del[$de]."");
}
}
fflush($fd);
flock($fd, LOCK_UN);
fclose($fd);
}
else
{
if($db_activate_msg_bugs=="si")
{
print "Busy File";
}
}
}
?>
I don´t know why no works , i try differents combinations but continue fail
Thank´s Regards
$fd=fopen("".$name_file_db."","w") probably didn't open a file. fopen() returns false if it fails:
Returns a file pointer resource on success, or FALSE on error.
BTW ftruncate() is needless, fopen() in mode w implicitly truncates the file. This does also break your code. The blocked process is truncating your file! Consider using a dedicated lock file or open the file nondeconstructive (e.g. mode c).
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";
}
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);
I'm reviewing code that was written by another developer. The code tries to open a file, and sets $canOpen based on success/failure.
$fh = #fopen('files.php', 'a+');
if (!$fh){
fclose($fh);
$canOpen = false;
} else {
$canOpen = true;
}
What I find strange is that it also tries to close the file but only when the open fails if (!$fh). Does this make sense? Shouldn't the close be in the else statement when the file was successfully opened?
No, it makes no sense.
If !$fh is met as a condition, it means the $fh contains boolean FALSE (or possibly, in the event of some weird internal PHP error, NULL). So what you are effectively doing is:
fclose(FALSE);
...which is pointless and will result in an error.
And, if all you're trying to do is populate the $canOpen variable and not do anything useful with the file handle, wouldn't a combination of is_file(), is_readable() and is_writable() suffice?
The only reason one would close an unopened file is if it is for resiliency purposes. For example, always closing the file in the finally block of a try...catch.
In your case, it looks like a coding error.
if you put a var_dump( $fh ) in the true block of your if, you'll find that it's not a resource handle.
The php manual states that fclose takes a resource. Therefore fclose can't be called on a file that can't be opened.
<?php
$fh = #fopen('files.php', 'a+');
if (!$fh){
var_dump( $fh );
fclose($fh); // this will cause an error
$canOpen = false;
} else {
$canOpen = true;
}
No it doesn't make sense, if you cannot open the file you won't need to close it eigther. At this point the filepointer is always open because if the file doesn't exists it will create a file.