Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I intercept a 404 error in PHP properly?
    text
    copied!<p>Essentially, what I am doing is intercepting a 404 using ErrorDocument in .htaccess, and pointing it at getFile.php. getFile then grabs the file name out of the url and searches for it in the mysql database, getting the data and applying appropriate headers.</p> <p>In my dev environment (up-to-date LAMP server running under ubuntu) everything seems to work fine, but the site is being hosted on GoDaddy, and there seems to be some issue with the headers returned. I think GoDaddy returns a 404, then sends the data anyway.</p> <p>The result is when I right click on a link that is to be handled in this manner, I can 'Save As', and everything works fine. If the file is an image, it opens no problem. But if I just click the link and it is a file that is to be downloaded rather than viewed in a browser, I get 'File Not Found' errors, across all browsers. Firebug Console claims it is a 404 error, regardless of whether the file successfully transfers. My code is the following:</p> <pre><code>$folder = explode( "/" , $_SERVER["REQUEST_URI"] ); $pageName = $folder[sizeof($folder)-1]; $sql = "SELECT data, mimeType, OCTET_LENGTH(data) AS size FROM tblFiles WHERE fileName = '".$pageName."' AND active=1;"; $result = mysql_query("$sql") or die(); $data = mysql_fetch_array($result); header("HTTP/1.0 200 OK"); header("Content-Type: ".$data['mimeType']); header("Content-Length: ".$data['size']); echo $data['data']; </code></pre> <p>And the .htaccess file:</p> <pre><code>ErrorDocument 404 /FILES/dbFiles/getFile.php </code></pre> <p>Now this is the full .htaccess file present at /FILES/dbFiles. I don't know if GoDaddy is expecting more stuff in there, but it seems to work just as written on my own server.</p> <p>I've tried numerous combinations of header info with no positive effect.</p> <p>My code apparently works, but I can't seem to prevent that 404 from happening.</p> <p>Thanks! -Jer</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload