doumeng3188
2014-05-20 11:34
浏览 60
已采纳

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

I've created an image upload using PHP, the idea being that the image will save to a directory and the path to the the database which is pretty standard. The problem is it wont save anything over 20kb. I have increased the max upload and post max size in the php.ini file to 10M and have also set size to < 200000kb in the function but it makes no difference. Can somebody please tell me where i have been banging my head off this for days now :(

File upload function (based on example at 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;
}

The form processing is as follows:

<?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()+ "
";
}
//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条回答 默认 最新

  • duanhuanbo5225 2014-05-20 11:41
    已采纳

    The size in $_FILES is expressed in bytes. 200.000 = around 195 kilobyte. Did you tested it without that condition in the if statement?

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题