Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]multiple files upload php mysql
wlodek_789
post
Post #1





Grupa: Zarejestrowani
Postów: 42
Pomógł: 0
Dołączył: 18.09.2013

Ostrzeżenie: (0%)
-----


Mam skrypt odpowiedzialny za przesyłanie jednego pliku i opisów. Plik jest przesyłany na serwer a informacje o nim wraz z opisem do bazy mysql. Chciałbym zrobić przesyłanie wielu plików ale nie wiem jak.
Z góry dziękuję za pomoc.

form
  1. <form enctype="multipart/form-data"
  2. action="upload_image.php" method="post">
  3. <input type="hidden"
  4. name="MAX_FILE_SIZE" value="524288">
  5.  
  6. <fieldset><legend>Select a JPEG or PNG
  7. image of 512KB or smaller to be
  8. uploaded:</legend>
  9.  
  10. <p><b>File:</b> <input type="file"
  11. name="filename" /></p>
  12. <p>
  13. <label for="caption">Caption:</label>
  14. <input type="text" name="caption" id="caption">
  15. </p>
  16.  
  17. <p><b>File2:</b> <input type="file"
  18. name="filename2" /></p>
  19. <p>
  20. <label for="caption2">Caption2:</label>
  21. <input type="text" name="caption" id="caption">
  22. </p>
  23.  
  24. </fieldset>
  25. <div align="center"><input type="submit"
  26. name="submit" value="Submit" /></div>
  27. <input type="hidden" name="submitted"
  28. value="TRUE" />
  29. </form>


-------------------------------------------------------------------------------------------

php

  1. <?php # Upload image
  2.  
  3. // Check if the form has been submitted:
  4. if (isset($_POST['submitted'])) {
  5.  
  6. // Check for an uploaded file:
  7. if (isset($_FILES['filename'])) {
  8.  
  9. // Validate the type. Should be JPEG or PNG.
  10. $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPEG', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
  11. if (in_array($_FILES['filename']['type'], $allowed)) {
  12.  
  13. // Move the file over.
  14. if (move_uploaded_file ($_FILES['filename']['tmp_name'], "../images/{$_FILES['filename']['name']}")) {
  15. echo '<p><em>The file has been uploaded!</em></p>';
  16. } // End of move... IF.
  17.  
  18. } else { // Invalid type.
  19. echo '<p class="error">Please upload a JPEG or PNG image.</p>';
  20. }
  21. } // End of isset($_FILES['upload']) IF.
  22.  
  23.  
  24. @$path = $_FILES["filename"]["name"];
  25. @$path = mysql_real_escape_string($path);
  26. @$type = $_FILES["filename"]["type"];
  27. @$size = $_FILES["filename"]["size"];
  28. @$nazwa = $_POST["caption"];
  29.  
  30. $query = "INSERT INTO photographs (";
  31. $query .= " filename, type, size, caption,";
  32. $query .= ") VALUES (";
  33. $query .= " '{$path}', '{$type}', '{$size}', '{$caption}'";
  34. $query .= ")";
  35. echo $query;
  36.  
  37. $result = mysqli_query($connection, $query);
  38. if ($result) {
  39. // Success
  40. echo " Success. ";
  41. } else {
  42. // Failure
  43. die(" Failure. " . mysqli_error($connection));
  44.  
  45.  
  46. // Check for an error:
  47.  
  48.  
  49. if ($_FILES['filename']['error'] > 0) {
  50. echo '<p class="error">The file could not be uploaded because: <strong>';
  51.  
  52. // Print a message based upon the error.
  53. switch ($_FILES['filename']['error']) {
  54. case 1:
  55. print 'The file exceeds the upload_max_filesize setting in php.ini.';
  56. break;
  57. case 2:
  58. print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
  59. break;
  60. case 3:
  61. print 'The file was only partially uploaded.';
  62. break;
  63. case 4:
  64. print 'No file was uploaded.';
  65. break;
  66. case 6:
  67. print 'No temporary folder was available.';
  68. break;
  69. case 7:
  70. print 'Unable to write to the disk.';
  71. break;
  72. case 8:
  73. print 'File upload stopped.';
  74. break;
  75. default:
  76. print 'A system error occurred.';
  77. break;
  78. } // End of switch.
  79.  
  80. print '</strong></p>';
  81.  
  82. } // End of error IF.
  83.  
  84. // Delete the file if it still exists:
  85.  
  86. if (file_exists ($_FILES['filename']['tmp_name']) && is_file($_FILES['filename']['tmp_name']) ) {
  87. unlink ($_FILES['filename']['tmp_name']);
  88. } // End of the submitted conditional.
  89. }
  90. }
  91.  
  92. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
wlodek_789
post
Post #2





Grupa: Zarejestrowani
Postów: 42
Pomógł: 0
Dołączył: 18.09.2013

Ostrzeżenie: (0%)
-----


zrobiłem jak według Twoich zaleceń i mam cos takiego ale coś jest nie tak ( @ - nie miało to żadnego znaczenia czy jest czy nie efekt jest taki sam)

  1. INSERT INTO photographs ( filename, type, size, nazwa ) VALUES ( 'carina_01.jpg', 'image/jpeg', '106767', '786') Success. INSERT INTO photographs ( filename, type, size, nazwa ) VALUES ( 'Catina_65_01.jpg', 'image/jpeg', '81895', '4675') Success. INSERT INTO photographs ( filename, type, size, nazwa ) VALUES ( '', '', '', '') Success. INSERT INTO photographs ( filename, type, size, nazwa ) VALUES ( '', '', '', '') Success. INSERT INTO photographs ( filename, type, size, nazwa ) VALUES ( '', '', '', '') Success. INSERT INTO photographs ( filename, type, size, nazwa ) VALUES ( '', '', '', '') Success.


w bazie mysql pojawia się 5 rekordów 2 z filename z linkami w filename2 nie ma nic w opisach nie ma nic w pozostałych 3 rekordach jest pusto.

A ja chciałbym żeby w jednym rekordzie pojawiały się link do zdjęć i opisy.

php
-----------------------
  1. <?php # Upload image
  2.  
  3. for($i=0; $i <= 5; $i++) {
  4.  
  5. // Check if the form has been submitted:
  6. if (isset($_POST['submitted'])) {
  7.  
  8. // Check for an uploaded file:
  9. if (isset($_FILES['filename'][$i])) {
  10.  
  11. // Validate the type. Should be JPEG or PNG.
  12. $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPEG', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
  13. if (in_array($_FILES['filename']['type'][$i], $allowed)) {
  14.  
  15. // Move the file over.
  16. if (move_uploaded_file ($_FILES['filename']['tmp_name'][$i], "../images/{$_FILES['filename']['name'][$i]}")) {
  17. echo '<p><em>The file has been uploaded!</em></p>';
  18. } // End of move... IF.
  19.  
  20. } else { // Invalid type.
  21. echo '<p class="error">Please upload a JPEG or PNG image.</p>';
  22. }
  23. } // End of isset($_FILES['upload']) IF.
  24.  
  25.  
  26. @$path = $_FILES["filename"]["name"][$i];
  27. @$path = mysql_real_escape_string($path);
  28. @$type = $_FILES["filename"]["type"][$i];
  29. @$size = $_FILES["filename"]["size"][$i];
  30. @$nazwa = $_POST["nazwa"][$i];
  31. //@$seria = $_POST["seria"];
  32. //@$kod = $_POST["kod"];
  33. //@$txt = $_POST["txt"];
  34. //@$visible = (int) $_POST["visible"];
  35.  
  36. // przyklad - by moc wpisac 'costam' - tylko txt, nie "file" i "int"
  37. $nazwa = mysqli_real_escape_string($connection, $nazwa);
  38. //$seria = mysqli_real_escape_string($connection, $seria);
  39. //$kod = mysqli_real_escape_string($connection, $kod);
  40. //$txt = mysqli_real_escape_string($connection, $txt);
  41.  
  42. $query = "INSERT INTO photographs (";
  43. $query .= " filename, type, size, nazwa ";
  44. $query .= ") VALUES (";
  45. $query .= " '{$path}', '{$type}', '{$size}', '{$nazwa}'";
  46. $query .= ")";
  47. echo $query;
  48.  
  49. $result = mysqli_query($connection, $query);
  50. if ($result) {
  51. // Success
  52. echo " Success. ";
  53. } else {
  54. // Failure
  55. die(" Failure. " . mysqli_error($connection));
  56.  
  57.  
  58. // Check for an error:
  59.  
  60.  
  61. if ($_FILES['filename']['error'][$i] > 0) {
  62. echo '<p class="error">The file could not be uploaded because: <strong>';
  63.  
  64. // Print a message based upon the error.
  65. switch ($_FILES['filename']['error'][$i]) {
  66. case 1:
  67. print 'The file exceeds the upload_max_filesize setting in php.ini.';
  68. break;
  69. case 2:
  70. print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
  71. break;
  72. case 3:
  73. print 'The file was only partially uploaded.';
  74. break;
  75. case 4:
  76. print 'No file was uploaded.';
  77. break;
  78. case 6:
  79. print 'No temporary folder was available.';
  80. break;
  81. case 7:
  82. print 'Unable to write to the disk.';
  83. break;
  84. case 8:
  85. print 'File upload stopped.';
  86. break;
  87. default:
  88. print 'A system error occurred.';
  89. break;
  90. } // End of switch.
  91.  
  92. print '</strong></p>';
  93.  
  94. } // End of error IF.
  95.  
  96. // Delete the file if it still exists:
  97.  
  98. if (file_exists ($_FILES['filename']['tmp_name'][$i]) && is_file($_FILES['filename']['tmp_name'][$i]) ) {
  99. unlink ($_FILES['filename']['tmp_name'][$i]);
  100. } // End of the submitted conditional.
  101. }
  102. }
  103. }
  104.  
  105. ?>



-------------------------------------
form

  1. <form enctype="multipart/form-data"
  2. action="photo_upload_x.php" method="post">
  3. <input type="hidden"
  4. name="MAX_FILE_SIZE" value="524288">
  5.  
  6. <fieldset><legend>Select a JPEG or PNG
  7. image of 512KB or smaller to be
  8. uploaded:</legend>
  9.  
  10. <p><b>File:</b> <input type="file"
  11. name="filename[]" /></p>
  12. <p>
  13. <label for="nazwa">nazwa:</label>
  14. <input type="text" name="nazwa[]" id="nazwa">
  15. </p>
  16.  
  17. <p><b>File2:</b> <input type="file"
  18. name="filename[]" /></p>
  19. <p>
  20. <label for="nazwa2">Nazwa2:</label>
  21. <input type="text" name="nazwa[]" id="nazwa2">
  22. </p>
  23.  
  24. </fieldset>
  25. <div align="center"><input type="submit"
  26. name="submit" value="Submit" /></div>
  27. <input type="hidden" name="submitted"
  28. value="TRUE" />
  29. </form>


Ten post edytował wlodek_789 11.10.2013, 17:53:23
Go to the top of the page
+Quote Post

Posty w temacie
- wlodek_789   [MySQL][PHP]multiple files upload php mysql   11.10.2013, 09:18:37
- - Turson   W formularzu HTML5 się kłania - multiple. [HTML] p...   11.10.2013, 12:57:51
- - Sephirus   W tym przypadku lepsze wydaje mi się kilka pól z p...   11.10.2013, 13:08:51
- - wlodek_789   dziękuję za odpowiedz:) zrobiłem tak jak pisaliści...   11.10.2013, 14:28:48
- - Turson   Cytatzrobiłem tak jak pisaliście ale nie działa Po...   11.10.2013, 14:38:11
- - wlodek_789   proszę to cały kod a i jeszcze w mysql mam coś tak...   11.10.2013, 14:47:00
- - Turson   Usuń @ bo wygłuszasz ew. błędy przy zmiennych [PH...   11.10.2013, 15:03:51
- - wlodek_789   zrobiłem jak według Twoich zaleceń i mam cos taki...   11.10.2013, 17:50:33
- - Turson   [PHP] pobierz, plaintext for($i=0; $i <= 5;...   11.10.2013, 17:56:15
- - wlodek_789   zmieniłem i efekt jest taki [PHP] pobierz, plaint...   11.10.2013, 18:02:21
- - Turson   [PHP] pobierz, plaintext for($i=1; $i ...   11.10.2013, 18:11:56
- - wlodek_789   w jednej tabeli photographs chciałbym mieć w jedny...   11.10.2013, 18:30:57
- - Turson   Czyli w sumie 2 tabele? Tabela 1: filename | type ...   11.10.2013, 18:48:28
- - wlodek_789   Nie jedna tablea w jednej tabeli która się nazywa ...   11.10.2013, 18:59:19
- - Turson   [PHP] pobierz, plaintext if (move_uploaded_fil...   11.10.2013, 20:05:11
- - wlodek_789   no dokładnie tak mam w kodzie [PHP] pobierz, plai...   11.10.2013, 21:03:24
- - Turson   Pytam bo wyzej napisales cos innego - ['filena...   11.10.2013, 21:19:58
- - wlodek_789   Udało się mogę przesyłać 2 pliki i umieszczać o ni...   11.10.2013, 22:28:30


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.12.2025 - 21:57