首先,我想使用自动重命名文件,但在重命名后找不到获取文件路径名的解决方案。所以,有没有可能在我的代码中添加一些东西来检查文件是否存在,然后它就不允许上传文件了?
这是我的代码:
public static function upload(&$file, $destinationDir = "", $destinationName = "", $secure = true)
{
$ret = false;
if (isset($file['tmp_name']) && isset($file['name']))
{
if ($destinationName == '')
{
$destinationName = $file['name'];
}
$destinationFile = $destinationDir . '/' . $destinationName;
if (move_uploaded_file($file['tmp_name'], $destinationFile))
{
if ($secure)
{
chmod($destinationFile, 0644); // without execution permissions if it is possible
}
$ret = true;
}
}
return $ret;
}
我的问题是,如果文件存在,它将被替换为现有文件,我可以找到一种使用自动重命名文件的方法,类似于这样:
$ext = pathinfo($file["name"], PATHINFO_EXTENSION);
$destinationName = sha1_file($file["tmp_name"]).time().".".$ext;
然而,在重命名到sql表后,我找不到发布文件名的解决方案。它将以实名制发布,而不是重命名文件。
所以我最后的选择是在我的代码中添加一些东西来检查文件是否存在,然后避免上传文件(如果存在)。
你能在这个问题上帮我吗?
非常感谢
使用您的代码建议,可能是这样的:
在行下:$destinationFile=$destinatationDir.'/'$destinationName;
放这个:
if(file_exists($destinationFile)){
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$destinationFile = $destinationDir.'/'.sha1_file($file['tmp_name']).time().'.'.$ext;
}