0

I'm using PHP's file_exist() Function to upload files (and dont get them double). Unfortunately, even when the folder is empty it says file exists. When i upload manually and let it check again it doenst recognize it as double. - Here is my code:

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

When the folder is empty: File exists When I put it in manually: File exists When I change it to if(exist==1) it works once but it doenst make any sense to me.

Thanks for any help in advance!

M. M.
  • 27
  • 3
  • There is a difference between = and == in php, = is assignment and == is comparison. effectively, ```$exist=0``` will always return false, so ```if(false) {} else{ // this will always happen }``` – MHewison Jul 19 '19 at 10:52
  • Obviously, I'm an idiot! Thank you very much - lack of concentration - I'm very glad that you found that silly mistake! Best regards, MM – M. M. Jul 19 '19 at 10:54

1 Answers1

0

There is a difference between = and == in php.
= is assignment and == is comparison.

Effectively, $exist=0 will always return false, so

if(false) {

} else{ 
     // this will always happen 
}
Andreas
  • 23,610
  • 6
  • 30
  • 62
M. M.
  • 27
  • 3