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%)
-----


proszę to cały kod a i jeszcze w mysql mam coś takiego id filename filename2 type size nazwa nazwa2

  1. <?php require_once("../../includes/functions_2.php"); ?>
  2. <?php require_once("../../includes/session.php"); ?>
  3. <?php require_once("../../includes/validation_functions.php"); ?>
  4. <?php confirm_logged_in(); ?>
  5. <?php
  6. //1. polaczenie z baza danych
  7. //polaczenie z baza danych + ustawienie kodowania na utf8
  8. $dbhost = "localhost";
  9. $dbuser = "root";
  10. $dbpass = "3edcvfr4";
  11. $dbname = "dw_bookstore";
  12. $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  13.  
  14. //sprawdzenie polaczenia
  15. if (mysqli_connect_errno()) {
  16. printf("Connect failed: %s\n", mysqli_connect_error());
  17. exit();
  18. }
  19.  
  20. //zmaiana znako na utf8
  21. if (!mysqli_set_charset($connection, "utf8")) {
  22. printf("Error loading character set utf8: %s\n", mysqli_error($connection));
  23. } else {
  24. //printf("Kodowanie ustawione na: %s\n", mysqli_character_set_name($connection));
  25. }
  26. ?>
  27.  
  28.  
  29.  
  30. <!doctype html>
  31. <html>
  32. <head>
  33. <meta charset="UTF-8">
  34. <title>Upload</title>
  35. <link href="../../stylesheets/main_2.css" rel="stylesheet" type="text/css" media="screen, projection" />
  36. </head>
  37.  
  38. <body>
  39. <div id="wrapper">
  40. <div id="header">
  41. <h2>Cennik: Panel Administracyjny</h2>
  42. </div>
  43. <div id="mainContent">
  44. <h1>Witaj w panelu administracyjnym: <?php echo htmlentities($_SESSION["username"]); ?></h1>
  45.  
  46. <?php # Upload image
  47.  
  48. for($i=0; $i <= 5; $i++) {
  49.  
  50. // Check if the form has been submitted:
  51. if (isset($_POST['submitted'])) {
  52.  
  53. // Check for an uploaded file:
  54. if (isset($_FILES['filename'])) {
  55.  
  56. // Validate the type. Should be JPEG or PNG.
  57. $allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPEG', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
  58. if (in_array($_FILES['filename']['type'], $allowed)) {
  59.  
  60. // Move the file over.
  61. if (move_uploaded_file ($_FILES['filename']['tmp_name'], "../images/{$_FILES['filename']['name']}")) {
  62. echo '<p><em>The file has been uploaded!</em></p>';
  63. } // End of move... IF.
  64.  
  65. } else { // Invalid type.
  66. echo '<p class="error">Please upload a JPEG or PNG image.</p>';
  67. }
  68. } // End of isset($_FILES['upload']) IF.
  69.  
  70.  
  71. @$path = $_FILES["filename"]["name"][$i];
  72. @$path = mysql_real_escape_string($path);
  73. @$type = $_FILES["filename"]["type"][$i];
  74. @$size = $_FILES["filename"]["size"][$i];
  75. @$nazwa = $_POST["nazwa"];
  76. //@$seria = $_POST["seria"];
  77. //@$kod = $_POST["kod"];
  78. //@$txt = $_POST["txt"];
  79. //@$visible = (int) $_POST["visible"];
  80.  
  81. // przyklad - by moc wpisac 'costam' - tylko txt, nie "file" i "int"
  82. $nazwa = mysqli_real_escape_string($connection, $nazwa);
  83. //$seria = mysqli_real_escape_string($connection, $seria);
  84. //$kod = mysqli_real_escape_string($connection, $kod);
  85. //$txt = mysqli_real_escape_string($connection, $txt);
  86.  
  87. $query = "INSERT INTO photographs (";
  88. $query .= " filename, type, size, nazwa ";
  89. $query .= ") VALUES (";
  90. $query .= " '{$path}', '{$type}', '{$size}', '{$nazwa}'";
  91. $query .= ")";
  92. echo $query;
  93.  
  94. $result = mysqli_query($connection, $query);
  95. if ($result) {
  96. // Success
  97. echo " Success. ";
  98. } else {
  99. // Failure
  100. die(" Failure. " . mysqli_error($connection));
  101.  
  102.  
  103. // Check for an error:
  104.  
  105.  
  106. if ($_FILES['filename']['error'] > 0) {
  107. echo '<p class="error">The file could not be uploaded because: <strong>';
  108.  
  109. // Print a message based upon the error.
  110. switch ($_FILES['filename']['error']) {
  111. case 1:
  112. print 'The file exceeds the upload_max_filesize setting in php.ini.';
  113. break;
  114. case 2:
  115. print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
  116. break;
  117. case 3:
  118. print 'The file was only partially uploaded.';
  119. break;
  120. case 4:
  121. print 'No file was uploaded.';
  122. break;
  123. case 6:
  124. print 'No temporary folder was available.';
  125. break;
  126. case 7:
  127. print 'Unable to write to the disk.';
  128. break;
  129. case 8:
  130. print 'File upload stopped.';
  131. break;
  132. default:
  133. print 'A system error occurred.';
  134. break;
  135. } // End of switch.
  136.  
  137. print '</strong></p>';
  138.  
  139. } // End of error IF.
  140.  
  141. // Delete the file if it still exists:
  142.  
  143. if (file_exists ($_FILES['filename']['tmp_name']) && is_file($_FILES['filename']['tmp_name']) ) {
  144. unlink ($_FILES['filename']['tmp_name']);
  145. } // End of the submitted conditional.
  146. }
  147. }
  148. }
  149.  
  150. ?>
  151. <form enctype="multipart/form-data"
  152. action="photo_upload_x.php" method="post">
  153. <input type="hidden"
  154. name="MAX_FILE_SIZE" value="524288">
  155.  
  156. <fieldset><legend>Select a JPEG or PNG
  157. image of 512KB or smaller to be
  158. uploaded:</legend>
  159.  
  160. <p><b>File:</b> <input type="file"
  161. name="filename[]" /></p>
  162. <p>
  163. <label for="nazwa">nazwa:</label>
  164. <input type="text" name="nazwa[]" id="nazwa">
  165. </p>
  166.  
  167. <p><b>File2:</b> <input type="file"
  168. name="filename[]" /></p>
  169. <p>
  170. <label for="nazwa2">Nazwa2:</label>
  171. <input type="text" name="nazwa[]" id="nazwa2">
  172. </p>
  173.  
  174. </fieldset>
  175. <div align="center"><input type="submit"
  176. name="submit" value="Submit" /></div>
  177. <input type="hidden" name="submitted"
  178. value="TRUE" />
  179. </form>
  180.  
  181. <?php
  182. // 5. Zamkniecie polaczenia
  183. mysqli_close($connection);
  184. ?>
  185. </div>
  186.  
  187. <div id="footer">
  188.  
  189. </div>
  190.  
  191. </body>
  192. </html>
  193. <?php if(isset($database)) { $database->close_connection(); } ?>
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 - 15:10