gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:56:36 24 4
gpt4 key购买 nike

我有以下文件上传脚本。它运行良好,我的文件上传完美。我能够使用它。但这个问题不是文件上传的问题,而是上传后处理查询的问题。它抛出错误 MySQL 服务器已消失当我上传小文件时,脚本按预期工作并且插入工作。只有当我上传大文件时,才会出现此问题。即使那些大文件也能成功保存,并且只有 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 秒。我不知道为什么会发生这种情况。 **

正如我上面所说

When I upload small files, the script works as expected. Only when I upload big files, this issues happens.

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

我的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

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

最佳答案

我认为发生的情况是您的文件上传需要很长时间,并且服务器由于 wait_timeout 设置过低而导致您超时。如果您在完成上传后和插入前立即连接,则“消失”问题可能会得到解决。因此,在带有插入的 mysql_query() 之前调用 dbConnect()

关于php - 文件上传 - MySQL 服务器已经消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37576117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com