Say I have an path: images/alphabet/abc/23345.jpg
How do I remove the file at the end from the path? So I end up with: images/aphabet/abc/
Removing the file path from a filename in PHP is be easy! The basename() function will remove the file's path from your string and leave only the filename. So, for example, it means that if your file path string is: '/home/anto.
PHP rmdir() Function The rmdir() function removes an empty directory.
__FILE__ is simply the name of the current file. realpath(dirname(__FILE__)) gets the name of the directory that the file is in -- in essence, the directory that the app is installed in. And @ is PHP's extremely silly way of suppressing errors.
You want dirname()
dirname()
only gives you the parent folder's name, sodirname()
will fail wherepathinfo()
will not.
For that, you should use pathinfo()
:
$dirname = pathinfo('images/alphabet/abc/23345.jpg', PATHINFO_DIRNAME);
The PATHINFO_DIRNAME
tells pathinfo
to directly return the dirname
.
See some examples:
For path images/alphabet/abc/23345.jpg
, both works:
<?php $dirname = dirname('images/alphabet/abc/23345.jpg'); // $dirname === 'images/alphabet/abc/' $dirname = pathinfo('images/alphabet/abc/23345.jpg', PATHINFO_DIRNAME); // $dirname === 'images/alphabet/abc/'
For path images/alphabet/abc/
, where dirname
fails:
<?php $dirname = dirname('images/alphabet/abc/'); // $dirname === 'images/alphabet/' $dirname = pathinfo('images/alphabet/abc/', PATHINFO_DIRNAME); // $dirname === 'images/alphabet/abc/'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With