PHP file_exists()看到文件,尽管确实存在

时间:2019-07-19 10:49:55

标签: php php-7

我正在使用PHP的file_exist()函数上传文件(并且不要让它们翻倍)。不幸的是,即使文件夹为空,它也表示文件存在。当我手动上传并再次检查时,它确实将其识别为两倍。 -这是我的代码:

<?php
  $directoy = $_POST['kategorie'];
  $heading = $_POST['headline1'];

  $file = $_FILES['fileToUpload']['name'];
  $file_tmp = $_FILES['fileToUpload']['tmp_name'];

  $endung = pathinfo($file, PATHINFO_EXTENSION);
  $custom_path = 'files/'.$directoy."/".$heading.".".$endung;
  $default_path = 'files/'.$directoy."/".$file;


  if(empty($heading)){
      $exist = file_exists($default_path);
      if($exist=0){
        move_uploaded_file($file_tmp, $default_path);
      }else{
        echo "Dateiname exisitert bereits!<br><br>";
      }

    }else{
      $exist = file_exists($custom_path);
      if($exist=0){
        move_uploaded_file($file_tmp, $custom_path);
      }else{
        echo "Dateiname exisitert bereits!<br><br>";
      }
  }
?>

当文件夹为空时:文件存在 当我手动将其放入时:文件存在 当我将其更改为if(exist == 1)时,它可以工作一次,但对我来说确实没有任何意义。

感谢您的任何帮助!

1 个答案:

答案 0 :(得分:0)

php中=和==之间有区别。
=是分配,==是比较。

实际上,$exist=0总是返回false,因此

if(false) {

} else{ 
     // this will always happen 
}