find the name of a file in php

To find the name of a file in PHP, you can use the basename() function. This function returns the base name of a file path, which is the last component of the path.

Here's an example:

main.php
$path = "/path/to/myfile.txt";
$filename = basename($path);
echo $filename; // outputs "myfile.txt"
100 chars
4 lines

In this example, we first define the file path using a string. We then pass this path to the basename() function, which returns the file name as a string. We can then use echo to output the name to the screen.

If you're working with a file uploaded via a form, you can use basename() to get the file name from its temporary location:

main.php
$filename = basename($_FILES["file"]["tmp_name"]);
echo $filename; // outputs the name of the uploaded file
108 chars
3 lines

Note that in this case, we're accessing the tmp_name property of the $_FILES array, which contains the temporary name of the uploaded file on the server. We can then pass this to basename() as before to get the file name.

related categories

gistlibby LogSnag