Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looking at your code, the download problem may be that the <code>$ext</code> variable contains an unexpected value or that the <code>$file</code> variable contains the name of a file that really doesn't exist.<br> In either this cases, none of your "if" conditions would be true, so the download would'nt start.<br> My suggestion is to add the following statements just before the "<code>//Finding MIME type</code>" comment line:</p> <pre><code>$log = "file='".$file."'\n"; $log .= "ext='".$ext."'\n"; @file_put_contents("/tmp/page1.log", $log, FILE_APPEND); </code></pre> <p>This way, looking at the "/tmp/page1.log" file you should be able to check if the <code>$file</code> and <code>$ext</code> variabiles effectively contain the expected values.<br> I've used "/tmp/page1.log" as the log file name since I suppose that you're working on linux; if not, please adjust the first argument of the "file_put_contents" function with a valid path and file name for your enviroment.<br> Also, I would replace the sequence of "if" tests with the following code:</p> <pre><code>$content_types = array( "pdf" =&gt; "application/pdf", "doc" =&gt; "application/msword", "txt" =&gt; "text/plain", "rtf" =&gt; "application/rtf", "docx" =&gt; "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "pptx" =&gt; "application/vnd.openxmlformats-officedocument.presentationml.presentation" ); if (isset($content_types[$ext])) { if (file_exists($file)) { header("Content-disposition: attachment; filename= '$filename'"); header('Content-type: '.$content_types[$ext]); readfile($file); die(""); } else { die("** '".$file."' does not exist **"); } } else { die("** Unhandled '".$ext."' extension **"); } </code></pre> <p>Obviously, you should implement the error handling in a much more robust way, not simply using the "die()" function as I did, but this is only an example.<br> Finally, be aware that there are also better ways of getting the content-type corresponding to the file extension; for example one solution is to use the PHP <a href="http://www.php.net/manual/en/ref.fileinfo.php" rel="nofollow noreferrer">Fileinfo</a> functions.<br> Have a look at <a href="https://stackoverflow.com/questions/1232769/how-to-get-the-content-type-of-a-file-in-php">this answer</a> for further information about this topic.<br> Keep also in mind that in <a href="http://it2.php.net/manual/en/features.safe-mode.php" rel="nofollow noreferrer">safe mode</a> the file_exists function always returns FALSE, and that the results of the file_exists function are cached; see the <a href="http://it2.php.net/manual/en/function.clearstatcache.php" rel="nofollow noreferrer">clearstatcache()</a> function on the PHP manual for further details.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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