uploading images with the help of arrays and fetch errors
Posted
by
bonny
on Stack Overflow
See other posts from Stack Overflow
or by bonny
Published on 2012-04-10T17:19:42Z
Indexed on
2012/04/10
17:29 UTC
Read the original article
Hit count: 183
i use a script to upload a couple of images to a directory. the code works great in case of just one picture will be uploaded. if i like to upload two images or more and have an extension that is not accepted, the script will upload the one with the extension that is allowed to upload and shows the errormessage for the one who is not accepted. but the upload takes place. that's my first problem.
second problem will be: in case of an errormessage i would like to display a message in which it is said, which of the images will be not allowed. i do not know how to fetch this one that has an unaccepted ending into a variable that i can echo to the errormessage. here is the code i use:
if(!empty($_FILES['image']['tmp_name'])){
$allowed_extension = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');
foreach($_FILES['image']['name'] as $key => $array_value){
$file_name = $_FILES['image']['name'][$key];
$file_size = $_FILES['image']['size'][$key];
$file_tmp = $_FILES['image']['tmp_name'][$key];
$file_extension = strtolower(end(explode('.', $file_name)));
if (in_array($file_extension, $allowed_extension) === false){
$errors[] = 'its an unaccepted format in picture $variable_that_count';
continue;
}
if ($file_size > 2097152){
$errors[] = 'reached maxsize of 2MB per file in picture $variable_that_count';
}
if (count($errors) == 0){
$path = "a/b/c/";
$uploadfile = $path."/".basename($_FILES['image']['name'][$key]);
if (move_uploaded_file($_FILES['image']['tmp_name'][$key], $uploadfile)){
echo "success.";
}
}
}
}
hope it will be clear what i like to approach. if there is someone who could help out i really would appreciate. thanks a lot.
© Stack Overflow or respective owner