rhs49010 Posted August 22, 2013 Share Posted August 22, 2013 I have this basic upload script it uploads a file to a database assigns a id number and moves the file to a directory and then you can view the uploaded files with the file names linked for downloading. everything works like it is suppose to but it wont download. I set var dump after the isset function in the download_files script so I know the view_files script is passing the correct id from the file name, but I keep getting the predefined error message....The file could not be located on the server. We apologize for any inconvenience. <?php # add_file.php // This page allows users to upload files to the server. // Set the page title and include the HTML header. $page_title = 'Upload a File'; include ('include/header.html'); $counter = 3; // Number of files to allow for. if (isset($_POST['submitted'])) { // Handle the form. require_once ('mysql_connect.php'); // Connect to the database. for ($i = 0; $i < $counter; $i++) { // Handle each uploaded file. // Create index names to refer to the proper upload and description. $filename = 'uploads' . $i; $description = 'description' . $i; // Check for a file. if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) { // Check for a description (not required). if (!empty($_POST[$description])) { $d = "'" . escape_data($_POST [$description]) . "'"; } else { $d = 'NULL'; } // Add the record to the dataabase. $query = "INSERT INTO uploads (file_name, file_size, file_type, description) VALUES ('{$_FILES[$filename]['name']}',{$_FILES [$filename]['size']},'{$_FILES [$filename]['type']}', $d)"; $result = mysql_query ($query); if ($result) { // Return the upload_id from the database. $upload_id = mysql_insert_id(); // Move the file over. if( move_uploaded_file($_FILES[$filename]['tmp_name'], 'uploads/'.$upload_id)) { echo '<p>File number ' . ($i + 1) . ' has been uploaded!</p>'; } else { // File could not be moved. echo '<p><font color="red">File number ' . ($i + 1) . ' could not be moved.</font></p>'; // Remove the record from the database. $query = "DELETE FROM uploads WHERE upload_id = $upload_id"; $result = mysql_query ($query); // Add more detailed error reporting, if desired. } } else { // If the query did not run OK. echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>'; // Print the query and invoke the mysql_error() function to debug. } } // End of if (isset($the_file)... } // End of FOR loop. mysql_close(); // Close the database connection. } ?> <form enctype="multipart/form-data" action="add_file.php" method="post"> <fieldset><legend>Fill out the form to upload a file:</legend> <input type="hidden" name="MAX_FILE_SIZE" value="5242880" /> <?php // Create the inputs. for ($i = 0; $i < $counter; $i++) { echo '<p><b>File:</b> <input type="file" name="uploads' . $i . '" /></p> <p><b>Description:</b> <textarea name="description' . $i . '" cols="40" rows="5"></textarea></p><br /> '; } ?> </fieldset> <input type="hidden" name="submitted" value="TRUE" /> <div align="center"><input type="submit" name="submit" value="Submit" /></div> </form> <?php include ('include/footer.html'); ?> <?php # view_files.php // This page displays the files uploaded to the server. // Set the page title and include the HTML header. $page_title = 'View Files'; include ('include/header.html'); require_once ('mysql_connect.php'); $first = TRUE; // Initialize the variable. // Query the database. $query = "SELECT upload_id, file_name, ROUND(file_size/1024) AS fs, description, DATE_FORMAT(date_entered, '%M %e, %Y') AS d FROM uploads ORDER BY date_entered DESC"; $result = mysql_query ($query); // Display all URLs. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // If this is the first record, create the table header. if ($first) { echo '<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center"> <tr> <td align="left" width="20%"><font size="+1">File Name</font></td> <td align="left" width="40%"><font size="+1">Description</font></td> <td align="center" width="20%"><font size="+1">File Size</font></td> <td align="left" width="20%"><font size="+1">Upload Date</font></td> </tr>'; $first = FALSE; // Once record has been returned. } // End of $first IF. // Display each record. echo " <tr> <td align=\"left\"><a href=\"download_file.php?uid={$row['upload_id']}\">{$row['file_name']}</a></td> <td align=\"left\">" . stripslashes($row ['description']) . "</td> <td align=\"center\">{$row ['fs']}kb</td> <td align=\"left\">{$row ['d']}</td> <td align=\"left\"><a href=\"edit_file.php?uid={$row['upload_id']}\">edit</a></td> </tr>\n"; } // End of while loop. // If no records were diplayed... if ($first) { echo '<div align="center">There are currently no files to be viewed.</div>'; } else { echo '</table>'; // Close the table. } mysql_close(); // Close the database connetion. include ('include/footer.html'); ?> <?php # download_file.php // This page handles file downloads through headers. // Check for an upload_id. if (isset($_GET['uid'])) { $uid = (int) $_GET['uid']; var_dump($uid); } else { // Big problem! $uid = 0; } if ($uid > 0) { // OK to proceed! require_once ('mysql_connect.php'); // Connect to the dataabase. // Get the information for this file. $query = "SELECT 'upload_id','file_name', 'file_size', 'file_type' FROM 'uploads' WHERE upload_id = $uid"; $result = mysql_query ($query); list ($fn, $fs, $ft) = mysql_fetch_array ($result, MYSQL_NUM); mysql_close(); // Close the database connection. // Determine the file name on the server. $the_file = "/uploads $uid"; // Check if it exists. if (file_exists ($the_file)) { // Send the file. header ("content-description: File Transfer"); header ("Content-disposition: attachment;filename=$fn"); header ("Content-Length:$fs"); header ("Content-Type: $ft"); readfile ($the_file); } else { // File doesn't exist. $page_title = 'File Download'; include ('include/header.html'); echo '<p><font color="red">The file could not be located on the server. We apologize for any inconvenience.</font></p>'; include ('include/footer.html'); } } else { // No valid upload ID. $page_title = 'File Download'; include ('include/header.html'); echo '<p><font color="red">Please select a valid file to download. </font></p>'; include ('include/footer.html'); } ?> any Ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/ Share on other sites More sharing options...
fastsol Posted August 22, 2013 Share Posted August 22, 2013 Looks like you're missing a / in the path where you define the var $the_file on the download page. Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446216 Share on other sites More sharing options...
kicken Posted August 22, 2013 Share Posted August 22, 2013 In addition, you almost certainly do not want the / at the beginning of the path in $the_file. That would indicate from the root of the filesystem which generally is not correct. Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446232 Share on other sites More sharing options...
rhs49010 Posted August 22, 2013 Author Share Posted August 22, 2013 You guys are correct, good eyes. Now the file are being read but not downloading, Example... If I upload a jpg or a pdf It will display a bunch of gibberish or if I upload a txt file it will display the content of the file in the browser. So for some reason it's not initiating the download window in the browser, I'm assuming it's a problem with the headers. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446348 Share on other sites More sharing options...
jazzman1 Posted August 22, 2013 Share Posted August 22, 2013 (edited) If the images (files) are stored in the filesystem is easy. Try that (tested many times) <?php // just so you know it is broken error_reporting(-1); if (!empty($_GET['uid'])) { $uid = intval($_GET['uid']); $sql = "SELECT 'upload_id','file_name', 'file_size', 'file_type' FROM 'uploads' WHERE upload_id = $uid"; // the result of the query $result = mysql_query($sql) or die("Invalid query: " . mysql_error()); $row = mysql_fetch_assoc($result) or die(mysql_error()); $the_file = '/uploads'.'/'.$row['file_name']; // dump this result to be sure that everything is fine header('Content-Description: File Transfer'); header("Content-type: " . $row['file_type']); header('Content-Disposition: attachment; filename=' . basename($the_file)); header('Content-Length: ' . filesize($the_file)); readfile($the_file); mysql_close($link); //close connection } PS: The header('Content-Length: ' . filesize($the_file)) you could replace with - header('Content-Length: ' . $row['file_size']); You have an error here - FROM 'uploads' Edited August 22, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446361 Share on other sites More sharing options...
jazzman1 Posted August 22, 2013 Share Posted August 22, 2013 Every column or table name should be enclosed with back ticks (if you want it) and every value with single quotes except the values with type of integer! So, that is totally wrong: $sql = "SELECT 'upload_id','file_name', 'file_size', 'file_type' FROM 'uploads' WHERE upload_id = $uid"; Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446371 Share on other sites More sharing options...
AbraCadaver Posted August 22, 2013 Share Posted August 22, 2013 (edited) Nevermind. Edited August 22, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446372 Share on other sites More sharing options...
fastsol Posted August 22, 2013 Share Posted August 22, 2013 Here is a reference on what should be used in the headers for file download. http://amecms.com/article/PHP-Force-File-Download-With-File-Whitelist Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446387 Share on other sites More sharing options...
rhs49010 Posted August 23, 2013 Author Share Posted August 23, 2013 If the images (files) are stored in the filesystem is easy. Try that (tested many times) <?php // just so you know it is broken error_reporting(-1); if (!empty($_GET['uid'])) { $uid = intval($_GET['uid']); $sql = "SELECT 'upload_id','file_name', 'file_size', 'file_type' FROM 'uploads' WHERE upload_id = $uid"; // the result of the query $result = mysql_query($sql) or die("Invalid query: " . mysql_error()); $row = mysql_fetch_assoc($result) or die(mysql_error()); $the_file = '/uploads'.'/'.$row['file_name']; // dump this result to be sure that everything is fine header('Content-Description: File Transfer'); header("Content-type: " . $row['file_type']); header('Content-Disposition: attachment; filename=' . basename($the_file)); header('Content-Length: ' . filesize($the_file)); readfile($the_file); mysql_close($link); //close connection } PS: The header('Content-Length: ' . filesize($the_file)) you could replace with - header('Content-Length: ' . $row['file_size']); You have an error here - FROM 'uploads' Thank you for your reply! I created a new download file using your script, had to change the .... $the_file = '/uploads'.'/'.$row['file_name']; to... $the_file = '/uploads'.'/'.$row['upload_id']; before it would read the file unfortunately it still displays the same way gibberish with jpg or pdf or the content of a text file. so I tried googling this.... header('Content-Disposition: attachment; filename=' . basename($the_file)); do you know anything about this? Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446417 Share on other sites More sharing options...
jazzman1 Posted August 23, 2013 Share Posted August 23, 2013 Add this and give me the result back: $the_file = '/uploads'.'/'.$row['file_name']; // dump this result to be sure that everything is fine echo $the_file; exit; Are you sure that you have an upload directory inside web root? The easiest way: if (!is_dir('/upload')) { echo "True"; } else { echo "False"; } Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446422 Share on other sites More sharing options...
rhs49010 Posted August 23, 2013 Author Share Posted August 23, 2013 Add this and give me the result back: $the_file = '/uploads'.'/'.$row['file_name']; // dump this result to be sure that everything is fine echo $the_file; exit; Are you sure that you have an upload directory inside web root? The easiest way: if (!is_dir('/upload')) { echo "True"; } else { echo "False"; } Trueint(108) ‰PNG IHDR Xšv‚psRGB®ÎégAMA±üaÿºIDATx^ì½ ^U™îÿìþµó}§·œôJ !=ôÐ¥( ˆ`/ˆýÎXÆ23Nsœ{gu¼3÷ªÿ™±_u,£"*(ˆ H -!õôþõÝÿo‡C˜ )¼¿Ãf·µ×^kí}ržg¯AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA„ç Õׂ ‚ €¥K—v.\¸0†¡7cÆŒOÌ In the upper left you'll see True with the var dump int(108) which is the id assigned to the file when uploaded, the other stuff this a sample of the page full of gibberish. That telling me it's reading the file and displaying it in the browser. This is a small text file, Trueint(113) 734-1730-0901 TIRE 20 X 8 X 8 CARLISLE. What has me baffled is I have another script that's similar to this one but that one works. I haven't had time to compare the two. probably later tonight I'll have some time to play around with it. I'll post them both if you want to scratch your head over it. It's bizarre. Thanks for your time. Later Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446490 Share on other sites More sharing options...
jazzman1 Posted August 23, 2013 Share Posted August 23, 2013 (edited) Few quetions, before to continue the battle 1. Where do you upload the images (binary files) to -> "/uploads/" or to -> "uploads/" directory? Do you know the difference between them? 2. How do you store the name of the file to your database column, with or without extension name (i.e - name.jpg or just "name") 3. Why do you want to reach the binary file with its ID, (i.e /uploads/file_id)? 4. This means, there is no a directory with this name ( made a typo yesterday night) if (!is_dir('/upload')) { echo "True"; } change to if (!is_dir('/uploads')) { echo "True"; } Edited August 23, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446498 Share on other sites More sharing options...
rhs49010 Posted August 24, 2013 Author Share Posted August 24, 2013 Few quetions, before to continue the battle 1. Where do you upload the images (binary files) to -> "/uploads/" or to -> "uploads/" directory? Do you know the difference between them? 2. How do you store the name of the file to your database column, with or without extension name (i.e - name.jpg or just "name") 3. Why do you want to reach the binary file with its ID, (i.e /uploads/file_id)? 4. This means, there is no a directory with this name ( made a typo yesterday night) if (!is_dir('/upload')) { echo "True"; } change to if (!is_dir('/uploads')) { echo "True"; } Okay the easiest way for me to answer your question is this image http://www.lrn.computerizedgraphics.com/screen_shots.jpg 1. Where do you upload the images (binary files) to -> "/uploads/" or to -> "uploads/" directory? Do you know the difference between them? There's a difference? "/uploads/upload_id" (see image for more detail) The other script I'm tinkering with stores the entire file name( name.jpg) in the files directory without a unique ID 2. How do you store the name of the file to your database column, with or without extension name (i.e - name.jpg or just "name") "name.jpg or name.pdf etc." 3. Why do you want to reach the binary file with its ID, (i.e /uploads/file_id)? The tutorial made me do it "It's home work gone awry" One thing I hate about tutorials they teach you how but not way one technique over an another, or when to use [] or {} or () ( fortunately I have a decent training and reference guide) and the php community for the most part is very friendly and helpful! 4. This means, there is no a directory with this name ( made a typo yesterday night) yeah I seen that "But I can stare right at my typos and never see them " I really appreciate all your help! Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446513 Share on other sites More sharing options...
mac_gyver Posted August 24, 2013 Share Posted August 24, 2013 the gibberish is the raw binary data, which means your code is finding, reading, and outputting the file, but the header's aren't working.you need to also set php's display_errors to ON, in addition to setting error_reporting, to see what php errors are being detected - ini_set("display_errors", "1"); Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446516 Share on other sites More sharing options...
jazzman1 Posted August 24, 2013 Share Posted August 24, 2013 Um...it should be work. I don't see any reason why that happens.The gibberish output is normal because you're dumping the image before to set any headers to it. Can you show us all updated code for this moment. Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446530 Share on other sites More sharing options...
rhs49010 Posted August 24, 2013 Author Share Posted August 24, 2013 (edited) <?php # add_file.php // This page allows users to upload files to the server. // Set the page title and include the HTML header. $page_title = 'Upload a File'; include ('include/header.html'); $counter = 3; // Number of files to allow for. if (isset($_POST['submitted'])) { // Handle the form. require_once ('mysql_connect.php'); // Connect to the database. for ($i = 0; $i < $counter; $i++) { // Handle each uploaded file. // Create index names to refer to the proper upload and description. $filename = 'uploads' . $i; $description = 'description' . $i; // Check for a file. if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) { // Check for a description (not required). if (!empty($_POST[$description])) { $d = "'" . escape_data($_POST [$description]) . "'"; } else { $d = 'NULL'; } // Add the record to the dataabase. $query = "INSERT INTO uploads (file_name, file_size, file_type, description) VALUES ('{$_FILES[$filename]['name']}',{$_FILES [$filename]['size']},'{$_FILES [$filename]['type']}', $d)"; $result = mysql_query ($query); if ($result) { // Return the upload_id from the database. $upload_id = mysql_insert_id(); // Move the file over. if( move_uploaded_file($_FILES[$filename]['tmp_name'], 'uploads/'.$upload_id)) { echo '<p>File number ' . ($i + 1) . ' has been uploaded!</p>'; } else { // File could not be moved. echo '<p><font color="red">File number ' . ($i + 1) . ' could not be moved.</font></p>'; // Remove the record from the database. $query = "DELETE FROM uploads WHERE upload_id = $upload_id"; $result = mysql_query ($query); // Add more detailed error reporting, if desired. } } else { // If the query did not run OK. echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>'; // Print the query and invoke the mysql_error() function to debug. } } // End of if (isset($the_file)... } // End of FOR loop. mysql_close(); // Close the database connection. } ?> <form enctype="multipart/form-data" action="add_file.php" method="post"> <fieldset><legend>Fill out the form to upload a file:</legend> <input type="hidden" name="MAX_FILE_SIZE" value="5242880" /> <?php // Create the inputs. for ($i = 0; $i < $counter; $i++) { echo '<p><b>File:</b> <input type="file" name="uploads' . $i . '" /></p> <p><b>Description:</b> <textarea name="description' . $i . '" cols="40" rows="5"></textarea></p><br /> '; } ?> </fieldset> <input type="hidden" name="submitted" value="TRUE" /> <div align="center"><input type="submit" name="submit" value="Submit" /></div> </form> <?php include ('include/footer.html'); ?> <?php # view_files.php // This page displays the files uploaded to the server. // Set the page title and include the HTML header. $page_title = 'View Files'; include ('include/header.html'); require_once ('mysql_connect.php'); $first = TRUE; // Initialize the variable. // Query the database. $query = "SELECT upload_id, file_name, ROUND(file_size/1024) AS fs, description, DATE_FORMAT(date_entered, '%M %e, %Y') AS d FROM uploads ORDER BY date_entered DESC"; $result = mysql_query ($query); // Display all URLs. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // If this is the first record, create the table header. if ($first) { echo '<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center"> <tr> <td align="left" width="20%"><font size="+1">File Name</font></td> <td align="left" width="40%"><font size="+1">Description</font></td> <td align="center" width="20%"><font size="+1">File Size</font></td> <td align="left" width="20%"><font size="+1">Upload Date</font></td> </tr>'; $first = FALSE; // Once record has been returned. } // End of $first IF. // Display each record. echo " <tr> <td align=\"left\"><a href=\"download_file.php?uid={$row['upload_id']}\">{$row['file_name']}</a></td> <td align=\"left\">" . stripslashes($row ['description']) . "</td> <td align=\"center\">{$row ['fs']}kb</td> <td align=\"left\">{$row ['d']}</td> <td align=\"left\"><a href=\"edit_file.php?uid={$row['upload_id']}\">edit</a></td> </tr>\n"; } // End of while loop. // If no records were diplayed... if ($first) { echo '<div align="center">There are currently no files to be viewed.</div>'; } else { echo '</table>'; // Close the table. } mysql_close(); // Close the database connetion. include ('include/footer.html'); ?> <?php # download_file.php // This page handles file downloads through headers. // Check for an upload_id. error_reporting(-1); if (!empty($_GET['uid'])) { $uid = intval($_GET['uid']); } else { // Big problem! $uid = 0; } if ($uid > 0) { // OK to proceed! require_once ('mysql_connect.php'); // Connect to the dataabase. $sql = "SELECT `upload_id`,`file_name`, `file_size`, `file_type` FROM `uploads` WHERE upload_id = $uid"; // the result of the query $result = mysql_query($sql) or die("Invalid query: " . mysql_error()); $row = mysql_fetch_assoc($result) or die(mysql_error()); $the_file = 'uploads'.'/'.$row['upload_id']; // dump this result to be sure that everything is fine if (!is_dir('/uploads')) { echo "True"; } else { echo "False"; } if (file_exists ($the_file)) { var_dump($uid); header ('content-type: application/octet-stream'); // IT MAKES NO DIFFERENCE IF THIS "content -type" IS COMMENTED OUT OR NOT. header('Content-Description: File Transfer'); header("Content-type: " . $row['file_type']); header('Content-Disposition: attachment; filename=' . basename($the_file)); header('Content-Length: ' . $row['file_size']); readfile($the_file); ini_set("display_errors", "1"); // mysql_close($link); //close connection //THIS IS THROWING A ERROR }else { // File doesn't exist. $page_title = 'File Download'; include ('include/header.html'); echo '<p><font color="red">The file could not be located on the server. We apologize for any inconvenience.</font></p>'; include ('include/footer.html'); } } else { // No valid upload ID. $page_title = 'File Download'; include ('include/header.html'); echo '<p><font color="red">Please select a valid file to download. </font></p>'; include ('include/footer.html'); } ?> Edited August 24, 2013 by rhs49010 Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446570 Share on other sites More sharing options...
jazzman1 Posted August 24, 2013 Share Posted August 24, 2013 (edited) Add next lines of code immediately after the $the_file variable. $the_file = 'uploads'.'/'.$row['upload_id']; // dump this result to be sure that everything is fine echo '<img src="$the_file" />'; exit; PS: Assuming this file is binary! If everything is fine, you will get an image on the page. Also, how did you get binary data string through var_dump(), I am surprise too There is totally wrong somewhere in your scripts but I'm not sure exactly where. Can you post the link to this tutorial? Edited August 24, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446577 Share on other sites More sharing options...
rhs49010 Posted August 24, 2013 Author Share Posted August 24, 2013 Add next lines of code immediately after the $the_file variable. $the_file = 'uploads'.'/'.$row['upload_id']; // dump this result to be sure that everything is fine echo '<img src="$the_file" />'; exit; PS: Assuming this file is binary! If everything is fine, you will get an image on the page. Also, how did you get binary data string through var_dump(), I am surprise too There is totally wrong somewhere in your scripts but I'm not sure exactly where. Can you post the link to this tutorial? I added the echo to the script and now the only thing on the page is a small black square with an X The var dump displays the ID assigned at upload and all the gibberish after that. EXAMPLE... True (the var dump int(108) ) ‰PNG IHDR Xšv‚psRGB®ÎégAMA±üaÿºIDATx^ì½ ^U™îÿìþµó}§·œôJ !=ôÐ¥( ˆ`/ˆýÎXÆ23Nsœ{gu¼3÷ªÿ™±_u,£"*(ˆ H -!õôþõÝÿo‡C˜ )¼¿Ãf·µ×^kí}ržg¯AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA„ç Õׂ ‚ €¥K—v.\¸0†¡7cÆŒOÌ The tutorial is an online training course I login to www.e-careers.com " support pretty much non existent" you get what you pay for I guess. (Groupon deal) Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446586 Share on other sites More sharing options...
mac_gyver Posted August 24, 2013 Share Posted August 24, 2013 why did you put the ini_set("display_errors", "1"); statement, that would have turned on the display of php errors, after the point where you are running the php code in question? Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446587 Share on other sites More sharing options...
jazzman1 Posted August 24, 2013 Share Posted August 24, 2013 I think, I've got the answer You are assigning the binary file to integer. Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446590 Share on other sites More sharing options...
mac_gyver Posted August 24, 2013 Share Posted August 24, 2013 kids, seriously, it's the readfile() statement that is reading and outputting the gibberish/actual file contents. the only thing that is being output by the var_dump() statement is the int(108). Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446591 Share on other sites More sharing options...
jazzman1 Posted August 24, 2013 Share Posted August 24, 2013 (edited) kids, seriously, it's the readfile() statement that is reading and outputting the gibberish/actual file contents. the only thing that is being output by the var_dump() statement is the int(108). No, you are wrong and the problem is here: $the_file = 'uploads'.'/'.$row['upload_id']; // dump this result to be sure that everything is fine The $row['upload_id'] should have an integer type because is not quoted! That's the problem, I think. Ignore the answer, I've tested that's no the problem. Edited August 25, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446596 Share on other sites More sharing options...
rhs49010 Posted August 25, 2013 Author Share Posted August 25, 2013 No, you are wrong and the problem is here: $the_file = 'uploads'.'/'.$row['upload_id']; // dump this result to be sure that everything is fine The $row['upload_id'] should have an integer type because is not quoted! That's the problem, I think. Can you give an example that might work? maybe... $the_file = 'uploads'.'/'.$row[$uid]; I'll try this for grins Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446598 Share on other sites More sharing options...
jazzman1 Posted August 25, 2013 Share Posted August 25, 2013 (edited) Try to reach the file statically: $the_file = 'uploads/108'; // dump this result to be sure that everything is fine echo '<img src='.$the_file.' />'; exit; Edited August 25, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446600 Share on other sites More sharing options...
rhs49010 Posted August 25, 2013 Author Share Posted August 25, 2013 Okay check this out modified script with error message <?php # download_file.php // This page handles file downloads through headers. // Check for an upload_id. error_reporting(-1); if (!empty($_GET['uid'])) { $uid = intval($_GET['uid']); } else { // Big problem! $uid = 0; } if ($uid > 0) { // OK to proceed! require_once ('mysql_connect.php'); // Connect to the dataabase. $sql = "SELECT `upload_id`,`file_name`, `file_size`, `file_type` FROM `uploads` WHERE upload_id = $uid"; // the result of the query $result = mysql_query($sql) or die("Invalid query: " . mysql_error()); $row = mysql_fetch_assoc($result) or die(mysql_error()); $the_file = 'uploads'.'/'.$row[$uid]; // dump this result to be sure that everything is fine ini_set("display_errors", "1"); if (!is_dir('/uploads')) { echo "True"; } else { echo "False"; } if (file_exists ($the_file)) { var_dump($uid); //header ('content-type: application/octet-stream'); header('Content-Description: File Transfer'); header("Content-type: " . $row['file_type']); header('Content-Disposition: attachment; filename=' . basename($the_file)); header('Content-Length: ' . $row['file_size']); readfile($the_file); // mysql_close($link); //close connection }else { // File doesn't exist. $page_title = 'File Download'; include ('include/header.html'); echo '<p><font color="red">The file could not be located on the server. We apologize for any inconvenience.</font></p>'; include ('include/footer.html'); } } else { // No valid upload ID. $page_title = 'File Download'; include ('include/header.html'); echo '<p><font color="red">Please select a valid file to download. </font></p>'; include ('include/footer.html'); } ?> I moved the ini set now I'm getting this error message... Trueint(108)Warning: Cannot modify header information - headers already sent by (output started at /home1/ecosolu1/public_html/lrn/download_file.php:33) in /home1/ecosolu1/public_html/lrn/download_file.php on line 40 WHAT? Quote Link to comment https://forums.phpfreaks.com/topic/281450-download-file-glitch/#findComment-1446602 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.