设置PHP的最大文件大小

设置PHP的最大文件大小,php,wamp,Php,Wamp,我正在创建一个社交网站,并试图限制用户可以上传的文件大小。我已经知道如何在wamp/xamp设置中更改upload\u max\u filesize,但是有办法手动完成吗?例如,设置最大为8mb,但我可以手动将其设置为7.5mb或PHP文件本身中的某个值吗?这就是我如何努力做到的: $file_size = $_FILES['files']['size']; $maxsize = 1048576; if($_FILES['files']['size'] > $maxsize) {

我正在创建一个社交网站,并试图限制用户可以上传的文件大小。我已经知道如何在wamp/xamp设置中更改
upload\u max\u filesize
,但是有办法手动完成吗?例如,设置最大为8mb,但我可以手动将其设置为7.5mb或PHP文件本身中的某个值吗?这就是我如何努力做到的:

$file_size = $_FILES['files']['size'];
$maxsize = 1048576;

if($_FILES['files']['size'] > $maxsize) {

    $errors = "Your file is to large";
}
我还将
$\u文件['FILES']['size']
替换为
$file\u size
。我也试过:

if ($file_size > 15097152) {

    $errors = 'File cannot be larger than 1MB';
}
多谢各位

编辑




$date\u time=date('Y-m-d\u H-i-s');
$img_限值=10;
$maxsize=3367463;
如果(isset($_POST['submit'])){
$errors=[];
$file_name=计数($_FILES['FILES']['name']);
$file\u size=$\u FILES['FILES']['size'];
//$file\u type=$\u FILES['FILES']['type'];
$imageFileType=pathinfo($file\u name,pathinfo\u扩展名);
如果(strtolower($imageFileType)!=“jpeg”&&strtolower($imageFileType)!=“jpg”&&
strtolower($imageFileType)!=“png”和&strtolower($imageFileType)!=“gif”){
$errors=“不允许使用文件类型。”;
}
如果($_FILES['FILES']['size']>=$maxsize){
$errors=“您的文件太大”;
}
如果($img\U限制>10){
$errors='不能上载超过10个图像';
}
//循环浏览每个文件
对于($i=0;$i<$file_name;$i++){
//获取临时文件路径
$file\u tmp=$\u FILES['FILES']['tmp\u name'][$i];
//确保我们有一个文件路径
如果(!$errors | |$file_tmp!=“”){
$picToUpload=$date\U time.“-#-”.md5($file\u name)。“-#-”$\u FILES['FILES']['name'][$i];
$uploadPicture=移动上传的文件($file\u tmp,“上传/”)$picToUpload);
$file_path=“uploads/”$picToUpload;
$stmt=$con->prepare(“插入图像(图像)值(?)”;
$stmt->bind_参数('s',$file_路径);
$stmt->execute();
//$stmt->close();
标题('Location:index4.php');
退出();
}
}
}
$\u会话['error']='

' . $错误。”

",; 标题('Location:index4.php'); 退出();
您需要将验证检查放入处理每个文件的
循环中

$date_time = date('Y-m-d_H-i-s');
$img_limit = 10;
$maxsize = 3367463;

if(isset($_POST['submit'])) {
    $errors = '';
    $file_count = count($_FILES['files']['name']);

    if ($file_count > $img_limit) {
        $errors = 'Cannot upload more than 10 images';
    }
    if (!$errors) {
        $stmt = $con->prepare("INSERT INTO images (image) VALUES (?)");
        $stmt->bind_param('s', $file_path);
        // Loop through each file
        for( $i=0 ; $i < $file_count ; $i++ ) {
            $file_name = $_FILES['files']['name'][$i];
            $file_size = $_FILES['files']['size'][$i];
            $file_tmp = $_FILES['files']['tmp_name'][$i];
        
            $imageFileType = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

            if ($file_size >= $maxsize) {
                $errors = "Your file is too large";
            } elseif ($imageFileType != "jpeg" && $imageFileType != "jpg" && 
                      $imageFileType != "png" && $imageFileType != "gif") {
                $errors = "File type not allowed.";
            }
            //Make sure we have a file path
            if (!$errors && $file_tmp != "") {

                $picToUpload = $date_time . " -#- " . md5($file_name) . " -#- " . $_FILES['files']['name'][$i];
                $uploadPicture = move_uploaded_file($file_tmp, "uploads/" . $picToUpload);

                $file_path = "uploads/" . $picToUpload;
                $stmt->execute();
            }
        }
    }
}
if ($errors) {
    $_SESSION['error'] = '<b><p style="color: #000; font-size: 30px; top: 34%;right: 50%;position: absolute;">
' . $errors . '</p></b>';
}
header('Location: index4.php');
exit();
此外,您对图像数量的限制是错误的。您需要比较上载到
$img\u limit
的文件数,而不是将
$img\u limit
与初始化时使用的相同值进行比较

我将重定向和退出循环,因为这将在上载第一个文件后重定向。我还将
prepare
bind_param
从循环中取出,因为每个文件都可以使用相同的prepared语句

$date_time = date('Y-m-d_H-i-s');
$img_limit = 10;
$maxsize = 3367463;

if(isset($_POST['submit'])) {
    $errors = '';
    $file_count = count($_FILES['files']['name']);

    if ($file_count > $img_limit) {
        $errors = 'Cannot upload more than 10 images';
    }
    if (!$errors) {
        $stmt = $con->prepare("INSERT INTO images (image) VALUES (?)");
        $stmt->bind_param('s', $file_path);
        // Loop through each file
        for( $i=0 ; $i < $file_count ; $i++ ) {
            $file_name = $_FILES['files']['name'][$i];
            $file_size = $_FILES['files']['size'][$i];
            $file_tmp = $_FILES['files']['tmp_name'][$i];
        
            $imageFileType = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

            if ($file_size >= $maxsize) {
                $errors = "Your file is too large";
            } elseif ($imageFileType != "jpeg" && $imageFileType != "jpg" && 
                      $imageFileType != "png" && $imageFileType != "gif") {
                $errors = "File type not allowed.";
            }
            //Make sure we have a file path
            if (!$errors && $file_tmp != "") {

                $picToUpload = $date_time . " -#- " . md5($file_name) . " -#- " . $_FILES['files']['name'][$i];
                $uploadPicture = move_uploaded_file($file_tmp, "uploads/" . $picToUpload);

                $file_path = "uploads/" . $picToUpload;
                $stmt->execute();
            }
        }
    }
}
if ($errors) {
    $_SESSION['error'] = '<b><p style="color: #000; font-size: 30px; top: 34%;right: 50%;position: absolute;">
' . $errors . '</p></b>';
}
header('Location: index4.php');
exit();
$date\u time=date('Y-m-d\u H-i-s');
$img_限值=10;
$maxsize=3367463;
如果(isset($_POST['submit'])){
$errors='';
$file_count=计数($_FILES['FILES']['name']);
如果($file\u count>$img\u limit){
$errors='不能上载超过10个图像';
}
如果(!$errors){
$stmt=$con->prepare(“插入图像(图像)值(?)”;
$stmt->bind_参数('s',$file_路径);
//循环浏览每个文件
对于($i=0;$i<$file\u count;$i++){
$file\u name=$\u FILES['FILES']['name'][$i];
$file\u size=$\u FILES['FILES']['size'][$i];
$file\u tmp=$\u FILES['FILES']['tmp\u name'][$i];
$imageFileType=strtolower(路径信息($file_name,pathinfo_EXTENSION));
如果($file\u size>=$maxsize){
$errors=“您的文件太大”;
}elseif($imageFileType!=“jpeg”&&$imageFileType!=“jpg”&&
$imageFileType!=“png”&&&$imageFileType!=“gif”){
$errors=“不允许使用文件类型。”;
}
//确保我们有一个文件路径
如果(!$errors&&$file\u tmp!=“”){
$picToUpload=$date\U time.“-#-”.md5($file\u name)。“-#-”$\u FILES['FILES']['name'][$i];
$uploadPicture=移动上传的文件($file\u tmp,“上传/”)$picToUpload);
$file_path=“uploads/”$picToUpload;
$stmt->execute();
}
}
}
}
如果($errors){
$\u会话['error']='

“.$错误。”

”; } 标题('Location:index4.php'); 退出();
您需要将验证检查放入处理每个文件的
循环中

$date_time = date('Y-m-d_H-i-s');
$img_limit = 10;
$maxsize = 3367463;

if(isset($_POST['submit'])) {
    $errors = '';
    $file_count = count($_FILES['files']['name']);

    if ($file_count > $img_limit) {
        $errors = 'Cannot upload more than 10 images';
    }
    if (!$errors) {
        $stmt = $con->prepare("INSERT INTO images (image) VALUES (?)");
        $stmt->bind_param('s', $file_path);
        // Loop through each file
        for( $i=0 ; $i < $file_count ; $i++ ) {
            $file_name = $_FILES['files']['name'][$i];
            $file_size = $_FILES['files']['size'][$i];
            $file_tmp = $_FILES['files']['tmp_name'][$i];
        
            $imageFileType = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

            if ($file_size >= $maxsize) {
                $errors = "Your file is too large";
            } elseif ($imageFileType != "jpeg" && $imageFileType != "jpg" && 
                      $imageFileType != "png" && $imageFileType != "gif") {
                $errors = "File type not allowed.";
            }
            //Make sure we have a file path
            if (!$errors && $file_tmp != "") {

                $picToUpload = $date_time . " -#- " . md5($file_name) . " -#- " . $_FILES['files']['name'][$i];
                $uploadPicture = move_uploaded_file($file_tmp, "uploads/" . $picToUpload);

                $file_path = "uploads/" . $picToUpload;
                $stmt->execute();
            }
        }
    }
}
if ($errors) {
    $_SESSION['error'] = '<b><p style="color: #000; font-size: 30px; top: 34%;right: 50%;position: absolute;">
' . $errors . '</p></b>';
}
header('Location: index4.php');
exit();
此外,您对图像数量的限制是错误的。您需要比较上载到
$img\u limit
的文件数,而不是将
$img\u limit
与初始化时使用的相同值进行比较

我将重定向和退出循环,因为这将在上载第一个文件后重定向。我还将
prepare
bind_param
从循环中取出,因为每个文件都可以使用相同的prepared语句

$date_time = date('Y-m-d_H-i-s');
$img_limit = 10;
$maxsize = 3367463;

if(isset($_POST['submit'])) {
    $errors = '';
    $file_count = count($_FILES['files']['name']);

    if ($file_count > $img_limit) {
        $errors = 'Cannot upload more than 10 images';
    }
    if (!$errors) {
        $stmt = $con->prepare("INSERT INTO images (image) VALUES (?)");
        $stmt->bind_param('s', $file_path);
        // Loop through each file
        for( $i=0 ; $i < $file_count ; $i++ ) {
            $file_name = $_FILES['files']['name'][$i];
            $file_size = $_FILES['files']['size'][$i];
            $file_tmp = $_FILES['files']['tmp_name'][$i];
        
            $imageFileType = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

            if ($file_size >= $maxsize) {
                $errors = "Your file is too large";
            } elseif ($imageFileType != "jpeg" && $imageFileType != "jpg" && 
                      $imageFileType != "png" && $imageFileType != "gif") {
                $errors = "File type not allowed.";
            }
            //Make sure we have a file path
            if (!$errors && $file_tmp != "") {

                $picToUpload = $date_time . " -#- " . md5($file_name) . " -#- " . $_FILES['files']['name'][$i];
                $uploadPicture = move_uploaded_file($file_tmp, "uploads/" . $picToUpload);

                $file_path = "uploads/" . $picToUpload;
                $stmt->execute();
            }
        }
    }
}
if ($errors) {
    $_SESSION['error'] = '<b><p style="color: #000; font-size: 30px; top: 34%;right: 50%;position: absolute;">
' . $errors . '</p></b>';
}
header('Location: index4.php');
exit();
$date\u time=date('Y-m-d\u H-i-s');
$img_限值=10;
$maxsize=3367463;
如果(isset($_POST['submit'])){
$errors='';
$file_count=计数($_FILES['FILES']['name']);
如果($file\u count>$img\u limit){
$errors='不能上载超过10个图像';
}
如果(!$errors){
$stmt=$con->prepare(“插入图像(图像)值(?)”;
$stmt->bind_参数('s',$file_路径);
//循环浏览每个文件
对于($i=0;$i<$file\u count;$i++){
$file\u name=$\u FILES['FILES']['name'][$i];
$file\u size=$\u FILES['FILES']['size'][$i];
$file\u tmp=$\u FILES['FILES']['tmp\u name'][$i];
$imageFileType=strtolower(路径信息($file_name,pathinfo_EXTENSION));
如果($file\u size>=$maxsize){
$errors=“您的文件太大”;
}elseif($imageFileType!=“jpeg”&&&$imageFileType!=“jpg”