title: PHP fopen() not working. file exists, path is correct, permission checked
body: <p>I'm trying to open a file with fopen() but it doesn't work. </p>
  
  <p>"Warning: fopen(): Filename cannot be empty"</p>
  
  <pre><code>&lt;?php
  error_reporting(E_ALL);
  ini_set('display_errors',1);
  $file = 'output/example.csv';    
  $handle = fopen("$file", "r");
  if ($handle != FALSE) {
      echo "error";
  }
  ?&gt;
  </code></pre>
  
  <p>I checked if the file exists and if the path is correct.</p>
  
  <pre><code>echo $file;
  </code></pre>
  
  <p>gives me "output/example.csv"</p>
  
  <p>I can't figure it out. In an other php script the code above works fine...</p>
  
  <p>Need help please. Any help is appreciated. Thanks!</p>
  
  <p><strong>*******EDIT*******</strong>
  Maybe I should precise my problem and post a bigger part of my code:</p>
  
  <pre><code>    &lt;?php
  error_reporting(E_ALL);
  ini_set('display_errors',1);
  clearstatcache();
  require('mainConfig.php');
  
  ################ Save &amp; delete markers #################
  if($_POST) //run only if there's a post data
  {
      //make sure request is comming from Ajax
      $xhr = $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; 
      if (!$xhr){ 
          header('HTTP/1.1 500 Error: Request must come from Ajax!'); 
          exit(); 
      }
  
       /***** Datei öffnen *****/
      $code = filter_var($_POST["mfileName"], FILTER_SANITIZE_STRING);
      $file = 'output/'.$code.".csv";
      echo "file to open: ".$file."&lt;br&gt;";
      if (!file_exists($file)) {
          echo "file doesn't exist: ".$file."&lt;br&gt;";
      } else {
          echo "file exists: ".$file."&lt;br&gt;";
      }
  
      $directory = 'output/backup/versions/'.$code;
      if (!file_exists($directory)) {
          mkdir($directory, 777, true);
      }
      $handle = fopen($file, "rb"); // 'r' = Lese und Schreib Rechte
  
      if (!$handle) {
          echo "Es ist ein Fehler aufgetreten. Datei konnte nicht geöffnet werden.&lt;br&gt;";
      }
      fclose($handle);
  
      $i = 0; 
      if ($handle = opendir($directory)) {
          while (($file = readdir($handle)) !== false){
              if (!in_array($file, array('.', '..')) &amp;&amp; !is_dir($dir.$file)) 
                  $i++;
          }
      }
      closedir($handle);
      $versionFileNameAndPath = 'output/backup/versions/'.$code.'/'.$code.'_versionNr_'.$i.".csv";
      echo "copy Ziel-Datei: ".$versionFileNameAndPath."&lt;br&gt;";
      if (!file_exists($versionFileNameAndPath)) {
          if ( copy($file , $versionFileNameAndPath) ) {
            // file copied.
          } else {
              print_r(error_get_last());
            // error occurred..call error_get_last() function for err details.      
          }
      }
      $handle = fopen($file, "r"); // 'r' = Lese und Schreib Rechte
      $managedPinArray = "";
      $managedPinArrayIndexInFile = 0;
      if ($handle != FALSE) {
          echo "Es ist ein Fehler aufgetreten. Datei konnte geöffnet werden.";
      } else {
          $connection = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
          mysql_select_db($DB_Name,$connection) or die(mysql_error());
  ...
  etc.
  </code></pre>
  
  <p>And this gives me the following Output:</p>
  
  <pre><code>file to open: output/69-1407031331-de.csv
  file exists: output/69-1407031331-de.csv
  copy Ziel-Datei: output/backup/versions/69-1407031331-de/69-1407031331-de_versionNr_0.csv
  
  Warning: copy(): Filename cannot be empty in C:\xampp\htdocs\guide\map_process.php on line 49
  Array ( [type] =&gt; 2 [message] =&gt; copy(): Filename cannot be empty [file] =&gt; C:\xampp\htdocs\guide\map_process.php [line] =&gt; 49 )
  Warning: fopen(): Filename cannot be empty in C:\xampp\htdocs\guide\map_process.php on line 56
  </code></pre>