简体   繁体   English

从PHP中的字符串获取文件名

[英]Get a filename from a string in PHP

I have this strings saved in my mysql database in format like: 我将此字符串以以下格式保存在我的mysql数据库中:

/uploads/attachments/18/105/WordPress_3_Cookbook.pdf

Now, I need to get only the file name. 现在,我只需要获取文件名。

I guess I need some function in PHP that check the string from the backwards and get everything to the first slash. 我想我需要在PHP中使用一些函数来从后向检查字符串,并使所有内容都达到第一个斜杠。

How to write such thing? 怎么写这样的东西?

您可以通过这种方式使用basename()

$filename = basename("/uploads/attachments/18/105/WordPress_3_Cookbook.pdf");

1.If you don't want to type all the link path inside the "basename()" function, you can use: 1.如果不想在“ basename()”函数中键入所有链接路径,则可以使用:

$_SERVER['REQUEST_URI']

this will return the whole path - (example below) 这将返回整个路径-(下面的示例)

"/uploads/attachments/18/105/WordPress_3_Cookbook.pdf"

2.THEN make use of the 'baseline()' function like this: 2.然后像这样使用'baseline()'函数:

      echo basename($_SERVER['REQUEST_URI']);

and will return the desired file name as a string. 并以字符串形式返回所需的文件名。

      // the output will be : WordPress_3_Cookbook.pdf 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM