Search code examples
phpwamp

Set max file size PHP


I am creating a social site and I am trying to limit the size of the file the user can upload. I already know about changing the upload_max_filesize in the wamp/xamp settings, but is there a way to do it manually ? For e.g. the settings max is 8mb but can I manually set it to 7.5mb or something in the PHP file itself ? This is how I have tried to far :

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

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

    $errors = "Your file is to large";
}

I also replaced $_FILES['files']['size'] with $file_size. And I also tried:

if ($file_size > 15097152) {

    $errors = 'File cannot be larger than 1MB';
}

Thank you.

EDIT


<input name="files[]" id='files' accept="image/png,image/gif,image/jpeg" type="file" multiple="multiple" />

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

if(isset($_POST['submit'])) {

    $errors = [];
    $file_name = count($_FILES['files']['name']);
    $file_size = $_FILES['files']['size'];
    
    //$file_type = $_FILES['files']['type'];
    $imageFileType = pathinfo($file_name, PATHINFO_EXTENSION);

    if (strtolower($imageFileType) != "jpeg"&& strtolower($imageFileType) != "jpg" && 
        strtolower($imageFileType) != "png" && strtolower($imageFileType) != "gif") {

        $errors = "File type not allowed.";
    }

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

        $errors = "Your file is to large";
    }

    if ($img_limit > 10) {

        $errors = 'Cannot upload more than 10 images';
    }
    // Loop through each file
    for( $i=0 ; $i < $file_name ; $i++ ) {

        //Get the temp file path
        $file_tmp = $_FILES['files']['tmp_name'][$i];

        //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 = $con->prepare("INSERT INTO images (image) VALUES (?)");
            $stmt->bind_param('s', $file_path);
            $stmt->execute();
            //$stmt->close();
            header('Location: index4.php');
            exit();
        }
    }
}

$_SESSION['error'] = '<b><p style="color: #000; font-size: 30px; top: 34%;right: 50%;position: absolute;">
' . $errors . '</p></b>';
header('Location: index4.php');
exit();

Solution

  • You need to put the validation checks inside the for loop that processes each file.

    ALso, your limit on the number of images is wrong. You need to compare the number of files that were uploaded to $img_limit, not compare $img_limit to the same value you initialized it with.

    I've taken the redirect and exit out of the loop, because that will redirect after uploading the first file. I've also taken prepare and bind_param out of the loop, since the same prepared statement can be used for each file.

    $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();