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);
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.
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.
I'm trying to move folder to other folder, with all it's files. Both folders are in root directory. Tried a lot of ways, and always get no result.
Here is my latest atempt:
$source = "template/"
$dest = "projects/"
function copyr($source, $dest){
if (is_link($source)) {
return symlink(readlink($source), $dest);
}
if (is_file($source)) {
return copy($source, $dest);
}
if (!is_dir($dest)) {
mkdir($dest);
}
$dir = dir($source);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
copyr("$source/$entry", "$dest/$entry");
}
$dir->close();
return true;
}
Need professional glance to tell me, where I'm getting it wrong?
EDIT:
Sorry for wrong tags.
Problem is - nothing is happening. Nothing is being copied. No error messages. Simply nothing happens.
File structure:
I suggest to try and do the following
How do you run the scrips? Do you open page in browser or run script in command line? If you open page in browser this might be an issue with permissions, paths (relative and not absolute) and errors not shown but logged.
Use absolute folder paths instead of relative paths. For example /var/www/project/template.
Apply realpath() function to all paths and check (output) the result. If path is wrong (folder does not exist, separators are wrong etc) you will get empty result from the function.
Make sure to use DIRECTORY_SEPARATOR instead of / if you run your script on Windows. I can not check if / works on Windows now but potentially this might be an issue. For example
copyr($source.DIRECTORY_SEPARATOR.$entry", $dest.DIRECTORY_SEPARATOR.$entry);
Check warnings and errors. If you do not have permission you should get warning like this
PHP Warning: mkdir(): Permission denied
You may need to enable warnings and errors if they are disabled. Try for example to make an obvious mistake with name and check if you get any error message.
Try to use tested solution from one of the answers. For example xcopy function.
Try to add debug messages or run your script in debugger step by step. Check what is happening, what is executed etc. You can add debug output near any operator like (just an idea):
echo 'Creating directory '.$name.' ... ';
mkdir($name);
echo (is_dir($name) ? 'created' : 'failed').PHP_EOL;
I would like a list or code block of all necessary checks needed to prevent and unlink function from throwing an error. The more detailed the better as I am hoping this question will solve a lot of peoples issues.
Reasons for Errors
File doesn't exist
File Not Writable
Containing Directory Not Writable #marc-anton-dahmen answer
File open/in use
This is what I have so far
if(is_actually_file($file)){
if(is_writable($file) && is_writable(dirname($file))) {
unlink($file);
} else {
// insufficient file permissions
}
} else {
// file doesn't exist
}
function is_actually_file($file){
clearstatcache(true, $file);
return is_file($file);
}
What other reasons are there for failing and how to check for them?
unlink source code
is_writable includes a file_exists check. But you have to check if the containing directory is writable as well.
This should do it:
if (is_writable($file) && is_writable(dirname($file))) {
unlink($file);
} else {
//...
}
i can't understand one thing. In code, for example:
$filePath = 'http://wwww.server.com/file.flv';
if( file_exist($filePath) )
{
echo 'yes';
}
else
{
echo 'no';
}
Why does script return 'no', but when i copy that link to the browser it downloads?
The file_exists() function is looking for a file or directory that exists from the point of view of the server's file system. If http://www.server.com/ equates to /home/username/public_html/ then you need to make your code:
$filename = '/home/username/public_html/file.flv';
if(file_exists($filename))
{
//true branch
}
else
{
//false brach
}
See http://php.net/file_exists for more info.
GIYF.
http://sg.php.net/manual/en/function.file-exists.php#75064
use
$_SERVER["DOCUMENT_ROOT"]
to assure the right filesystem path, not dependent by development or production system for example.
in this case, it will be
$filePath = $_SERVER["DOCUMENT_ROOT"].'/file.flv';
file_exists() check for filesystem files and directories. Use fopen() too see whether that web URL is accessible. In case the respective server will return 404 Not Found for that resource, fopen() will return false and issue an warning. A much better solution would be to issue an HTTP HEAD request.
First of all the php function you need to use is file_exists() with the 's' at the end. Second of all, I think the path to the file needs to be a local file path, not a URL. Not sure though...
do:
function isExistsFileOnMyWebsite($fileName) {
return file_exist($_SERVER['DOCUMENT_ROOT'].'/'.$fileName);
}
if( isExistsFileOnMyWebsite('file.flv') )
{
echo 'yes';
}
else
{
echo 'no';
}