Setting PHP timeout for xml fopen - php

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";
}

Related

Why is is_readable() lying?

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.

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.

Flock for delete entry in text no work me

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).

Cannot open file using fopen (php)

I am trying to open a file for reading in php script but having trouble.
This is my code
$fileHandle = fopen("1234_main.csv", "r")or die("Unable to open");
if (!file_exists($fileHandle))
{
echo "Cannot find file.";
}
The script is in the same directory as the file I am trying to read and there are no other read/write permission errors as I can create/read other files in the same directory.
When I run the script I just get the "Cannot find file" error message. Why is this error message being shown? Surely if fopen() can't open the file the "or die statement" should end the script?
Also, why can't I open the file when it definitely exists and is in the same location as the script (I have also tried using the full path of the filename instead of just the filename).
I am fairly new to php (but have exp in c++) so if its a stupid question I apologize.
Many thanks
In PHP, file_exists() expects a file name rather than a handle. Try this:
$fileName = "1234_main.csv";
if (!file_exists($fileName))
{
echo "Cannot find file.";
} else {
$fileHandle = fopen($fileName, "r")or die("Unable to open");
}
Also keep in mind that filenames have to be specified relative to the originally requested php-script when executing scripts on a web server.
You can use file_get_content() for this operation. On failure, file_get_contents() will return FALSE.For example
$file = file_get_contents('1234_main.csv');
if( $file === false ){
echo "Cannot find file.";
}
file_exists() take the file-name as input, but the logic of your code has problem. You first try to open a file then you check its existence?
You first should check its existence by file_exists("1234_main.csv") and if it exists try to open it.
file_exists takes a string, not a file handle. See http://php.net/manual/en/function.file-exists.php

In PHP, check if a file can be deleted

I have a large video file that is streamed using a streaming server outside my control, and sometimes I want to delete the video file. When the file happens to be viewed by someone using the streaming server, PHP errors with "Permission denied".
I would like to check before attempting to delete if the file can be deleted or not. I do not want to actually try to delete the file and see if that fails, I would like to check beforehand.
This is my code so far:
$file = "video.flv";
$file2 = "newvideoname.flv";
clearstatcache();
if (is_writeable($file)) {
echo "is writeable";
}
else {
echo "is NOT writeable";
}
echo "\n";
$fh = fopen($file, 'a+');
if (!flock($fh, LOCK_EX | LOCK_NB)) {
// file locked, do something else
echo "is locked";
}
else {
echo "not locked!";
}
fclose($fh);
echo "\n";
if (touch($file)) {
echo "modification time has been changed to present time";
}
else {
echo "Sorry, could not change modification time";
}
echo "\n";
rename($file, $file2);
The output I get when I stream video.flv while executing the code:
is writeable
not locked!
modification time has been changed to present time
PHP Warning: rename(video.flv,newvideoname.flv): Permission denied in ...
Sometimes I get:
is writeable
PHP Warning: fopen(video.flv): failed to open stream: Permission denied ...
PHP Warning: flock() expects parameter 1 to be resource, boolean given
is locked
PHP Warning: fclose(): supplied argument is not a valid stream resource
PHP Warning: touch(): Utime failed: Permission denied
Sorry, could not change modification time
PHP Warning: rename(video.flv,newvideoname.flv): Permission denied ...
So sometimes the file cannot be locked by PHP and it cannot be touch()ed by PHP, and then of course the rename doesn't work, but sometimes PHP says "all is fine" until the rename command. The rename command has never worked by random chance.
What should I do with the file?
if (!flock(fopen($file, 'a+'), LOCK_EX | LOCK_NB)) {
You are locking the file. Make sure you unlock it again if the call succeeds (just after echo "not locked!";
The problem as I see it is that when you checked if the file is locked with flock you opened a resource to the file but did not close the file. So the file is now locked and you can't rename it.
You cannot delete a file that is streaming...if you REALLY WANT TO...make a copy, raname it, and put the file(original) in a stack(DB) and have a cron job or something to delete it...
The solution is here :
$file = "test.pdf";
if (!is_file($file)) {
print "File doesn't exist.";
} else {
$fh = #fopen($file, "r+");
if ($fh) {
print "File is not opened and seems able to be deleted.";
fclose($fh);
} else {
print "File seems to be opened somewhere and can't be deleted.";
}
}

Categories