PHP Upload multiple images and check their file extensions prior -
i have following code upload file folder, depending on file input upload file renamed e.g. first input - picture renamed picture01.jpg, 5th input - picture renamed picture picture01.jpg, etc...
<form action="upload_file.php" enctype="multipart/form-data" method="post"> <p><input name="image01" type="file"> </p> <p><input name="image02" type="file"> </p> <p><input name="image03" type="file"> </p> <p><input name="image04" type="file"> </p> <p><input name="image05" type="file"> </p> <input type="submit" value="upload file"> </form>
this upload_file.php -
<?php $pic_names = array ( '01' => 'picture01', '02' => 'picture02', '03' => 'picture03', '04' => 'picture04', '05' => 'picture05', ); $folder = "temp_images/"; if(preg_match("/(jpg|jpeg|png|gif|mp3|mp4)/",$ext)) { // picture01 $image_path01 = $folder . $pic_names['01'] . '.jpg'; move_uploaded_file($_files['image01']['tmp_name'], $image_path01); // picture02 $image_path02 = $folder . $pic_names['02'] . '.jpg'; move_uploaded_file($_files['image02']['tmp_name'], $image_path02); // picture03 $image_path03 = $folder . $pic_names['03'] . '.jpg'; move_uploaded_file($_files['image03']['tmp_name'], $image_path03); // picture04 $image_path04 = $folder . $pic_names['04'] . '.jpg'; move_uploaded_file($_files['image04']['tmp_name'], $image_path04); // picture05 $image_path05 = $folder . $pic_names['05'] . '.jpg'; move_uploaded_file($_files['image05']['tmp_name'], $image_path05); echo 'uploaded successfully!'; } else { echo 'uploaded successfully!';} ?>
i check before file uploaded checked either 1 of following extensions exist prior uploading jpg, gif or png. if have extension not uploaded , error given.
the solution managed find when input name="image" type="file"> have same name e.g. in case name. how can case different names input?
i got follwoing error: php parse error: syntax error, unexpected t_else
though sure more parse error.
any suggestion or please?
update: amended code telling me 'wrong file type!' image format too.
the error in both string echo 'uploaded successfully!';{
position , content.
1) no statements allowed between }
, else
.
2) 'uploaded successfully!';{
contains {
superfluous.
update: 1 of quite ways process uploaded files. depends on $pic_names array op.
$pic_names = array ( '01' => 'picture01', '02' => 'picture02', '03' => 'picture03', '04' => 'picture04', '05' => 'picture05', ); $allowed_mime = array( 'image/jpeg' => 'jpg', 'image/pjpeg' => 'jpg', 'image/gif' => 'gif', 'image/png' => 'png', ); $upload_messages = array( upload_err_ok => ' uploaded successfully!', upload_err_ini_size => ' uploaded file exceeds maxumum size of ' . ini_get('upload_max_filesize') . '.', upload_err_partial => ' uploaded file partially uploaded.', upload_err_no_file => ' no file uploaded', upload_err_no_tmp_dir => ' missing temporary folder.', upload_err_cant_write => ' failed write file disk.', upload_err_extension => ' php extension stopped file upload.', 100 => ' wrong file type.', ); $folder = "temp_images/"; $error_message = $success_message = ''; $upload_counter = 0; foreach ($pic_names $key => $value) { if (isset($_files['image'.$key]) && !$_files['image'.$key]['error']) { $file_mime = getfilemime($_files['image'.$key]['tmp_name']); if ( in_array($file_mime, array_keys($allowed_mime)) ) { $new_path = $folder . $value . '.' . $allowed_mime[$file_mime]; if ( !move_uploaded_file($_files['image'.$key]['tmp_name'], $new_path) ) { $error_message .= ' image' . $key . ':' . $upload_messages[upload_err_cant_write]; } else { $upload_counter++; } } else { $error_message .= ' image' . $key . ':' . $upload_messages[100]; } } else { $error_message .= ' image' . $key . ':' . ($_files['image'.$key]['error'] ? $upload_messages[$_files['image'.$key]['error']] : $upload_messages[upload_err_no_file]); } } if ( $upload_counter == count($pic_names) ) { echo $upload_messages[upload_err_ok]; } else { echo 'loaded ' . $upload_counter . ' file' . ($upload_counter != 1 ? 's' : '') . '.' . $error_message; } function getfilemime ($file) { if (php_version >= '5.3.0') { $finfo = new finfo(fileinfo_mime_type); return $finfo->file($file); } else { return mime_content_type($file); } }
Comments
Post a Comment