yakking Posted September 5, 2015 Share Posted September 5, 2015 I've been reading up on "DIE" in PHP when a file is not found you can display a message such as "File not found" etc. So I am trying to work out how to implement it. I have a simple download.php file inside which is <?php echo $_GET["f"]; ?> which prints the URL when called e.g. www.example.com/download.php?f=name.zip So if a file is not found what is the correct code to use in this situation? I don't wish to use a apache rewrite as its not specific to my needs. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 5, 2015 Share Posted September 5, 2015 There is no universal "correct" way. It all depends on your specific needs. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 5, 2015 Share Posted September 5, 2015 Just like Barand said. Can try this though, if want direct download with header and content types will have to work that out. $path = "/download/"; if (isset($_GET['f']) && trim($_GET['f']) != "") { if (is_file($_SERVER['DOCUMENT_ROOT'] . $path . $_GET['f'])) { echo " <a href='" . $path . $_GET['f'] . "' download>" . $_GET['f'] . "</a>"; } else { header("HTTP/1.0 404 Not Found"); die('File not found'); } } else { header("HTTP/1.0 404 Not Found"); die('File missing'); } Quote Link to comment Share on other sites More sharing options...
yakking Posted September 5, 2015 Author Share Posted September 5, 2015 Thanks QuickOldCar I'll check it out! Quote Link to comment Share on other sites More sharing options...
gizmola Posted September 6, 2015 Share Posted September 6, 2015 Personally, While QoC's code will work and answers the question, I don't feel that a 404 is appropriate there. 404's are really for URL's that can't be found, as part of the HTTP spec. In your case, I think you should simply return a message to the user indicating the issue. Exactly what you want to do is really a matter of personal preference, that comes down to User interface design. Quote Link to comment Share on other sites More sharing options...
scootstah Posted September 6, 2015 Share Posted September 6, 2015 Returning a 404 in the case that a resource does not exist is perfectly valid. If $_GET['f'] acts as a resource identifier, then a 404 in this case is fine. But from a UX point of view I agree that a message is better. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.