php get file name (errors)

File path: $ path = '/home/files/1234.jpg' ;
php get file name, we should be well placed to exploit, and write a small function, every minute
. 1 ? < PHP 
 2  // get file name 
. 3  function get_fileName ( $ file_path ) {
 . 4      // . 1, to obtain a filename portion 
. 5      $ file_base_name = the basename ( $ file_path );
 . 6      // 2, is split into an array, i.e., obtain can 
. 7      $ file_name_arr = the explode (, '.' $ file_base_name );
 . 8      $ f_name = $ file_name_arr [0 ]; 
 . 9      return  $ f_name ;
 10  }
 . 11  
12 is >?

Calls the function echo get_fileName ($ path); // output 1234

A closer look, nothing wrong ah, what is the problem? The next step is to witness the miracle of time

Now comes a new file path /home/upload/abc.123.test.zip
Call the function again, the result output abc
This. . .
Now we know where the problem lies, right!
Well foolproof way is as follows
. 1 ? < PHP 
 2  // get file name 
. 3  function get_fileName ( $ file_path ) {
 . 4      // . 1, to obtain a filename portion 
. 5      $ file_base_name = the basename ( $ file_path );
 . 6      // 2, taken to find 
. 7      $ f_name = substr ( $ file_base_name , 0, strrpos ( $ file_base_name , '.' ));
 . 8      return  $ f_name ;
 . 9  }
 10 >?

 

Guess you like

Origin www.cnblogs.com/guliang/p/11661039.html