jmall Posted May 22, 2019 Share Posted May 22, 2019 Hi, I bought both version of the book (1st, 2nd) and the electronic version of the second version too, awhile ago. In second book, I'm trying to modify the script (add_pdf) in the first site (selling virtual products) to add image instead of pdf, by using the script (add_other_products) in the second site example (selling physical product) Here, I upload my modified file (add_pdf). Everything goes fine when I upload an image, but for some reason I cannot view them. I keep getting the error from the script view_pdf (first site): "This page has been accessed in error." Do I need to change those values in the veiw_pdf script or something else? header('Content-type:application/image'); or header('Content-type:image/jpg'); ------------------------------------------------------------------------- // Send the content information: header('Content-type:application/pdf'); header('Content-Disposition:inline;filename="' . $row['file_name'] . '"'); $fs = filesize($file); header("Content-Length:$fs\n"); ------------------------------------------------------------------------------------ Please help me all, please Mr larry Ullman help. I'd like to be able to upload different file type, such as images(png, jpg, psd), video(mp4) and pdfs. Note: Yes, I also bought larry Ullman other books too. (php and mysql both version: 1st and second edition). This other book has a file upload with php and javascript but it's different. add_pdf.php Link to comment Share on other sites More sharing options...
Larry Posted May 23, 2019 Share Posted May 23, 2019 Yes, you do need to change the content-type header. It ought to match the content type of the file (i.e., of the image). You can use the https://www.php.net/manual/en/function.mime-content-type.php function to get that from the actual file on the server. Link to comment Share on other sites More sharing options...
jmall Posted May 23, 2019 Author Share Posted May 23, 2019 First off, thanks very much for your reply. What content-type should I put there for images? as you can see the code in the view_pdf script below, it didn't work. Please check the attached view_pdf script for to see if there's something incorrect there for me. Is there somewhere in the script do I need to modify in order to make it work? ------------------------------------------------------------------------------------------------- // Send the content information: header('Content-type:image/jpeg'); header('Content-Disposition:inline;filename="' . $row['file_name'] . '"'); $fs = filesize($file); header("Content-Length:$fs\n"); // Send the file: readfile ($file); exit(); --------------------------------------------------------------------------------------------------------------------------- Link to comment Share on other sites More sharing options...
Larry Posted May 23, 2019 Share Posted May 23, 2019 The content-type would differ based upon the type of image. This is why I suggested using the mime_content_type() function to dynamically get the MIME type and then provide that dynamically in the code. Link to comment Share on other sites More sharing options...
jmall Posted May 23, 2019 Author Share Posted May 23, 2019 Thanks so much for your feedback sir, This is good information you provide me, but I'm going to figure out how to put it in the code, I'm so novice about this. I think that I need to find some tutorial on youtube to teach me how to use that function in the script view_pdf. Link to comment Share on other sites More sharing options...
Larry Posted May 25, 2019 Share Posted May 25, 2019 Here is a somewhat similar example: https://github.com/LarryUllman/phpmysqlvqp-5ed/blob/master/ch11/show_image.php Link to comment Share on other sites More sharing options...
jmall Posted June 12, 2019 Author Share Posted June 12, 2019 Wow, thanks so much sir, I'm going to check this one. I hate myself for being away for your reply right now. I've been on a project and it's taking my time away. and I've already bought a similar download file code tamplate for the project. I might not use it if yours works. It seems it's in your book 5ed. I checked this book at barnes and nobles awhile ago, But I didn't buy it and I didn't check well. I've seen another one in your book 2nd edition which I bought awhile back, (php and javascript on chap. 11 extended topics), viewing images. I think that I might buy the 5ed, I can buy it directly from you or one of online book stores that I have membership with. I also have a peachpit account so I might buy it there if I can't get it from barnes and nobles right now. Thanks again sir. To me, you are a very serious author. I admire your support on this forum. Link to comment Share on other sites More sharing options...
Larry Posted June 12, 2019 Share Posted June 12, 2019 You're most welcome. Thanks for the nice words! Link to comment Share on other sites More sharing options...
jmall Posted June 13, 2019 Author Share Posted June 13, 2019 I've just purchased the book form peachpit.com Now I'm going to dig it since I read a few lines from it at barnes and noble yesterday. I should get this book 2 years ago, well I have so many computer books with similar examples. I'm not surprised by your books sir, you are solid with your product, money well spent. Link to comment Share on other sites More sharing options...
Larry Posted June 13, 2019 Share Posted June 13, 2019 Thanks! Link to comment Share on other sites More sharing options...
jmall Posted June 14, 2019 Author Share Posted June 14, 2019 I tried to make the script works with different file type but I'm still having trouble to make it work. I only not to view pdf file, but also images, other files like psd type of file photoshop, microsoft office type files as well. Here's the script that I'm having problem with below, only pdf would work. <?php // This pages retrieves and shows a PDF. // This script is created in Chapter 5. // Require the configuration before any PHP code as the configuration controls error reporting: require('./includes/config.inc.php'); // The config file also starts the session. // Require the database connection: require(MYSQL); // Assume invalid info: $valid = false; // Validate the PDF ID: if (isset($_GET['id']) && (strlen($_GET['id']) === 63) && (substr($_GET['id'], 0, 1) !== '.') ) { // Identify the file: $file = PDFS_DIR . $_GET['id']; // Check that the PDF exists and is a file: if (file_exists ($file) && (is_file($file)) ) { // Get the info: $q = 'SELECT id, title, description, file_name FROM pdfs WHERE tmp_name="' . escape_data($_GET['id'], $dbc) . '"'; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) === 1) { // OK! // Fetch the info: $row = mysqli_fetch_array($r, MYSQLI_ASSOC); // Indicate that the file reference is fine: $valid = true; // Only display the PDF to a user whose account is active: if (isset($_SESSION['user_not_expired'])) { // Bonus material! Referenced in Chapter 5. // Record this visit to the history table: // $q = "INSERT INTO history (user_id, type, pdf_id) VALUES ({$_SESSION['user_id']}, 'pdf', {$row['id']})"; // $r = mysqli_query($dbc, $q); // Send the content information: header('Content-type:application/pdf'); header('Content-Disposition:inline;filename="' . $row['file_name'] . '"'); $fs = filesize($file); header("Content-Length:$fs\n"); // Send the file: readfile ($file); exit(); } else { // Inactive account! // Display an HTML page instead: $page_title = $row['title']; include('./includes/header.html'); echo "<h1>$page_title</h1>"; // Change the message based upon the user's status: if (isset($_SESSION['user_id'])) { echo '<div class="alert"><h4>Expired Account</h4>Thank you for your interest in this content, but your account is no longer current. Please <a href="renew.php">renew your account</a> in order to access this file.</div>'; } else { // Not logged in. echo '<div class="alert">Thank you for your interest in this content. You must be logged in as a registered user to access this file.</div>'; } // Complete the page: echo '<div>' . htmlspecialchars($row['description']) . '</div>'; include('./includes/footer.html'); } // End of user IF-ELSE. } // End of mysqli_num_rows() IF. } // End of file_exists() IF. } // End of $_GET['id'] IF. // If something didn't work... if (!$valid) { $page_title = 'Error!'; include('./includes/header.html'); echo '<div class="alert alert-danger">This page has been accessed in error.</div>'; include('./includes/footer.html'); } ?> Link to comment Share on other sites More sharing options...
Larry Posted June 14, 2019 Share Posted June 14, 2019 You don't say how it's not working but my guess is the problem is b/c you're hard-coding the PDF application type in there. The content-type header indicates to the browser what type of file to expect. If you say to expect a PDF but send a Word doc, that's not going to work. Link to comment Share on other sites More sharing options...
jmall Posted June 14, 2019 Author Share Posted June 14, 2019 I post the original code from the book. But here's the code that I'm using, how I modify it in: <?php // This pages retrieves and shows a PDF. // This script is created in Chapter 5. // Require the configuration before any PHP code as the configuration controls error reporting: require('./includes/config.inc.php'); // The config file also starts the session. // Require the database connection: require(MYSQL); // Assume invalid info: $valid = false; // Validate the PDF ID: if (isset($_GET['id']) && (strlen($_GET['id']) === 63) && (substr($_GET['id'], 0, 1) !== '.') ) { // Identify the file: $file = PDFS_DIR . $_GET['id']; // Check that the PDF exists and is a file: if (file_exists ($file) && (is_file($file)) ) { // Get the info: $q = 'SELECT id, title, description, file_name FROM pdfs WHERE tmp_name="' . escape_data($_GET['id'], $dbc) . '"'; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) === 1) { // OK! // Fetch the info: $row = mysqli_fetch_array($r, MYSQLI_ASSOC); // Indicate that the file reference is fine: $valid = true; // Only display the PDF to a user whose account is active: if (isset($_SESSION['user_not_expired'])) { // Bonus material! Referenced in Chapter 5. // Record this visit to the history table: // $q = "INSERT INTO history (user_id, type, pdf_id) VALUES ({$_SESSION['user_id']}, 'pdf', {$row['id']})"; // $r = mysqli_query($dbc, $q); // Send the content information: $info = getimagesize($file); header("content-type: {$info['mime']}"); header('Content-Disposition:inline;filename="' . $row['file_name'] . '"'); $fs = filesize($file); header("Content-Length:$fs\n"); // Send the file: readfile ($file); exit(); } else { // Inactive account! // Display an HTML page instead: $page_title = $row['title']; include('./includes/header.html'); echo "<h1>$page_title</h1>"; // Change the message based upon the user's status: if (isset($_SESSION['user_id'])) { echo '<div class="alert"><h4>Expired Account</h4>Thank you for your interest in this content, but your account is no longer current. Please <a href="renew.php">renew your account</a> in order to access this file.</div>'; } else { // Not logged in. echo '<div class="alert">Thank you for your interest in this content. You must be logged in as a registered user to access this file.</div>'; } // Complete the page: echo '<div>' . htmlspecialchars($row['description']) . '</div>'; include('./includes/footer.html'); } // End of user IF-ELSE. } // End of mysqli_num_rows() IF. } // End of file_exists() IF. } // End of $_GET['id'] IF. // If something didn't work... if (!$valid) { $page_title = 'Error!'; include('./includes/header.html'); echo '<div class="alert alert-danger">This page has been accessed in error.</div>'; include('./includes/footer.html'); } ?> Link to comment Share on other sites More sharing options...
jmall Posted July 1, 2019 Author Share Posted July 1, 2019 at least can I get an answer? Link to comment Share on other sites More sharing options...
Larry Posted July 1, 2019 Share Posted July 1, 2019 I cannot give you any better answer without knowing how it's not working. What is it doing that it shouldn't be? What is it not doing that it should be doing? What debugging steps have you tried and what were the results? Just posting code and saying "I'm still having trouble to make it work" doesn't provide sufficient information. Link to comment Share on other sites More sharing options...
Recommended Posts