How do I check if a directory exists? is_dir, file_exists or both?
Both would return true on Unix systems - in Unix everything is a file, including directories. But to test if that name is taken, you should check both. There might be a regular file named 'foo', which would prevent you from creating a directory name 'foo'.
Check if directory exists, make directory
Change this :
if(!is_dir($new_path)) {
echo "The Directory {$new_path} exists";
}
to this :
if(is_dir($new_path)) {
echo "The Directory {$new_path} exists";
}
Try and tell me the result :)
Check whether a directory exists in PHP
Should work correctly. From is_dir()
documentation:
Returns TRUE if the filename exists
and is a directory, FALSE
otherwise.
Well, anyway if it doesn't try this:
if(file_exists($dir) && is_dir($dir))
BTW. results of these functions are cached in stat cache. Use clearstatcache() to clean that cache.
How to use file_exists function for a directory outside of current folder?
The solution was the use the ABSOLUTE file path, which I found using __FILE__
So the correct path was :
<?php
$filename = "/root/26/987654321/htdocs/websites/WEBSITE/190/$info[product_id].jpg";
if (file_exists($filename)) { echo 'Found'; } else { echo 'Not Found'; }
?>
Note actual file path has been altered for security reasons, the path provided above is for illustrative purposes.
How to check if file exists in a folder, if not move onto next folder to check php
<?php foreach ($userdetails as $key => $userdetail): ?>
<tr>
<td><?php echo $userdetail['username']; ?></td>
<td>
<?php
foreach(
[
'admin/assets/images/admin/' => 'admin',
'vendor/assets/images/vendor/' => 'vendor',
'customer/assets/images/Customer/' => 'customer',
] as $path => $stub
) {
$filename = $path . $userdetail['user_image']; // e.g. admin/assets/images/vendor/foobar.jpg
$url = 'assets/images/' . $stub . '/' . $userdetails['user_image']; // e.g. assets/images/vendor/foobar.jpg
if ( file_exists( $filename ) ) {
?>
<img src="<?= $url ?>" style="width: 50px; height: 50px;">
<?php
break;
}
}
?>
</td>
</tr>
<?php endforeach ?>
php check file_exists() out of the third level to the root dir
From the PHP docs for is_dir(), regarding the first passed parameter:
Path to the file. If filename is a relative filename, it will be checked relative to the current working directory. If filename is a symbolic or hard link then the link will be resolved and checked. If you have enabled safe mode, or open_basedir further restrictions may apply.
In other words, if you specify a relative path, it will be considered relative to the currently executing script, which is why your script works in the first case, but not in the second. Personally I perfer to use full paths when operating on files and directories to make sure I know what I'm pointing at. To poke around a bit and see what I'm talking about try this:
<?php
$current_dir = dirname(__FILE__);
$path = realpath( $current_dir . '/../images' );
var_dump( $path );
In general, PHP frameworks tend to specify some full paths during their bootstrap, like absolute path to the document root, the application folder, the log folder, etc. and then use those paths whenever specifying a path to a file. This is handy and tends to be very stable because the bootstrap file tends to stay in the same filepath, whereas with the problem you are experiencing, your path resolution breaks when you move the script from folder to folder. I say all this by way of saying you might consider creating a "document root" variable in a header file, for example, include that header file in each of your scripts, and then use that variable when you need to create filepaths.
Related Topics
How So I Convert This Sha256 + Base64 from Swift to PHP
Converting an Array from One to Multi-Dimensional Based on Parent Id Values
Difference Between Var_Dump,Var_Export & Print_R
Accessing an Associative Array by Integer Index in PHP
Get Next Element in Foreach Loop
How to Rotate Image and Save the Image
PHP Preg_Match (.*) Not Matching Past Line Breaks
Preg_Match Equivalent in Swift
Changing Woocommerce Cart Item Names
Check If Url Has Certain String with PHP
Pull All Images from a Specified Directory and Then Display Them
Create New Xml File and Write Data to It
Column Count of MySQL.Proc Is Wrong. Expected 20, Found 16. the Table Is Probably Corrupted