如何在php中获取图像文件名

xSe*_*Sea 0 php url image

假设我有这个文件夹来存储验证码图像

$directory = "http://localhost/test/wp-content/plugins/test/captcha/";
Run Code Online (Sandbox Code Playgroud)

我尝试了很多代码,如 opendir、glob() 等,试图从上面的目录中尝试我的图像文件名,但失败了。

图像文件名为 captcha.jpeg。

我别无选择,只能使用静态 url,如下所示。但是我的验证码将生成带有随机(名称)的图像。jpeg 这就是为什么我需要一种动态方式来获取图像文件,其中 *.jpeg

$directory="http://localhost/test/wpcontent/plugins/test/captcha/captcha.jpeg";
Run Code Online (Sandbox Code Playgroud)

我想将整个图像 url 作为 json 返回。请任何人有资源帮助我谢谢。

return array("image url" => $image_url);
Run Code Online (Sandbox Code Playgroud)

小智 5

你试了吗?

$path_parts = pathinfo('http://localhost/test/wpcontent/plugins/test/captcha/captcha.jpeg');

$returnArray = ['image url' => $path_parts['basename']];

return json_encode($returnArray);
Run Code Online (Sandbox Code Playgroud)

它返回:

{
    "image_url" : "captcha.jpeg"
}
Run Code Online (Sandbox Code Playgroud)

Pathinfo() PHP 手册