title: php failed to open stream when file exists
body: <pre><code>$fileToAccess="userFiles/".$json[0]-&gt;userfor2.".json";
  document.addEventListener('click', function(e)
  {
     xhttp=new XMLHttpRequest();
     xhttp.onreadystatechange = function() 
     {
       if (this.readyState == 4 &amp;&amp; this.status == 200) 
          {
              window.alert(this.responseText);
          }
     }
     xhttp.open("GET", "evaluate.php?q=[{\"id\": \""+e.target.id+"\"},{\"file\": \"&lt;?php echo $fileToAccess;?&gt;\"}]", true);
     xhttp.send();
     //alert(e.target.id)
  });
  </code></pre>
  
  <p>and my getData.php:</p>
  
  <pre><code>&lt;?php
      $file=$_REQUEST['q'];
      $myfile=file_get_contents($file);
      $json=json_decode($myfile);
      echo $json[1];
  ?&gt;
  </code></pre>
  
  <p><strong>Things I tried</strong></p>
  
  <p>added ./ in front of userFiles.</p>
  
  <p>added__DIR__ before /userFiles</p>
  
  <p>but I still get the error failed to open stream, even though I see the url displayed completely fine in the alert message and my file exists too</p>
  
  <p><strong>Permissions</strong>: chmod -R 777 on entire directory</p>
  
  <p><strong>EDIT:</strong> alert message:
  <a href="https://i.stack.imgur.com/5tVu5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5tVu5.png" alt="This is the case using __DIR__"></a>
  <strong>EDIT 2:</strong> after adding <code>var_dump($file)</code> in getData.php:
  <a href="https://i.stack.imgur.com/Canid.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Canid.png" alt="enter image description here"></a>
  <strong>EDIT 3:</strong> after removing all spaces and " from $file, I get the following alert: <a href="https://i.stack.imgur.com/pLhlz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pLhlz.png" alt="enter image description here"></a></p>