'Forbidden(403) error when try to redirect to url

I've got form on my page, when press send and attachment is too big I would like to back to form and display info that file is too big.

I used:


$file_name =    $_FILES['uploaded-file']["name"][$i];   $redirectUrl = sprintf('Location: %s#file-size-error', $_SERVER['HTTP_REFERER']);

if ($_FILES['uploaded-file']['size'][$i] > $max_size) { 
                   
                    $_SESSION['file-size'] =  "plik. $file_name .jest zbyt duży";
                    header($redirectUrl);
                    die();
                             }

When I press send button and file is too big, sometimes it works as it should and sometimes I have 403 error. When I put direct url to header result is the same. I set chmod of project folder to 775 still the same problem, can it be a fault of serwer settings ?

full php code:

<?php
session_start();
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
//$mail->addCustomHeader('Content-Type', 'text/plain;charset=utf-8');

$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
setlocale( LC_ALL, "pl_PL.UTF-8" );

$honeypot = $_POST['honey'];
$user_name =  $_POST['name'];
$user_email = $_POST['email'];
$user_message = $_POST['message'];
$user_phone = $_POST['phone'];
$honeypot = trim($_POST["honey"]);
$max_size = 2 * 1024 * 1204; //2mb
$attachment = $_FILES['uploaded-file'];


if ($_SERVER["REQUEST_METHOD"] == "POST") {


    if(!empty($honeypot)) {
        echo "NO SPAM!"; 
        exit;
      }    else {

        $mail = new PHPMailer; //From email address and name 
        $mail->isMail(); 

       
        
        //sender
        $mail->From = $user_email;
        $mail->FromName = $user_name;

        //recipient
        $mail->addAddress("[email protected]");
        

        //mail subject
        $mail->Subject = "Zapytanie ze strony www"; 
        
        
        $mail->isHTML(true);
        //body mail 
        $mail->Body = "Telefon:$user_phone<br><br>Treść wiadomośći:<br>$user_message";
        $mail->AltBody = "Telefon:$user_phone\n$content"; 
        
        //attachment
        if(isset($attachment)) {
                        
            $amount_ofFiles = count($_FILES['uploaded-file']['size']);
            $redirectUrl = sprintf('Location: %s#file-size-error', $_SERVER['HTTP_REFERER']);
           
            if ($amount_ofFiles > 3)  {

                $_SESSION['file-amount'] =  "przekroczono limit załączników";
                header($redirectUrl);

                die();
            } 
             

            for ($i = 0; $i < count($_FILES['uploaded-file']['name']); $i++) {
                if ($_FILES['uploaded-file']['error'][$i] !== UPLOAD_ERR_OK) continue;
                $file_TmpName = $_FILES['uploaded-file']["tmp_name"][$i]; 
                $file_name =    $_FILES['uploaded-file']["name"][$i];  

                if ($_FILES['uploaded-file']['size'][$i] > $max_size) { 
                   
                    $_SESSION['file-size'] =  "plik. $file_name .jest zbyt duży";
                    header($redirectUrl);
                    die();
                             }
                 
                else{
                move_uploaded_file($file_TmpName,  "uploads/" . $file_name);
                $mail-> AddAttachment("uploads/". $file_name);

                    }      
            }//for
        }//isset

        if(!$mail->send()) {
            echo "Mailer Error: " . $mail->ErrorInfo;
           exit();
      } 
      
              else {
              header("Location: sent.html");
                             
                 exit();

      }//if send else

}//honey else end

}//post end


code in my html file(set in htaccess to treat html files as php)

  <div id ="file-size-error" class="file-error">
                    <?php
                    
                session_start();
                if(isset($_SESSION['status'])) {
    
                    echo($_SESSION['status']);
                    unset($_SESSION['status']);  
                                    
                                }
                    
                    ?>
                    
                    
                </div>```


Solution 1:[1]

Problem was in .htaccess file

By default in htaccess attached to CMS(batflat) there was a line, that caused the problem:

<Files ~ "^.*\.(sdb|md|html|txt)$">
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
        Satisfy All
    </IfModule>
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
</Files>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jaro