Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just reworked your PHP code a bit. Especially you'll get more information about what's going wrong. Just try this code and read the following comments, which explain what happend, if you get one of the new error messages. Also read the NOTE part below, which explains why you probably can't access a file from PHP, even it's existing and is in the right directory.</p> <ol> <li>Using window.location.reload(); instead of document.location...</li> <li>I added an error()-function. You can add more HTML to it, so it's producing a page in the layout you want. And you could log the error to a local file, too. There is a private info parameter used to pass sensible information as database errors (can contain SQL) to the function. For productive use you shouldn't display that to the user. Instead you can log it into a file or only display it for privileged users (e.g. Administrators).</li> <li>Checks weather $id is set. Returns error() message if not; Could happen if session was not updated correctly.</li> <li>I added "$id = addslashes($id);" for security reasons. If your id could be set to values like $id = "' OR 1" (SQL-Injection) for example, you could get into trouble. If you are sure this can not happen, you can remove it.</li> <li>It checks the $result variable after the DB query. If e.g. your database connection wasn't established or the script cannot connect this will produce an error()-output that informs you. The same happens if you have an error in your SQL syntax, e.g. wrong table name.</li> <li>It's also checked weather a valid $row is fetched from the database. If there isn't a row returned your $id is problably wrong (there isn't such an entry in your database).</li> <li>I rewrote your string operations to $filepath = $rootpath . "/" . $uploader . "/" . $filename; where $rootpath is set before without "/" at the end; This is easier to read...</li> <li>Extensions and MIME-Types are now put into an array, instead of using a lot of "if-then"-blocks, that's easier to maintain. Also the code inside that blocks were similar... so we only need to write it once.</li> <li>A default MIME type (Content-Type:"application/octet-stream) is sent, if the file extension is not known.</li> <li>We check for file_exists() and output an error message, with $filename given to allow checking weather the path is correct...</li> </ol> <p>So here is the source code:</p> <pre><code>&lt;?php function error($message, $info = "") { echo "ERROR: $message&lt;br&gt;"; echo "PRIVATE-INFO: $info"; // probably you only want to log that into a file? exit; } // REFERSH IF FIRSTPASS IS LIVE if ($_SESSION["PASS1"] == "YES") { $_SESSION["PASS1"] = "no"; $_SESSION["PASS2"] = "YES"; echo "&lt;script&gt;window.location.reload();&lt;/script&gt;"; exit; } if ($_SESSION["PASS2"] == "YES") { // Grab reference data from session: $id = $_SESSION['passreference']; if (!$id) error("Internal Error ('id' not set)"); // Select file location from DB $id = addslashes($id); $query = "SELECT * from rightplace WHERE id = '$id'"; $result = mysql_query($query); if (!$result) error("DB-query execution error", mysql_error()); $row = mysql_fetch_array($result); mysql_free_result($result); if (!$row) error("File with ID '$id' was not found in DB."); $filename = $row['file']; $uploader = $row['uploader']; // Setting up download variables $rootpath = "/home/domain/aboveroot"; $filepath = $rootpath . "/" . $uploader . "/" . $filename; $ext = strtolower(end(explode('.', $filename))); // Serve the file download // List of known extensions and their MIME-types... $typelist = 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", "ppt" =&gt; "application/vnd.ms-powerpoint" ); // set default content-type $type = "application/octet-stream"; // for known extensions, assign specific content-type if (!isset($typelist[$ext])) $type = $typelist[$ext]; if (file_exists($filepath)) { header("Content-disposition: attachment; filename= '$filename'"); header("Content-type: $type"); readfile($filepath); } else { error("Error: File '$filepath' was not found!", $filepath); } } ?&gt; </code></pre> <h2>NOTES:</h2> <ol> <li><p>The file not found error can happen even the file exists. If this happens, this is most probably a security mechanism that prevents the PHP script to access files outside the HTML-root directory. For example php scripts could be executed in a "chrooted" environment, where the root directory "/" is mapped e.g. to "/home/username/". So if you want to access "/home/username/dir/file" you would need to write "/dir/file" in your PHP script. It can be even worse, if your root is set like "/home/username/html"; then you'll not be able to access directories below your "html" directory. To work around that, you can create a directory inside the HTML-root and put a file named ".htaccess" there. Write "DENY FROM ALL" in it, which prevents access to the directory by browser request (only scripts can access it). This works for apache servers only. But there are solutions like that for other server software too... More info on this can be found under: <a href="http://www.php.net/manual/en/ini.core.php#ini.open-basedir" rel="nofollow">http://www.php.net/manual/en/ini.core.php#ini.open-basedir</a> </p></li> <li><p>Another possibility is that your file access right (for uploaded files) are not set in a way, that your script is allowed to access them. With some security settings enabled (on a linux server), your PHP script can only access files owned by the same user as the "owner" set for the script file. After upload via "ftp" this is most probably the usersname of the ftp user. If edited on the shell, this will be the current users username. => But: Uploaded files are sometimes assigned to the user the webserver is running as (e.g. "www-data", "www-run" or "apache"). So find out which it is and assign your script to this owner.</p></li> <li>For file uploads you should use move_uploaded_file(...) which is explained here: www.php.net/manual/en/function.move-uploaded-file.php ; If you don't do this, the file access right may be wrong or you might not be able to access the file. </li> </ol>
    singulars
    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.
 

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