Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/298065-the-correct-way-to-use-die/
Share on other sites

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');

}

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.