这是我上传的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();
上面的代码运行得很好,我的意思是,上传到服务器也在运行,获取路径文件名并插入sql表也在运行。
示例:当我上传一个文件时,例如ABC.jpg,它将上传到tests文件夹,路径文件名为(../tests/ABC.jpg),并插入到sql表中。
问题就在这里:
我更改了全局功能,使用以下代码自动重命名文件:
以前是:
$destinationName = $file['name'];
我将其更改为:
$ext = pathinfo($file["name"], PATHINFO_EXTENSION);
$destinationName = sha1_file($file["tmp_name"]).time().".".$ext;
现在,上传文件到测试文件夹后,它会自动重命名,但路径文件名仍然相同,它是测试文件夹中未重命名的ABC.jpg文件。
如何获得重命名路径文件名
我真的很感谢你在这个问题上的帮助。
提前感谢
使用basename()从路径中获取文件名。
$filename = basename('/path/to/file.ext');
这将为您提供:file.ext
要重命名路径文件名,可以使用以下命令:
if ( !file_exists( $path ) ) {
mkdir( $path, 0777, true );
}
这将确保路径存在,如果不存在,将创建路径。现在我们可以重命名()
rename( __FILE__ "/new/path/".$file_name );
如果需要,这将在目录之间移动它。