PHP文件上传限制图像超过20kb

时间:2014-05-20 11:34:03

标签: php forms file-upload

我已经使用PHP创建了一个图像上传,其想法是图像将保存到目录以及数据库的路径,这是非常标准的。问题是它不会节省超过20kb的任何东西。我已将php.ini文件中的max upload和post max size增加到10M,并且还将大小设置为<功能200000kb,但没有区别。有人可以告诉我,我现在已经把这个头撞了几天了:(

文件上传功能(基于W3Schools的示例)

function upload_file(){
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["page_main_image"]["name"]);
$extension = end($temp);
if ((($_FILES["page_main_image"]["type"] == "image/gif")
        || ($_FILES["page_main_image"]["type"] == "image/jpeg")
        || ($_FILES["page_main_image"]["type"] == "image/jpg")
        || ($_FILES["page_main_image"]["type"] == "image/pjpeg")
        || ($_FILES["page_main_image"]["type"] == "image/x-png")
        || ($_FILES["page_main_image"]["type"] == "image/png"))
    && ($_FILES["page_main_image"]["size"] < 200000)
    && in_array($extension, $allowedExts))
{
    if ($_FILES["page_main_image"]["error"] > 0) {
        echo "Return Code: " . $_FILES["page_main_image"]["error"] . "<br />";;
    }
    else {
        echo "Upload: " . $_FILES["page_main_image"]["name"] . "<br />";
        echo "Type: " . $_FILES["page_main_image"]["type"] . "<br />";
        echo "Size: " . ($_FILES["page_main_image"]["size"] / 1024) . " kb<br />";

        if (file_exists("uploads/" . $_FILES["page_main_image"]["name"]))
        {
            echo $_FILES["page_main_image"]["name"] . " already exists. ";
        }
        else
        {
            move_uploaded_file($_FILES["page_main_image"]["tmp_name"],
                "uploads/" . $_FILES["page_main_image"]["name"]);
            echo "Stored in: " . "uploads/" . $_FILES["page_main_image"]["name"] . "<br />";
            $image="{$_FILES['page_main_image']['name']}";
        }
    }
}
else {
    echo "Invalid file";
}

return $image;
}

表格处理如下:

<?php
if (isset($_POST['submit'])) {
//Process the form
$image = upload_file();

$project_id = $_POST['project_id'];
//var_dump ($project_id);

$wireframe_title = mysql_prep($_POST["wireframe_title"]);
$browser_title = $_POST["browser_title"];
$url_key = $_POST["url_key"];
$wireframe_type = $_POST["wireframe_type"];
//$image = $_POST["page_main_image"];
$page_bg_color = $_POST ["page_bg_color"];

$query  = "INSERT INTO wireframes (";
$query .= " project_id, wireframe_title, browser_title, url_key, wireframe_type, page_main_image, page_bg_color";
$query .= " ) VALUES (";
$query .= " '{$project_id}','{$wireframe_title}', '{$browser_title}', '{$url_key}', '{$wireframe_type}', '{$image}', '{$page_bg_color}' ";
$query .= ")";

echo $query;

try { $result = mysqli_query($connection, $query);
} catch (Exception $e) {
    return 'Caught exception: '+  $e->getMessage()+ "\n";
}
//Test if there was a query error
if ($result) {
    //Success
    // would normally use a redirect ie redirect_to("somepage.php");
    //$message = "Subject created.";
    redirect_to("wireframes.php?id=$project_id");
}else {
    //failure
    //$message = "Subject creation failed.";
    //redirect_to("add_project.php");
    echo $query;
}
} else {
// This is probably a GET request
redirect_to("add_edit_wireframe.php?id= echo $_GET[$project_id]");
}
?>

1 个答案:

答案 0 :(得分:1)

$ _FILES中的大小以字节表示。 200.000 =约195千字节。 你是否在if语句中没有这个条件进行过测试?