How do I get a file name from a full path with PHP?
You're looking for basename
.
The example from the PHP manual:
<?php
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
?>
Get full path from filename in php
You can use the solution provided here.
It allows you to recurse through a directory and list all files in the directory and sub-directories. You can then compare to see if it matches the files you are looking for.
How to get file name and its full path with PHP using scandir
Try this:
function listFolderFiles($dir){
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
echo '<li>'.$ff;
echo " Real Path: ". $dir.'/'.$ff;
if(is_dir($dir.'/'.$ff))
listFolderFiles($dir.'/'.$ff);
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('/var/www/TestFiles');
extract filename from path
you should use the function basename instead:
$filepath = 'abc\filename.txt';
$filename = basename($filepath);
edit: important note, you need to use single quotes when you have backslashes in your strings, else escape them properly.
note: this will not work:
$filepath = "abc\filename.txt";
$filename = basename($filepath);
because you're variable $filepath infact holds:
abc[special char here equalling \f]ilename.txt
another edit:
this regex works too..
$filepath = '\def\abc\filename.txt';
$basename = preg_replace('/^.+\\\\/', '', $filepath);
all that was wrong with your original was that you had double-quotes rather than single, and backslash needs double escaped (\\ rather than \).
php get full path of file in a folder
<?php
$path = dirname(__FILE__);
$objects = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($objects as $file => $object) {
$basename = $object->getBasename();
if ($basename == '.' or $basename == '..') {
continue;
}
if ($object->isDir()) {
continue;
}
$fileData[] = $object->getPathname();
}
var_export($fileData);
PHP force download, download without naming the file the full path name
In readfile you have to pass full path, but in header in filename file name for user:
ob_clean();
if(isset($_POST['file_name'])){
$file_for_user = $_POST['file_name'];
$full_path_file = "STORAGE_PATH".$file_for_user;
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$file_for_user.'"');
readfile($full_path_file);
exit();
}
Way to get absolute path of file PHP
You can try like this
include dirname(__FILE__).'/../yourfile.php';
Since PHP 5.3.0 you can use also the magic constant __DIR__
More info in the docs
Another way is
$rootDir = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$rootDir/yourfile.php";
Related Topics
PHP Method Chaining or Fluent Interface
Pdo Prepared Inserts Multiple Rows in Single Query
How to Get File_Get_Contents() to Work With Https
Sort an Array by Keys Based on Another Array
Why Does PHP Consider 0 to Be Equal to a String
Simplexml: Selecting Elements Which Have a Certain Attribute Value
Download File to Server from Url
Creating the Singleton Design Pattern in PHP5
Bind Multiple Parameters into MySQLi Query
How to Remove  from the Beginning of a File
How to Strip All Spaces Out of a String in PHP
How to Get Numeric Types from MySQL Using Pdo
Automated or Regular Backup of MySQL Data
Understanding MVC Views in PHP
PHP Error: "Cannot Pass Parameter 2 by Reference"
Automatically Deleting Related Rows in Laravel (Eloquent Orm)