0

I am making an Ajax request to update a Mysql table. When I successfully upload a pdf file, the filename and filepath is updated in the table accordingly. The problem I am getting is this. When I upload a small file (414kb) the upload is successful and I am able to update my record. When the file is large (3.6Mb), the upload appears successful but the responseText I get from the Ajax request is a blank. I handled the php side with an IF/ELSE statement and therefore there should be no 'blank' response. I really do not understand where the problem is. Please see the code below.

My 'upload_max_filesize' is 32M.

JS:

 function completeHandler(event){
    document.getElementById('p_status').style.color = '#19A347';

    if(xmlhttp_file.readyState == 4 && xmlhttp_file.status == 200){
        if(xmlhttp_file.responseText == 'update_success'){
            console.log('congratulations. Success');
        }else{
            console.log('fail!');
            console.log('responsText: '+xmlhttp_file.responseText);
            console.log('xmlhttp_file.readyState: '+ xmlhttp_file.readyState);
            console.log('xmlhttp_file.status: '+ xmlhttp_file.status);

        }
    }
}

PHP:

 $move_success = move_uploaded_file($file_TmpPath, "$pdf_dir/$file");
        if($move_success){

            // ESCAPE CHARACTERS BEFORE INSERTION INTO _DB
            // INSERT 'filename', 'filename_uniq', 'filepath'
            $file_name = $mysqli->real_escape_string($file_name);
            $file_id = $mysqli->real_escape_string($file_id);
            $file = $mysqli->real_escape_string($file);
            $filepath = $mysqli->real_escape_string("$pdf_dir/$file");

            $sql = "UPDATE pdf_library SET filename='$file_name', filename_uniq='$file_id', filepath='$filepath' WHERE id='$idOfPost'";

            $result = $mysqli -> query($sql);
            if($result){
                echo 'update_success';
            }else{
                echo 'update_fail';
            }


        }else{
            echo 'move fail';
        }

When uploading a large pdf - Console:

    fail!
    new_home.js:1473 responsText: 
    new_home.js:1474 xmlhttp_file.readyState: 4
    new_home.js:1475 xmlhttp_file.status: 200
dave
  • 475
  • 6
  • 17
  • 1
    You will have to debug the request properly. Your server could be erroring. http://stackoverflow.com/a/21617685/2191572 – MonkeyZeus Sep 01 '15 at 19:16
  • check the sizes within php.ini of `upload_max_filesize` , `post_max_size`, `max_execution_time` and `max_input_time` etc. By default these values may be set too low for what you are trying to do... – Professor Abronsius Sep 01 '15 at 19:18
  • You may suffer other error situation. Read http://php.net/manual/en/features.file-upload.php please. – Joe Horn Sep 01 '15 at 19:22

2 Answers2

0

PHP limits the file size automatically that only certain sizes will be uploaded. It doesn't look like you have a check for the file size to be equal to or lower than the max file size allowed. The default is 2 MB and you have to change it in order to be able to upload more.

PHP change the maximum upload file size

Community
  • 1
  • 1
J. Han
  • 122
  • 1
  • 9
0

You should do an error debug on $_FILES or move_uploaded_file to get exactly what's occurring (although it likely seems you're running into your MAX_FILE_SIZE configuration as that'd be the first intuition if you are getting differing results on larger file sizes)

You can debug the $_FILES global variable by checking in $_FILES['filename']['error']

http://php.net/manual/en/features.file-upload.errors.php

q.Then
  • 2,743
  • 1
  • 21
  • 31