如何在此代码中添加大小限制?
我是php的新手。试图创建表单。我在网上找到了这个代码,但是当我上传一个文件时,它只接受700kb
个文件而不是更多,否则就会出错。我至少需要20mb
。我必须在我的代码中编写什么才能使它工作?即使它应该有助于更新。
请帮忙
<body>
<div id="art-main">
<div id="art-header-bg" class="clearfix">
</div>
<div id="art-hmenu-bg" class="art-bar art-nav clearfix">
</div>
<div class="art-sheet clearfix">
<header class="art-header clearfix">
<div class="art-shapes">
<h1 class="art-headline" data-left="0.5%">
<a href="http://www.99demos.blogspot.com" target="_blank">library</a>
</h1>
<h2 class="art-slogan" data-left="87.05%"><a href="http://99demos.blogspot.in/2014/06/createreadupdate-delete-with-image.html"></a></h2>
</div>
<nav class="art-nav clearfix">
<div class="art-nav-inner">
<ul class="art-hmenu"><li><a href="index.php" class="active">Home</a></li></ul>
</div>
</nav>
</header>
<div class="art-layout-wrapper clearfix">
<div class="art-content-layout">
<div class="art-content-layout-row">
<div class="art-layout-cell art-content clearfix"><article class="art-post art-article">
<h2 class="art-postheader">customer form</h2>
<div class="art-postcontent art-postcontent-0 clearfix"><div class="art-content-layout">
<div class="art-content-layout-row">
<div class="art-layout-cell layout-item-0" style="width: 100%" >
<br>
<?php
ob_start();
include('include/connect.php');
if (isset($_GET['id'])) {
$qry = "SELECT * FROM tbl_emp where id=" . $_GET['id'];
$result = mysql_query($qry);
$row = mysql_fetch_array($result);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$age = $_POST["age"];
$gender = $_POST['gender'];
$martial = $_POST["martial"];
$contact = $_POST["contact"];
$email = $_POST["email"];
$location = $_POST["location"];
$drname = $_POST["drname"];
$cname = $_POST["cname"];
$clocation = $_POST["clocation"];
$implants = implode(',', $_POST['implants']);
$problem = $_POST["problem"];
$tgiven = $_POST["tgiven"];
$tdate = $_POST["tdate"];
IF ($_FILES['file']['name'] != '') {
$file = 'uploads/' . $row['image'];
@unlink($file);
$tmp_name = $_FILES["file"]["tmp_name"];
$namefile = $_FILES["file"]["name"];
$ext = end(explode(".", $namefile));
$image_name = time() . "." . $ext;
$fileUpload = move_uploaded_file($tmp_name, "uploads/" . $image_name);
} else {
$image_name = $row['image'];
}
$sqlAdd = "update tbl_emp set name='" . $name . "', age='" . $age . "', gender='" . $gender . "', martial='" . $martial . "', contact='" . $contact . "', email='" . $email . "', location='" . $location . "', drname='" . $drname . "', cname='" . $cname . "', clocation='" . $clocation . "', implants='" . $implants . "', problem='" . $problem . "', tgiven='" . $tgiven . "', tdate='" . $tdate . "', image='" . $image_name . "' where id=" . $_GET['id'];
mysql_query($sqlAdd);
header("Location:add.php?id=" . @$_GET['id'] . "&msg=success");
exit;
}
} else {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$age = $_POST["age"];
$gender = $_POST['gender'];
$martial = $_POST["martial"];
$contact = $_POST["contact"];
$email = $_POST["email"];
$location = $_POST["location"];
$drname = $_POST["drname"];
$cname = $_POST["cname"];
$clocation = $_POST["clocation"];
$implants = $_POST["implants"];
$problem = $_POST["problem"];
$tgiven = $_POST["tgiven"];
$tdate = $_POST["tdate"];
IF ($_FILES['file']['name'] != '') {
$tmp_name = $_FILES["file"]["tmp_name"];
$namefile = $_FILES["file"]["name"];
$ext = end(explode(".", $namefile));
$image_name = time() . "." . $ext;
$fileUpload = move_uploaded_file($tmp_name, "uploads/" . $image_name);
}
$sqlAdd = mysql_query("insert into tbl_emp(name,age,gender,martial,contact,email,location,drname,cname,clocation,implants,problem,tgiven,tdate,image) VALUES('$name','$age','$gender','$martial','$contact','$email','$location','$drname','$cname','$clocation','$implants','$problem','$tgiven','$tdate','$image_name')");
header("Location:index.php?msg=success");
exit;
}
}
ob_end_flush();
if (isset($_GET['msg'])) {
?>
<div style="color:red;padding-bottom:10px;" class="form-message" align="center"><b>Task completd successfully.</b></div>
<?php
}
?>
<form method="post" name="login" id="login" enctype="multipart/form-data">
<table class="table" width="100%">
<tr>
<td width="10%">Customer Details</td>
<td></td>
</tr>
<tr>
<td width="10%">Full Name</td>
<td><input name="name" type="text" placeholder="Firstname Lastname" pattern="[A-Za-z-0-9]+\s[A-Za-z-'0-9]+" required value="<?php echo @$row['name']; ?>"></td>
</tr>
<tr>
<td width="20%">Age</td>
<td><input name="age" type="text" value="<?php echo @$row['2']; ?>" ></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="Radio" name="gender" value="male" <?php
if ($gender == 'male') {
echo 'checked';
}
?> />
Male
<input type="Radio" name="gender" value="female" <?php
if ($gender == 'female') {
echo 'checked';
}
?> />
Female</td>
</tr>
<tr>
<td width="10%">Martial Status</td>
<td><input name="martial" type="text" value="<?php echo @$row['4']; ?>" ></td>
</tr>
<tr>
<td width="20%">Contact Number</td>
<td><input name="contact" type="text" value="<?php echo @$row['5']; ?>"></td>
</tr>
<tr>
<td width="10%">Email Address</td>
<td><input name="email" type="text" value="<?php echo @$row['6']; ?>"></td>
</tr>
<tr>
<td width="10%">Location</td>
<td><input name="location" type="text" value="<?php echo @$row['7']; ?>" ></td>
</tr>
<tr>
<td width="10%">Treatment Details</td>
<td></td>
</tr>
<tr>
<td width="10%">Doctor Name</td>
<td><input name="drname" type="text" value="<?php echo @$row['8']; ?>" ></td>
</tr>
<tr>
<td width="10%">Clinic Name</td>
<td><input name="cname" type="text" value="<?php echo @$row['9']; ?>" ></td>
</tr>
<tr>
<td width="10%">Clinic Location & Branch</td>
<td><input name="clocation" type="text" value="<?php echo @$row['10']; ?>" ></td>
</tr>
<tr>
<td width="10%">Implants</td>
<td>
<select name="implants" id="implants" value="<?php echo @$row['11']; ?>">
<option <?php if ($implants == '1') echo 'selected="selected'; ?> value="1">1</option>
<option <?php if ($implants == '2') echo 'selected="selected'; ?> value="2">2</option>
<option <?php if ($implants == '3') echo 'selected="selected'; ?> value="3">3</option>
</select>
</td>
</tr>
<tr>
<td width="10%">Problem</td>
<td><input name="problem" type="text" value="<?php echo @$row['12']; ?>" ></td>
</tr>
<tr>
<td width="10%">Treatment Given</td>
<td><input name="tgiven" type="text" value="<?php echo @$row['13']; ?>" ></td>
</tr>
<tr>
<td width="10%">Treatment Date</td>
<td><input name="tdate" type="date" value="<?php echo @$row['14']; ?>" ></td>
</tr>
<tr>
<td>multifile
<td>
<input id="fileupload" type="file" multiple="multiple" />
<hr />
<b>Live Preview</b>
<br />
<br />
<div id="dvPreview">
</div>
</td>
</tr>
<tr>
<td width="10%">X-Ray Image</td>
<td><input name="file" type="file" multiple="" ></td>
</tr>
<?php
if (isset($row['image'])) {
?>
<tr>
<td> </td>
<td><img src="uploads/<?php echo $row['image']; ?> " height="50" width="50"></td>
</tr>
<?php
}
?>
<tr>
<td> </td>
<td>
<input name="submit" value="Submit" type="submit" class="submit">
<input name="submit" value="Cancel" type="button" class="submit" onClick="window.location = 'index.php'">
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
</article></div>
</div>
</div>
</div>
</div>
<footer class="art-footer clearfix">
<div class="art-footer-inner">
<p>Copyright © 2016. All Rights Reserved.</p>
<p class="art-page-footer">
</p>
</div>
</footer>
</div>
我的php.ini文件
; This file is for CGI/FastCGI installations.
; Try copying it to php5.ini, if it doesn't work
; adjust memory limit
memory_limit = 64M
max_execution_time = 18000
; disable magic quotes for php request vars
magic_quotes_gpc = off
; disable automatic session start
; before autoload was initialized
flag session.auto_start = off
; enable resulting html compression
zlib.output_compression = on
; disable user agent verification to not break multiple image upload
suhosin.session.cryptua = off
; turn off compatibility with PHP4 when dealing with objects
zend.ze1_compatibility_mode = off
; PHP for some reason ignores this setting in system php.ini
; and disables mcrypt if this line is missing in local php.ini
extension=mcrypt.so
; Disable PHP errors, notices and warnings output in production mode to prevent exposing sensitive information.
display_errors = Off
答案 0 :(得分:1)
上传文件时应始终检查的第一件事是$ _FILES数组中提供的错误条件
// Check $_FILES['file']['error'] value.
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
echo 'No file sent.';
break;
case UPLOAD_ERR_PARTIAL :
echo 'Only part of the file was uploaded';
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
echo 'Exceeded filesize limit.';
break;
default:
echo 'Unknown errors.';
}
我猜您会收到超出文件大小限制消息。
如果是这种情况,您必须在php.ini
文件中修改这些限制。
寻找
upload_max_filesize(上传文件的最大尺寸。)
max_file_uploads(允许同时上传的最大文件数。)
post_max_size(设置允许的后期数据的最大大小。此设置也会影响文件上传。要上传大文件,此值必须大于upload_max_filesize。一般来说,memory_limit应大于post_max_size。当一个整数是使用时,该值以字节为单位。也可以使用此常见问题解答中描述的简写表示法。如果发布数据的大小大于post_max_size,则$ _POST和$ _FILES超全局变量为空。可以在各种中跟踪方法,例如通过将$ _GET变量传递给处理数据的脚本,即检查$ _GET [&#39;已处理&#39;]是否已设置。)
memory_limit(这设置允许脚本分配的最大内存量(以字节为单位)。这有助于防止编写糟糕的脚本占用服务器上的所有可用内存。请注意,没有内存限制,请设置这个指令为-1。)
手册,以便您可以计算出使用的值http://php.net/manual/en/ini.list.php
请记住它们是相关的,所以一个简单的经验法则来计算post_max_filesize
它们将是
post_max_size = (upload_max_filesize * max_file_uploads)
+ some additional space for all other input fields
答案 1 :(得分:-1)
我在代码中找不到与文件大小相关的任何条件。您应该检查php.ini
。
要验证尺寸,您只需检查file size
。
$maxsize = 2097152; // 2MB
if(($_FILES['file']['size'] >= $maxsize) || ($_FILES['file']["size"] == 0)) {
echo $errors = 'File too large. File must be less than 2 megabytes.';
}
答案 2 :(得分:-1)
在您的文件中实现给定的代码
<?php
$uploadOk=1;
if($_FILES['file']['name']!='')
{
if ($_FILES["fileToUpload"]["size"] > 2500000)
{
$uploadOk = 0;
}
$tmp_name = $_FILES["file"]["tmp_name"];
$namefile = $_FILES["file"]["name"];
$ext = end(explode(".", $namefile));
$image_name=time().".".$ext;
if($uploadOk==1)
{
$fileUpload = move_uploaded_file($tmp_name,"uploads/".$image_name);
}
else
{
header("location:add.php?id=".$_GET['id']."&msg=error");
}
}
?>