打开页面时PHP上传脚本导致错误500

时间:2016-04-18 05:33:23

标签: php

我有一个页面可以上传文件。在我的本地主机上,一切正常,但在共享主机上无法正常工作,当我加载页面时,它崩溃并显示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>";  
}    

表单很简单,几乎没有字段......没什么特别的。对此有何帮助?

1 个答案:

答案 0 :(得分:0)

请更改此内容:

$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' => '')
);