html - PHP success on file upload -
i'm sure there simple solution can't see.
i have form uploading stuff.
when script completes uses header('location: admin.php?success')
, if($_get['success']) { echo woohoo success }
type message before rest of script run.
the problem is want upload 2 files @ once can't because first part of script executed , nothing else. considered using boolean value set true or false , display message failed.
i'd able upload several files in succession , receive success message each one.
many thanks.
relevant php:
if(isset($_get['success']) && empty($_get['success'])){ echo '<h2>file upload successful! whoop!</h2>'; } else{ if(empty($_post) === false){ //check file has been uploaded if(isset($_files['mytrainingfile']) && !empty($_files['mytrainingfile']['tmp_name'])){ file stuff... if(in_array($fileext, $blacklist) === true){ $errors[] = "file type not allowed"; } } if(empty($errors) === true){ //run update move file stuff... } } $comments = htmlentities(trim($_post['comments'])); $category = htmlentities(trim($_post['category'])); $training->uploaddocument($filename, $category, $comments); header('location: admin.php?success'); exit(); } else if (empty($errors) === false) { //header('location: messageuser.php?msg=' .implode($errors)); echo '<p>' . implode('</p><p>', $errors) . '</p>'; }} } ?>
you need loop through $_files
super-global array , upload each file.
here's working example give better idea.
<?php $upload_dir= './uploads'; $num_uploads = 2; $max_file_size = 51200; $ini_max = str_replace('m', '', ini_get('upload_max_filesize')); $upload_max = $ini_max * 1024; $msg = 'please select files uploading'; $messages = array(); if(isset($_files['userfile']['tmp_name'])) { for($i=0; $i < count($_files['userfile']['tmp_name']);$i++) { if(!is_uploaded_file($_files['userfile']['tmp_name'][$i])) { $messages[] = 'no file uploaded'; } elseif($_files['userfile']['size'][$i] > $upload_max) { $messages[] = "file size exceeds $upload_max php.ini limit"; } elseif($_files['userfile']['size'][$i] > $max_file_size) { $messages[] = "file size exceeds $max_file_size limit"; } else { if(@copy($_files['userfile']['tmp_name'][$i],$upload_dir.'/'.$_files['userfile']['name'][$i])) { $messages[] = $_files['userfile']['name'][$i].' uploaded'; } else { $messages[] = 'uploading '.$_files['userfile']['name'][$i].' failed'; } } } } ?> <html> <head> <title>multiple file upload</title> </head> <body> <h3><?php echo $msg; ?></h3> <p> <?php if(sizeof($messages) != 0) { foreach($messages $err) { echo $err.'<br />'; } } ?> </p> <form enctype="multipart/form-data" action="<?php echo htmlentities($_server['php_self']);?>" method="post"> <input type="hidden" name="max_file_size" value="<?php echo $max_file_size; ?>" /> <?php $num = 0; while($num < $num_uploads) { echo '<div><input name="userfile[]" type="file" /></div>'; $num++; } ?> <input type="submit" value="upload" /> </form> </body> </html>
hope helps!
Comments
Post a Comment