Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Zepsute pliki po daniu do downloadu i ściągnięciu
Forum PHP.pl > Forum > Przedszkole
kuba_pilach
Witam, mam download i upload na stronie, który uploaduje pliki do bazy danych mysql.
I download, dzięki któremu mam listę plików w mysql i mam możliwość ściągnięcia chcianego pliku.
I teraz przed uploadem, plik w pdf działa. Po uploadzie i downloadzie tego pliku, plik jest zepsuty...
Wie ktoś o co może chodzić?
nekomata
Trochę mało napisałeś , ja bym sprawdził po kolei czy :
1.Plik ma ten sam format (końcówka .pdf w twoim przypadku, możesz mieć wyłączone wyświetlanie tego , w opcjach folderów możesz znaleźć opcje.)
2.Sprawdź rozmiar pliku.
Jeśli obydwa są takie same jak w pliku który uploadowałeś , nie widzę powodu aby plik był zepsuty.
kuba_pilach
Plik jest zepsuty, bo jak próbowałem odpalić tym samym programem, to pisało, że plik jest zepsuty i nie chciał odpalić...
Uploudowałem także plik .php i urwał mi kawałek (duży ;]) kodu...
nekomata
Zapodaj kod , nie jesteśmy czarodziejami tongue.gif , w ciemno mogę strzelić tyle że dajesz zły header dla danego pliku(Content-type:application/pdf , itd.).
kuba_pilach
Oto kody:
upload.php
  1. <!DOCTYPE html>
  2. <head>
  3. <title>MySQL file upload example</title>
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5. </head>
  6. <body>
  7. <form action="index.php?go=add_file" method="post" enctype="multipart/form-data">
  8. <input type="file" name="uploaded_file"><br>
  9. <input type="submit" value="Upload file">
  10. </form>
  11. <p>
  12. <a href="index.php?go=download">See all files</a>
  13. </p>
  14. </body>
  15. </html>


add_file.php
  1. <?php
  2. // Check if a file has been uploaded
  3. if(isset($_FILES['uploaded_file'])) {
  4. // Make sure the file was sent without errors
  5. if($_FILES['uploaded_file']['error'] == 0) {
  6. // Connect to the database
  7. include 'connection.php';
  8.  
  9. // Gather all required data
  10. $name = mysql_real_escape_string($_FILES['uploaded_file']['name']);
  11. $mime = mysql_real_escape_string($_FILES['uploaded_file']['type']);
  12. $data = mysql_real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
  13. $size = intval($_FILES['uploaded_file']['size']);
  14.  
  15. // Create the SQL query
  16. $query = "
  17. INSERT INTO `file` (
  18. `name`, `mime`, `size`, `data`, `created`
  19. )
  20. VALUES (
  21. '{$name}', '{$mime}', {$size}, '{$data}', NOW()
  22. )";
  23.  
  24. // Execute the query
  25. $result = mysql_query($query);
  26.  
  27. // Check if it was successfull
  28. if($result) {
  29. echo 'Success! Your file was successfully added!';
  30. }
  31. else {
  32. echo 'Error! Failed to insert the file'
  33. . "<pre>{mysql_error}</pre>";
  34. }
  35. }
  36. else {
  37. echo 'An error accured while the file was being uploaded. '
  38. . 'Error code: '. intval($_FILES['uploaded_file']['error']);
  39. }
  40.  
  41. }
  42. else {
  43. echo 'Error! A file was not sent!';
  44. }
  45.  
  46. // Echo a link back to the main page
  47. echo '<p>Click <a href="index.php?go=upload.php">here</a> to go back</p>';
  48. ?>
  49.  


download.php
  1. <?php
  2. // Connect to the database
  3. include 'connection.php';
  4.  
  5. // Query for a list of all existing files
  6. $sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`';
  7. $result = mysql_query($sql);
  8.  
  9. // Check if it was successfull
  10. if($result) {
  11. // Make sure there are some files in there
  12. if(mysql_num_rows($result) == 0) {
  13. echo '<p>There are no files in the database</p>';
  14. }
  15. else {
  16. // Print the top of a table
  17. echo '<table width="100%">
  18. <tr>
  19. <td><b>Name</b></td>
  20. <td><b>Mime</b></td>
  21. <td><b>Size (bytes)</b></td>
  22. <td><b>Created</b></td>
  23. <td><b>&nbsp;</b></td>
  24. </tr>';
  25.  
  26. // Print each file
  27. while($row = mysql_fetch_assoc($result)) {
  28. echo "
  29. <tr>
  30. <td>{$row['name']}</td>
  31. <td>{$row['mime']}</td>
  32. <td>{$row['size']}</td>
  33. <td>{$row['created']}</td>
  34. <td><a href='index.php?go=get_file&amp;id={$row['id']}'>Download</a></td>
  35. </tr>";
  36. }
  37.  
  38. // Close table
  39. echo '</table>';
  40. }
  41.  
  42.  
  43. }
  44. else
  45. {
  46. echo 'Error! SQL query failed:';
  47. echo "<pre>{mysql_error}</pre>";
  48. }
  49.  
  50. ?>


get_file.php
  1. <?php
  2. // Make sure an ID was passed
  3. if(isset($_GET['id'])) {
  4. // Get the ID
  5. $id = intval($_GET['id']);
  6.  
  7. // Make sure the ID is in fact a valid ID
  8. if($id <= 0) {
  9. die('The ID is invalid!');
  10. }
  11. else {
  12. // Connect to the database
  13. include 'connection.php';
  14.  
  15.  
  16. // Fetch the file information
  17. $query = "
  18. SELECT `mime`, `name`, `size`, `data`
  19. FROM `file`
  20. WHERE `id` = {$id}";
  21. $result = mysql_query($query);
  22.  
  23. if($result) {
  24. // Make sure the result is valid
  25. if(mysql_num_rows($result) == 1) {
  26. // Get the row
  27. $row = mysql_fetch_assoc($result);
  28.  
  29. // Print headers
  30. header("Content-Type: ". $row['mime']);
  31. header("Content-Length: ". $row['size']);
  32. header("Content-Disposition: attachment; filename=". $row['name']);
  33.  
  34. // Print data
  35. echo $row['data'];
  36. }
  37. else {
  38. echo 'Error! No image exists with that ID.';
  39. }
  40.  
  41. }
  42. else {
  43. echo "Error! Query failed: <pre>{mysql_error}</pre>";
  44. }
  45. }
  46. }
  47. else {
  48. echo 'Error! No ID was passed.';
  49. }
  50. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.