我正在处理通过网络接收的此脚本,以将文件上传到服务器文件夹。该脚本工作正常,但它只允许重量小于2 MB的jpeg图像。 我通过添加其他格式来加载代码来修改代码,它很有用,不幸的是不再考虑重量限制(2 mb)。我哪里错了? 谢谢(我附上原始和编辑的脚本)
原创剧本
<?php
require('connect.php');
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
$extension = substr($name, strpos($name, '.') + 1);
$max_size = 2000000;
if(isset($name) && !empty($name)){
if(($extension == "jpg" || $extension == "jpeg") && $type == "image/jpeg" && $extension == $size<=$max_size){
$location = "uploads/";
if(move_uploaded_file($tmp_name, $location.$name)){
$query = "INSERT INTO `upload` (name, size, type, location) VALUES ('$name', '$size', '$type', '$location$name')";
$result = mysqli_query($connection, $query);
$smsg = "Caricamento riuscito.";
}else{
$fmsg = "Caricamento fallito";
}
}else{
$fmsg = "Il file deve avere una dimesione inferiore a 2 mb e in formato jpeg";
}
}else{
$fmsg = "Seleziona un file";
}
?>
<html>
<head>
<title>File Upload Script Using PHP MySQL</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
<link rel="stylesheet" href="styles.css" >
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<?php //echo $name; ?>
<?php //echo $size; ?>
<?php //echo $type; ?>
<?php //echo $tmp_name; ?>
<form class="form-signin" method="POST" enctype="multipart/form-data">
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<h2 class="form-signin-heading">Upload File</h2>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" name="file" id="exampleInputFile">
<p class="help-block">Carica file in formato JPEG inferiori a 2 MB</p>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">carica</button>
</form>
</div>
</body>
</html>
&#13;
Codice modificato
if(isset($name) && !empty($name)){
if(($extension == "jpg" || $extension == "jpeg") && $type == "image/jpeg" || $type == "image/png " || $type == "image/gif" && $extension == $size<=$max_size){
$location = "uploads/";
&#13;
我想我会错过语法,有人可以帮助我吗?
答案 0 :(得分:1)
<?php
require('connect.php');
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
$extension = substr($name, strpos($name, '.') + 1);
$max_size = 2000000;
if(isset($name) && !empty($name)){
if(($type == "image/jpeg" || $type == "image/png" || $type == "image/gif" || $type == "application/x-zip-compressed") && ($size<=$max_size)){
$location = "uploads/";
if(move_uploaded_file($tmp_name, $location.$name)){
$query = "INSERT INTO `upload` (name, size, type, location) VALUES ('$name', '$size', '$type', '$location$name')";
$result = mysqli_query($connection, $query);
$smsg = "Caricamento riuscito.";
}else{
$fmsg = "Caricamento fallito";
}
}else{
$fmsg = "Il file deve avere una dimesione inferiore a 2 mb e in formato: jpeg, png, gif, zip";
}
}else{
$fmsg = "Seleziona un file";
}
?>
<html>
<head>
<title>Carica file</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
<link rel="stylesheet" href="styles.css" >
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<?php //echo $name; ?>
<?php //echo $size; ?>
<?php //echo $type; ?>
<?php //echo $tmp_name; ?>
<form class="form-signin" method="POST" enctype="multipart/form-data">
<?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?>
<?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?>
<h2 class="form-signin-heading">Upload File</h2>
<div class="form-group">
<label for="exampleInputFile">Scegli file</label>
<input type="file" name="file" id="exampleInputFile">
<p class="help-block">Carica file in formato JPEG, PNG, GIF, ZIP inferiori a 2 MB</p>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">carica</button>
</form>
</div>
</body>
</html>
这是最终的解决方案。感谢@ADyson的宝贵帮助。 如果有进一步的步骤来改进脚本,那么它们很受欢迎
答案 1 :(得分:0)
默认情况下,pхp.ini中的Giorgio设置为2 MB: upload_max_filesize = 2M 可能按顺序 - 824。您需要使用所需的值更改它。