[英]How to get file name from full path with PHP
This is my upload php: 这是我上传的php:
if (trim($_FILES['path_filename']['name']))
{
if (File::upload($_FILES['path_filename'], dirname(realpath(__FILE__)) . '/../tests'))
{
$test->setPathFilename('../tests/' . $_FILES['path_filename']['name']);
}
}
}
else
{
if ($aux)
{
$aux = str_replace("\\", "/", $aux);
$aux = preg_replace("/[\/]+/", "/", $aux);
$test->setPathFilename($aux);
}
}
$_POST["upload_file"] = $test->getPathFilename();
This above code is working well, I mean, upload to server is working and also getting Path File Name and insert into sql table is working too. 上面的代码运行良好,我的意思是,上传到服务器正常工作,并且也获取路径文件名并插入sql表中也正常工作。
Example: When I upload a file for example: ABC.jpg , it will upload to tests folder and also Path File Name is (( ../tests/ABC.jpg )) and it will insert to sql table. 示例:例如,当我上载一个文件:ABC.jpg时,它将上载到tests文件夹 ,并且路径文件名也是(( ../tests/ABC.jpg )),并且它将插入到sql表中。
The problem is here: 问题在这里:
I changed global function to rename files automatically by using this following code: 我使用以下代码将全局功能更改为自动重命名文件:
Before It was: 在此之前:
$destinationName = $file['name'];
I changed it to: 我将其更改为:
$ext = pathinfo($file["name"], PATHINFO_EXTENSION);
$destinationName = sha1_file($file["tmp_name"]).time().".".$ext;
Now, After upload file to tests folder, it will be renamed automatically, but still Path File name is same, It's ABC.jpg not renamed file in tests folder. 现在,将文件上传到测试文件夹后,它将自动重命名,但是路径文件名仍然相同,这是ABC.jpg未在测试文件夹中重命名的文件。
How to get Renamed Path File Name ???
如何获取重命名的路径文件名?
I really appreciate your help on this issue. 非常感谢您在此问题上的帮助。
Thanks in advance 提前致谢
Use basename() to get the filename from a path. 使用basename()从路径获取文件名。
$filename = basename('/path/to/file.ext');
This will give you: file.ext
这将为您提供:
file.ext
To rename the path file name you could use this: 要重命名路径文件名,可以使用以下命令:
if ( !file_exists( $path ) ) {
mkdir( $path, 0777, true );
}
This will make sure the path exist and if it doesn't it will created. 这将确保路径存在,如果不存在,则将创建该路径。 Now we can rename()
现在我们可以重命名()
rename( __FILE__ "/new/path/".$file_name );
This will move it between directories if necessary. 如有必要,这将在目录之间移动它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.