我有一个可以上传文件的页面。在我的本地主机上,一切都很好,但在共享主机上不起作用,当我加载页面时,它会崩溃并显示Error 500 - Internal Server Error
。
本地配置和主机上的配置之间唯一的不同是eAccelerator
,它在共享主机上默认启用。
我曾尝试在主机上创建php.ini
并禁用它,但到目前为止没有成功。在.htaccess
中,我放置了:
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/myuser/public_html/php.ini
</IfModule>
注意:myuser
不是我真正的用户。。我只是为了这里才改的。然后在php.ini
中,我把它放了下来,但在phpinfo()
上仍然显示它是启用的。
eaccelerator.enable 0
eaccelerator.optimizer 0
遗憾的是,任何地方都没有错误,所以我不知道这里到底发生了什么。cPanel中没有错误。文件所在的目录中没有错误。尝试使用error_reporting(E_ALL);
和ini_set('display_errors', 'on');
时没有错误。
当我删除这部分代码时,页面就会加载,没有错误。所以问题出在代码+eaccelator中。这是位于同一文件中HTML表单上方的代码。
if (array_key_exists("add", $_POST)) {
$pdo = Database::connect();
$allowedFileTypes = array('image/gif', 'image/jpeg', 'image/jpg', 'image/png', 'image/bmp');
$uploadFolder = "uploads/";
$totalFiles = 0;
$files = (object)[
'file_1' => (object)['name' => ''],
'file_2' => (object)['name' => ''],
'file_3' => (object)['name' => '']
];
$query = "INSERT INTO file_upload (
`file_title`, `file_program`, `file_subject`, `file_1`, `file_2`, `file_3`, `uploaded_by`, `upload_date`
) VALUES (
:file_title, :file_program, :file_subject, :file_1, :file_2, :file_3, :uploaded_by, NOW()
)";
$queryPrepare = $pdo->prepare($query);
foreach (array_keys($_FILES) as $file) {
$filesInCurrentInput = count(array_filter($_FILES[$file]['tmp_name']));
if (!$filesInCurrentInput) {
continue;
}
$totalFiles += $filesInCurrentInput;
for ($i = 0; $i < $filesInCurrentInput; $i++) {
if ($_FILES[$file]['error'][$i]) {
continue;
}
$type = $_FILES[$file]['type'][$i];
if (!in_array($type, $allowedFileTypes)) {
throw new Exception("File type not allowed.");
}
$oldName = $_FILES[$file]['name'][$i];
$getEndOfFile = explode(".", $oldName);
$extension = end($getEndOfFile);
$name = hash("md5", uniqid(rand(), true)) . '.' . $extension;
$temporaryFile = $_FILES[$file]['tmp_name'][$i];
if (!move_uploaded_file($temporaryFile, $uploadFolder.$name)) {
throw new Exception("An error occured while trying to upload a file.");
} else {
$files->$file->name .= $name . ',';
}
}
}
if (!$totalFiles) {
throw new Exception("You must attach files to the lesson.");
}
$title = htmlspecialchars($_POST['file_title']);
$file_program = htmlspecialchars($_POST['file_program']);
$file_subject = htmlspecialchars($_POST['file_subject']);
$uploaded_by = htmlspecialchars($_SESSION['user_username']);
$queryPrepare->bindValue(":file_title", $title);
$queryPrepare->bindValue(":file_program", $upload_program);
$queryPrepare->bindValue(":file_subject", $upload_subject);
$queryPrepare->bindValue(":file_1", trim($files->file_1->name, ','));
$queryPrepare->bindValue(":file_2", $files->file_2->name);
$queryPrepare->bindValue(":file_3", $files->file_3->name);
$queryPrepare->bindValue(":uploaded_by", $uploaded_by);
$queryPrepare->execute();
echo "<p class='bg-success text-center'>File added successfully.</p>";
}
该表单是一个只有几个字段的简单表单。。没有什么特别的。有什么帮助吗?
请更改此项:
$files = (object)[
'file_1' => (object)['name' => ''],
'file_2' => (object)['name' => ''],
'file_3' => (object)['name' => '']
];
对此并尝试重新加载页面:
$files = (object)array(
'file_1' => (object)array('name' => ''),
'file_2' => (object)array('name' => ''),
'file_3' => (object)array('name' => '')
);