Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you force the inputs into an array by using</p> <pre><code>&lt;input type='file' name='file[]' /&gt; &lt;input type='file' name='file[]' /&gt; &lt;input type='file' name='file[]' /&gt; </code></pre> <p>then use the following function I found from <a href="http://www.php.net/manual/en/features.file-upload.multiple.php#53240" rel="nofollow">file-upload.multiple.php</a></p> <pre><code>function reArrayFiles(&amp;$file_post) { $file_ary = array(); $file_count = count($file_post['name']); $file_keys = array_keys($file_post); for ($i=0; $i&lt;$file_count; $i++) { foreach ($file_keys as $key) { $file_ary[$i][$key] = $file_post[$key][$i]; } } return $file_ary; } </code></pre> <p>You will be able to loop through the array very easily, hardly noticing you dealing with multiple files.</p> <pre><code>if (isset ($_POST['upload'])) { extract ($_POST, EXTR_PREFIX_ALL, 'post'); $file_ary = reArrayFiles ($_FILES['file']); $error = 0; foreach ($file_ary as $file) { // Test a file is uploaded for each input if ($file['size'] == 0 || empty ($file['name'])) { $error = 1; break; // Note: If not all input slots are required to be filled, // replace the above with: // continue; // This will stop running this step of the loop here // and start the next one. } // Test file format if (!in_array ($file['type'], array ( 'image/jpg', 'image/png', 'image/jpeg', 'image/gif')) { $error = 2; break; } } if (!$error) { // True is returned if value is not 0; $newpath = 'photos/'.time(); foreach ($file_ary as $file) { $res = @move_uploaded_file($file['tmp_name'], $newpath.'-'.$file['name']); unlink ($file['tmp_name']); if (!$res) { $error = 3; break; } } } if (!$error) { $res = mysql_query(' ... insert query ... '); if (!$res) { $error = 4; } } if ($error) { // If you wanted to be super thorough, you could loop through // and make sure that if any files did get moved that they // were deleted. $msg = ""; switch ($error) { case 1: $msg = 'File not found.'; break; case 2: $msg = 'Incorrect file format.'; break; case 3: $msg = 'Error moving uploaded file.'; break; case 4: $msg = 'Database query failed. '.mysql_error(); break; default: $msg = 'Error'; } echo "&lt;h2 class='error'&gt;$msg&lt;/h2&gt;&lt;a href='sell.php'&gt;Try Again.&lt;/a&gt;"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload