hbphoto Posted September 6, 2012 Share Posted September 6, 2012 Hello: I have a form which uploads the photo name, description, client name, original size image, and thumbnail size image. I have a table which contains the following fields: photo_id, client_id, photo_name, description, image_original, image_thumb Can someone help me figure out a more efficient way to upload two images on the same form? Here is the code for the form. <form enctype="multipart/form-data" action="add_image.php" method="post" accept-charset="utf-8"> <input type="hidden" name="MAX_FILE_SIZE" value="10485760 " /> <label><strong>Image Name:</strong></label><?php create_form_input('name', 'text', $add_image_errors); ?> <br><br><br> <label><strong>Description:</strong></label><?php create_form_input('description', 'text', $add_image_errors); ?> <br><br><br> <label><strong>Client:</strong></label> <select name="client"<?php if (array_key_exists('client', $add_image_errors)) echo ' class="error"'; ?>> <option>Select One</option> <?php // Retrieve all the categories and add to the pull-down menu: $q = "SELECT client_id, CONCAT_WS(' ', first_name, last_name) FROM client ORDER BY last_name, first_name ASC"; $r = mysqli_query ($dbc, $q); while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) { echo "<option value=\"$row[0]\""; // Check for stickyness: if (isset($_POST['client_id']) && ($_POST['client_id'] == $row[0]) ) echo ' selected="selected"'; echo ">$row[1]</option>\n"; } ?> </select> <br><br><br> <label><strong>Original Image:</strong></label> <?php // Check for an error: if (array_key_exists('image', $add_image_errors)) { echo '<span class="error">' . $add_image_errors['image'] . '</span><input type="file" name="image" class="error"/>'; } else { // No error. echo '<input type="file" name="image" />'; // If the file exists (from a previous form submission but there were other errors), // store the file info in a session and note its existence: if (isset($_SESSION['image'])) { echo "<br />Currently '{$_SESSION['image']['file_name']}'"; } } // end of errors IF-ELSE. ?> <br><br><br> <label><strong>Thumbnail Image:</strong></label> <?php // Check for an error: if (array_key_exists('image_thumb', $add_image_errors)) { echo '<span class="error">' . $add_image_errors['image_thumb'] . '</span><input type="file" name="image_thumb" class="error"/>'; } else { // No error. echo '<input type="file" name="image_thumb" />'; // If the file exists (from a previous form submission but there were other errors), // store the file info in a session and note its existence: if (isset($_SESSION['image_thumb'])) { echo "<br />Currently '{$_SESSION['image_thumb']['file_name_thumb']}'"; } } // end of errors IF-ELSE. ?> <br><br><br> <label> </label><img src="Captcha/captcha/captchac_code.php" id="captcha"><br><br> <label> </label><input type="text" name="imgverify" value="" maxlength="100" size="10" /> <br><br> <button type="submit">Submit</button> </form> Here is the PHP code for the actual upload. When I tested the form, only one of the images was uploaded. With my limited PHP experience, I began by having two blocks of code. One for the original size and the other for the thumbnail size. The code is the same except the variables were changed to have _thumb to identify the thumbnails. ############ Check for the ORIGINAL sized image: ######################## if (is_uploaded_file ($_FILES['image']['tmp_name']) && ($_FILES['image']['error'] == UPLOAD_ERR_OK)) { $file = $_FILES['image']; $size = ROUND($file['size']/1024); // Validate the file size: if ($size > 99999999999) { $add_image_errors['image'] = 'The uploaded file was too large.'; } // Validate the file type: $allowed_mime = array ('image/gif', 'image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png'); $allowed_extensions = array ('.jpg', '.gif', '.png', 'jpeg'); $image_info = getimagesize($file['tmp_name']); $ext = substr($file['name'], -4); if ( (!in_array($file['type'], $allowed_mime)) || (!in_array($image_info['mime'], $allowed_mime) ) || (!in_array($ext, $allowed_extensions) ) ) { $add_product_errors['image'] = 'The uploaded file was not of the proper type.'; } // Move the file over, if no problems: if (!array_key_exists('image', $add_image_errors)) { // Create a new name for the file: $new_name = (string) sha1($file['name'] . uniqid('',true)); // Add the extension: $new_name .= ((substr($ext, 0, 1) != '.') ? ".{$ext}" : $ext); // Move the file to its proper folder but add _tmp, just in case: $dest = "../photography/uploads/$new_name"; if (move_uploaded_file($file['tmp_name'], $dest)) { // Store the data in the session for later use: $_SESSION['image']['new_name'] = $new_name; $_SESSION['image']['file_name'] = $file['name']; // Print a message: echo '<h4>The file has been uploaded!</h4>'; } else { trigger_error('The file could not be moved.'); unlink ($file['tmp_name']); } } // End of array_key_exists() IF. } elseif (!isset($_SESSION['image'])) { // No current or previous uploaded file. switch ($_FILES['image']['error']) { case 1: case 2: $add_image_errors['image'] = 'The uploaded file was too large.'; break; case 3: $add_image_errors['image'] = 'The file was only partially uploaded.'; break; case 6: case 7: case 8: $add_image_errors['image'] = 'The file could not be uploaded due to a system error.'; break; case 4: default: $add_image_errors['image'] = 'No file was uploaded.'; break; } // End of SWITCH. } // End of $_FILES IF-ELSEIF-ELSE. ############ Check for the THUMBNAIL sized image: ######################## if (is_uploaded_file ($_FILES['image_thumb']['tmp_name']) && ($_FILES['image_thumb']['error'] == UPLOAD_ERR_OK)) { $file = $_FILES['image_thumb']; $size = ROUND($file['size']/1024); // Validate the file size: if ($size > 99999999999) { $add_image_errors['image_thumb'] = 'The uploaded file was too large.'; } // Validate the file type: $allowed_mime = array ('image/gif', 'image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png'); $allowed_extensions = array ('.jpg', '.gif', '.png', 'jpeg'); $image_info = getimagesize($file['tmp_name']); $ext = substr($file['name'], -4); if ( (!in_array($file['type'], $allowed_mime)) || (!in_array($image_info['mime'], $allowed_mime) ) || (!in_array($ext, $allowed_extensions) ) ) { $add_product_errors['image_thumb'] = 'The uploaded file was not of the proper type.'; } // Move the file over, if no problems: if (!array_key_exists('image_thumb', $add_image_errors)) { // Create a new name for the file: $new_name_thumb = (string) sha1($file['name'] . uniqid('',true)); // Add the extension: $new_name_thumb .= ((substr($ext, 0, 1) != '.') ? ".{$ext}" : $ext); // Move the file to its proper folder but add _tmp, just in case: $dest = "../photography/uploads/$new_name_thumb"; if (move_uploaded_file($file['tmp_name'], $dest)) { // Store the data in the session for later use: $_SESSION['image_thumb']['new_name_thumb'] = $new_name_thumb; $_SESSION['image_thumb']['file_name_thumb'] = $file['name']; // Print a message: echo '<h4>The file has been uploaded!</h4>'; } else { trigger_error('The file could not be moved.'); unlink ($file['tmp_name']); } } // End of array_key_exists() IF. } elseif (!isset($_SESSION['image_thumb'])) { // No current or previous uploaded file. switch ($_FILES['image_thumb']['error']) { case 1: case 2: $add_image_errors['image_thumb'] = 'The uploaded file was too large.'; break; case 3: $add_image_errors['image_thumb'] = 'The file was only partially uploaded.'; break; case 6: case 7: case 8: $add_image_errors['image_thumb'] = 'The file could not be uploaded due to a system error.'; break; case 4: default: $add_image_errors['image_thumb'] = 'No file was uploaded.'; break; } // End of SWITCH. } // End of $_FILES IF-ELSEIF-ELSE. Thanks for the help Link to comment Share on other sites More sharing options...
rob Posted September 6, 2012 Share Posted September 6, 2012 You can't really expect someone to wade through all that code for you, you should just post salient code. Didn't margaux already post a solution to uploading multiple files in your other post http://www.larryullman.com/forums/index.php?/topic/1558-chapter-11-page-316-adding-products/ Link to comment Share on other sites More sharing options...
Edward Posted September 6, 2012 Share Posted September 6, 2012 Yeah you have to break down your code and show us the error. Link to comment Share on other sites More sharing options...
Larisa Posted September 9, 2012 Share Posted September 9, 2012 <html> <head> <title>multiple file upload </title> <script type="text/javascript"> function add_file_field(){ var container=document.getElementById('file_container'); var file_field=document.createElement('input'); file_field.name='images[]'; file_field.type='file'; container.appendChild(file_field); var br_field=document.createElement('br'); container.appendChild(br_field); } </script> </head> <body> <form action="mupload.php" method="post" enctype="multipart/form-data" name="mutiple_file_upload_form" id="mutiple_file_upload_form"> <h3>Multiple File Upload </h3> <div id="file_container"> <input name="images[]" type="file" /> <br /> </div> <a href="javascript:void(0);" onclick="add_file_field();">Add another</a><br /> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> This was taken from http://wintekweb.blogspot.com/2012/05/multiple-file-upload-php.html you better to visit it Link to comment Share on other sites More sharing options...
Recommended Posts