0

Every time I use this code and download the file, I get an errors. I assume this is because of the path to the file.

The download.php file is located in /modules/ and is included within index.php on root (inside htdocs/project/) / . The file is located on /modules/download/file.zip

What could be the cause? The URL should be system path? What if I don't want to use system path? Or what is my other issue here?

        $stmt = $this->GetDatabaseConnection()->prepare('SELECT * FROM download_files WHERE id=?');
        $stmt->bind_param('i', $_GET['file']);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($id, $name, $desc, $filename, $size, $added, $clicks, $uclicks);
        $stmt->fetch();
        if($stmt->num_rows > 0) {
            $fakeFileName= $name . '.zip';
            $realFileName = $filename;
            $file = "download/".$realFileName;
            $fp = fopen($file, 'rb');
            header("Content-Type: application/octet-stream");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Disposition: attachment; filename=$fakeFileName");
            header("Content-Length: " . filesize($file));
            fpassthru($fp);
            exit;
        }
        $stmt->free_result();
        $stmt->close();

Error:

<b>Warning</b>:  fopen(download/6694488-PHPUploadFile.zip): failed to open stream: No such file or directory in <b>C:\xampp\htdocs\project\modules\download.inc.php</b> on line <b>27</b><br />
<br />
<b>Warning</b>:  filesize(): stat failed for download/6694488-PHPUploadFile.zip in <b>C:\xampp\htdocs\project\modules\download.inc.php</b> on line <b>31</b><br />
<br />
<b>Warning</b>:  fpassthru() expects parameter 1 to be resource, boolean given in <b>C:\xampp\htdocs\project\modules\download.inc.php</b> on line <b>32</b><br />
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • Hiding the real url as stated from: http://stackoverflow.com/questions/10997516/how-to-hide-the-actual-download-folder-location – HelpNeeder Nov 13 '14 at 06:00
  • It is correct '6694488-PHPUploadFile.zip' for example. And the file exist on server in 'modules/download/6694488-PHPUploadFile.zip'. – HelpNeeder Nov 13 '14 at 06:02

1 Answers1

0

You're using a relative path and fopen() needs an absolute path. Use any of your constants/methods/etc which gives you the absolute path to your project directory to construct the absolute path to your file.
One possibility to do that would be by using the server variables:

$file = $_SERVER['DOCUMENT_ROOT'] . '/modules/download/' . $realFileName;
Havelock
  • 6,913
  • 4
  • 34
  • 42
  • I get an error saying that cannot open file. I think it's the windows thing. The file does exist on this exact location: `C:/xampp/htdocs/modules/download/6694488-PHPUploadFile.zip
    Warning: fopen(C:/xampp/htdocsC:/xampp/htdocs/modules/download/6694488-PHPUploadFile.zip): failed to open stream: Invalid argument in C:\xampp\htdocs\project\modules\download.inc.php on line 28

    Warning: filesize(): stat failed for C:/xampp/htdocs/modules/download/6694488-PHPUploadFile.zip in C:\xampp\htdocs\project\modules\download.inc.php on line 32`
    – HelpNeeder Nov 13 '14 at 06:23
  • Well, by the error message you can see that the path passed to `fopen()` is all borked up: `fopen(C:/xampp/htdocsC:/xampp/htdocs...` you may also want to replace the slash by escaped backslash `/` -> `\\ ` as in the example shown in the [docs for `fopen()`](http://php.net/manual/en/function.fopen.php) – Havelock Nov 13 '14 at 06:28
  • My mistake, I fixed that with similar results. – HelpNeeder Nov 13 '14 at 06:31