Home > Article > Backend Development > How to get only the file name in php
How to get only the file name in php: first create a PHP sample file; then get the file part through the "basename($file_path);" method; finally get the file name through the "substr()" method .
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How does php only get the file name?
php gets the file name of the file (misunderstanding)
File path: $path = '/home/files/1234.jpg';
php gets the file name, everyone You should be familiar with it. Write a small function and call the function echo get_fileName($path) in minutes
<?php //获取文件名 function get_fileName($file_path){ //1、先获取带文件部分 $file_base_name = basename($file_path); //2、拆分为数组,获取即可 $file_name_arr = explode('.',$file_base_name); $f_name = $file_name_arr[0]; return $f_name; } ?>
; // Output 1234
Look carefully, there is nothing wrong with it. What is the problem? Is it? Now it’s time to witness the miracle
Now comes a new file path/home/upload/abc.123.test.zip
Call the function again, and the result is abc
this. . .
Now you know what the problem is!
Okay, the complete method is as follows
<?php //获取文件名 function get_fileName($file_path){ //1、先获取带文件部分 $file_base_name = basename($file_path); //2、查找截取即可 $f_name = substr($file_base_name,0,strrpos($file_base_name,'.')); return $f_name; } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get only the file name in php. For more information, please follow other related articles on the PHP Chinese website!