This is driving me crazy. Maybe someone can see something I can't and make me happy.
I have a form with two upload buttons. It needs to be this way (client requirement).
<fieldset>
<legend>SUBIR DOCUMENTOS </legend>
<span class="help-block">Aquí solo tienes que subir los documentos NO relacionados con un contrato. </span>
<div class="form-group">
<label for="inputFile1"> Documento A </label>
<input type="file" id="inputFile1" name="inputFile1">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo EGAR_MAXFILESIZE; ?>" />
</div>
<div class="form-group">
<label for="inputFile2"> Documento B </label>
<input type="file" id="inputFile2" name="inputFile2">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo EGAR_MAXFILESIZE; ?>" />
</div>
</fieldset>
Every thing works well except for the error message when the second file (inputFile2) exceeds the max size. In that case, the file is not uploaded (what is correct) but it displays the success message.
Here is the code:
include_once ('includes/functions.php');
if(!empty($_FILES)) {
$message_error = array();
$message_success = array();
foreach($_FILES as $file){
$file_name = $file['name'];
$file_temp_name = $file['tmp_name'];
$file_type = $file['type'];
$file_size = $file['size'];
$server_file= EGAR_UPLOADPATH . basename($file_name);
$ext = pathinfo($server_file,PATHINFO_EXTENSION);
$path_parts = pathinfo($file_name);
$allowedExtensions = array("pdf","doc","docx","rtf","txt", "gif", "png", "jpg", "jpeg");
if (!empty($file['name'])){
//move the file to the server
if ((in_array( $ext,$allowedExtensions)) && ($file_size <= EGAR_MAXFILESIZE)){
$server_file= EGAR_UPLOADPATH . $file_name; ///public_html/test-site-6/uploads
move_uploaded_file($file_temp_name,$server_file);
$success_upload= "$file_name subido correctamente";
array_push($message_success,$success_upload);
}else{
if($file_size > EGAR_MAXFILESIZE) {//1mb
$filesize= "es demasiado grande (máximo 1MB) ";
$error_upload ="$file_name $filesize <br/>";
array_push($message_error,$error_upload);
}
if(!in_array( $ext,$allowedExtensions)) {
$filetype=" es un tipo de archivo no válido";
$error_upload ="$file_name $filetype <br/>";
array_push($message_error,$error_upload);
}
}
}//if empty
}//end foreach
foreach($message_error as $msg) {
echo '<h3 class="error">'. $msg . '</h3>';
}
foreach($message_success as $msg) {
echo '<h3 class="success">'. $msg . '</h3>';
}
}
Any help will be much appreciated.