文件上传 - MySQL服务器已经消失

时间:2016-06-01 18:22:14

标签: php mysql file-upload

我有以下文件上传脚本。它工作得很好,我的文件上传完美。我能够使用它。但是这个问题不是关于文件上传,而是上传后正在处理的查询。它抛出错误MySQL server has gone away当我上传小文件时,脚本按预期工作,插入正常。只有当我上传大文件时,才会出现这种问题。即使这些大文件也能成功保存,错误只发生在MySQL Insert

<?php
include('utils.php');
$result = array();
log_event("info", "Upload script initiated...");
log_event("info", "POST: ".json_encode($_POST, JSON_PRETTY_PRINT));
if(!isset($_FILES['file']['name'])){
    $result['error'] = "File not present";  echo json_encode($result); exit();
}
if(isset($_POST['auth'])){
    $is_valid = cookie_login($_POST['auth']);
    if($is_valid){
        log_event("info", "Authenticated for uploading.");
        if(isset($_POST['id'])){
            $exist = get_project($_POST['id']);
            if(!$exist){
                $result['error'] = "Project not specified";
            } else{
                log_event("info", "Project loaded. Analysing files for upload.");
                log_event("info", "FILES: ".json_encode($_FILES, JSON_PRETTY_PRINT));
                $file_name = $_FILES['file']['name'][0];
                $file_size = $_FILES['file']['size'][0];
                $file_tmp = $_FILES['file']['tmp_name'][0];
                $file_type = $_FILES['file']['type'][0];
                $file_name = mysql_real_escape_string($file_name);
                $file_ext=$ext = pathinfo($file_name, PATHINFO_EXTENSION);
                //print_r($_FILES['file']);

                $token = md5($file_name. $file_size.$file_tmp.$file_type. time() . rand(0, 99999) . rand(0, 99999));
                log_event("info", "Token Generated: $token");
                $filePath = "files/".$token;
                log_event("info", "Movable file path named after token: $filePath");
                if(!move_uploaded_file($file_tmp, $filePath)){
                    $result['error'] = "File Copying Error";
                    log_event("error", "File copying error");
                } else {
                    $sql = mysql_query("INSERT INTO files (name, path, package_name, token, username) VALUES ('$file_name', '$filePath', '$exist[package_name]', '$token', '$is_valid')");
                    if(!$sql){
                        $result['error'] = "Internal Server Error. Contact Administrator.";
                        log_event("error", mysql_error());
                    }
                }


            }
        } else {
            $result['error'] = "Project not specified";
        }
    } else {
        $result['error'] = "Authenticaton Failure";
    }
} else {
    $result['error'] = "Authenticaton Failure";
}

echo json_encode($result);

为了分析错误,我创建了一个log_event()方法,只记录事件到文件的内容。这些是结果。

[Wednesday 1st of June 2016 09:08:42 PM][info][http://www.example.com/bucket/upload.php] cookie_login(db64f68dee27eb08d29117c7da678f81): Success 
[Wednesday 1st of June 2016 09:08:42 PM][info][http://www.example.com/bucket/upload.php] Project loaded. Analysing files for upload. 
[Wednesday 1st of June 2016 09:08:42 PM][info][http://www.example.com/bucket/upload.php] FILES: {
    "file": {
        "name": [
            "Sibidharan_Software_Engineer_Android 3.zip"
        ],
        "type": [
            "application\/zip"
        ],
        "tmp_name": [
            "\/tmp\/phpTJJcQz"
        ],
        "error": [
            0
        ],
        "size": [
            97226470
        ]
    }
} 
[Wednesday 1st of June 2016 09:08:42 PM][info][http://www.example.com/bucket/upload.php] Token Generated: 599293ad3d30fd0d981f967df9d0f61f 
[Wednesday 1st of June 2016 09:08:42 PM][info][http://www.example.com/bucket/upload.php] Movable file path named after token: files/599293ad3d30fd0d981f967df9d0f61f 
[Wednesday 1st of June 2016 09:09:05 PM][error][http://www.example.com/bucket/upload.php] MySQL server has gone away 

如果您看到最后2个进程之间的时间,则处理查询花了将近23秒。我不知道为什么会这样。 **

正如我上面所说

  

当我上传小文件时,脚本按预期工作。只有我   上传大文件,这个问题就发生了。

**此外,问题不在于我的本地主机,而只在于实时服务器。

我的PHP_MAX_UPLOAD设置为128M,上传的文件大小约为98M,您可以在日志中看到。

更新 - 以下是我连接数据库的方式

$db = NULL;
$logs_enabled = "yes";

function dbConnect(){

    $DB_SERVER = "localhost";
    $DB_USER = "root";
    $DB_PASSWORD = "";
    $DB = "bucket";

    $GLOBALS['db'] = mysql_pconnect($DB_SERVER,$DB_USER,$DB_PASSWORD);

    if (!$GLOBALS['db']){
        log_event("error", "mysql_pconnect(): ".mysql_error());
        return false;
    }
    if($GLOBALS['db']){
        log_event("success", "mysql_pconnect(): Connection obtained");
        $check = mysql_select_db($DB,$GLOBALS['db']);
        if($check){
            log_event("success", "mysql_pconnect(): Database selected");
             mysql_query ("set character_set_client='utf8'"); 
             mysql_query ("set character_set_results='utf8'"); 
             mysql_query ("set collation_connection='utf8_general_ci'");
        } else {
            log_event("error", "mysql_pconnect(): Couldn't select database. Error: ".mysql_error());
        }
        return true;
    }
}

这是在utils.php文件中完成的,该文件包含在上面的脚本中。

更新2 正如Sasha Pachev所说,我尝试在查询之前放置`dbConnect(),然后得到以下日志。

[Thursday 2nd of June 2016 12:41:57 AM][info][http://www.example.com/bucket/upload.php] Project loaded. Analysing files for upload. 
[Thursday 2nd of June 2016 12:41:57 AM][info][http://www.example.com/bucket/upload.php] FILES: {
    "file": {
        "name": [
            "Sibidharan_Software_Engineer_Android 3.zip"
        ],
        "type": [
            "application\/zip"
        ],
        "tmp_name": [
            "\/tmp\/php3Gtac3"
        ],
        "error": [
            0
        ],
        "size": [
            97226470
        ]
    }
} 
[Thursday 2nd of June 2016 12:41:57 AM][info][http://www.example.com/bucket/upload.php] Token Generated: f2131640ca97468b62e4ab2b2eed25f9 
[Thursday 2nd of June 2016 12:41:57 AM][info][http://www.example.com/bucket/upload.php] Movable file path named after token: files/f2131640ca97468b62e4ab2b2eed25f9 
[Thursday 2nd of June 2016 12:42:20 AM][success][http://www.example.com/bucket/upload.php] mysql_pconnect(): Connection obtained 
[Thursday 2nd of June 2016 12:42:20 AM][error][http://www.example.com/bucket/upload.php] mysql_pconnect(): Couldn't select database. Error: MySQL server has gone away 
[Thursday 2nd of June 2016 12:42:20 AM][error][http://www.example.com/bucket/upload.php] MySQL server has gone away

结果:错误仍然存​​在。

1 个答案:

答案 0 :(得分:0)

我认为发生的事情是您的文件上传需要很长时间,而且由于dbConnect()的设置较低,服务器会将您停用。如果您在完成上传后立即连接并且在插入之前立即连接,则“离开”问题可能会得到解决。因此,在mysql_query()之前使用插入方式调用armeabi-v7a