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

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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘