如果文件名存在,如何避免上传

First of all, I wanted to use auto rename file , But I couldn't find a solution to get file path name after it's renamed. so, is it possible to add something into my code to check if file exist then it won't allow to upload file?

Here's my code:

 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;
}

My problem is about if file exist, it will be replaced to existing file, I could find a way to use auto renamed file, something like this:

$ext = pathinfo($file["name"], PATHINFO_EXTENSION);
$destinationName = sha1_file($file["tmp_name"]).time().".".$ext;

However I couldn't find a solution to post file name after renamed into sql table. It will post as real name not renamed file.

So my last choice is to add something into my code to check if file exist then avoid to upload file if its exist.

Could you please help me on this issue?

Thank you so much

Using your code suggestion, maybe something like this:

Under this line: $destinationFile = $destinationDir . '/' . $destinationName;

Put this:

if(file_exists($destinationFile)){
    $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    $destinationFile = $destinationDir.'/'.sha1_file($file['tmp_name']).time().'.'.$ext;
}