Alert script does not work

Asked

Viewed 206 times

0

I have a perfectly functioning file upload code, but Alert in Else condition is not only working IF Alert.

Follows the code:

if(isset($_FILES['allfiles'])){//verifica o input file, caso estiver setado irá preparar as variaveis e caso o arquivo seja maior que 15mb irá mostrar erro.
   $errors= array();
   $file_name = $_FILES['allfiles']['name'];
   $namefile = $_POST ['allfiles'];
   $file_size = $_FILES['allfiles']['size'];
   $file_tmp = $_FILES['allfiles']['tmp_name'];
   $file_type= $_FILES['allfiles']['type'];
   $ext = pathinfo($file_name, PATHINFO_EXTENSION);
   $pasta1 = $_POST ['pasta1'];
   $pasta2 = $_POST ['pasta2'];
   if($file_size > 15097152){
      $errors[]='O arquivo nao pode exceder 15mb';
   } 


   if(empty($errors)==true){//caso não haja erro irá mover o arquivo para o diretório selecionado.
    if (empty ($namefile)==true){
        if (empty ($pasta1)==false){
            move_uploaded_file($file_tmp,"pasta/".$ID."/pasta1/".$file_name);
        }
        if (empty ($pasta2) ==false){
            move_uploaded_file($file_tmp,"pasta/".$ID."/pasta2/".$file_name);
        }
    }
    if (empty ($namefile)==false){
        if (empty ($pasta1)==false){
            move_uploaded_file($file_tmp,"pasta/".$ID."/pasta1/".$file_name);
            rename ("pasta/".$ID."/pasta1/".$file_name, "pasta/".$ID."/pasta1/".$namefile.".".$ext);
        }
        if (empty ($pasta2) ==false){
            move_uploaded_file($file_tmp,"pasta/".$ID."/pasta2/".$file_name);
            rename ("pasta/".$ID."/pasta2/".$file_name, "pasta/".$ID."/pasta2/".$namefile.".".$ext);
        }
    }
   echo "<script>alert('Arquivo enviado com sucesso!')</script>";
   }else{
   echo "<script>alert('Arquivo deve ser menor que 15mb!')</script>";
   }
}

OBS : I already checked if the problem was the variable $errors but even putting in the IF of $file_size still does not work, I believe there is some conflict in php. I have already tested the code on another blank page and it hasn’t worked yet, the only thing that happens when I try to upload a file bigger than 15mb is a Reload on the page, which so far I don’t understand because I have no reference to a refresh in the code. Does anyone have any idea what might be causing this problem?

  • The problem is not clear, especially when you say that "is working perfectly, but does not work". You apparently have two alerts: one on if, another in the else. The alert of if works, but of else No? You are aware that only one will run at a time, right? If so, in which situation exactly does it not work? And can you improve code indentation? It’s really hard to understand when a if begins and ends...

  • Exactly. Else’s Alert does not work, this Else belongs to the IF in which the condition is the empty $errors variable, where it only receives a value if the file is greater than 15 Mb, the file upload does not also occur what is right, but I would like a warning that this did not occur and I am not able to solve it in any way.

  • Inserts an echo "test";Exit; into your Else and see if it appears. Sometimes the problem isn’t Alert, maybe it’s not entering Else.

  • He hasn’t returned anything either. but the problem is not in Else exactly, but in something else because it had put the script inside if($file_size > 15097152)? } and also didn’t work, I believe there is something related to trying to upload large files. maybe an overload?

  • See in your php.ini how the body limit size of a request is, it must be less than 15mb, then it won’t even execute the code. Are the options post_max_size and upload_max_filesize

  • I changed both to 50 and now I can not even load the page, would have some value ideal to put?

  • What value was before? The default value is 20M. You need to restart the server as well to take effect.

  • yes, after restarting the server I could not more. I will test with 20

  • Now the server is not even starting, I don’t see sense, I put the post_max_size=20 and upload_max_filesize=20M

  • I made a reboot and now it’s back to the problem of not loading the page.

  • Solved by adding M to post_max_size=20M

  • Problem solved, if you can answer the question to end the post

Show 7 more comments

1 answer

1


When the page does not even load after uploading a very large file, you need to check that the POST body limit and the file size limit for uploading in PHP is not too low. You can check these values in the php.ini file:

post_max_size: http://php.net/post-max-size

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
post_max_size = 20M

upload_max_filesize: http://php.net/upload-max-filesize

; Maximum allowed size for uploaded files.
upload_max_filesize = 20M

Remembering that it is necessary to put the megabyte "M", otherwise the values will be read as bytes. It is also necessary to note that the post_max_size should never be less than the upload_max_filesize, and if you want to send more than one file at a time, for example 3 files of 20mb, post_max_size would need to be 60mb.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.