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.php100 chars4 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.php108 chars3 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.
gistlibby LogSnag