Search code examples
phpsizetemporary-files

Function to retrieve the temporary path of a file is failing for a given file size


I have a simple code as follows :

   $file_tmp = $_FILES['file']['tmp_name'];

   echo $file_tmp;

   $file = new SplFileObject($file_tmp);

   echo filesize($file_tmp);

['file'] is the field of a html form in which the user selects the file to be treated in the action written in php.

<form method="POST" action="processCity.php" enctype="multipart/form-data">
     <h2>Register list of cities from the file:</h2>
     <input type="file" name="file" required> <input type="submit" name="btnImport" value="Import">
</form>

It happens that when the file has more than 2097152 bytes (2.1 MB), the path is not returned in the $file_tmp variable. Does anyone understand this? If the file size is less than or equal to the size given above, everything works fine. ¯_(ツ)_/¯ PHP 8.2.8 Ubuntu 20.04.6

I tried: $file = file($file_tmp); but it didn't work either. As before, only if the file size is <= 2097152 bytes (2.1 MB), the variable is populated with the path information of the file sent from the form with the POST method.

I looked for an answer here on the site and outside of it too but I didn't get an answer. I can't even imagine what it might be like to try something more on my own.


Solution

  • The issue is related to upload_max_filesize defined in php.ini file. And also a Maximum number of files that can be uploaded via a single request defines as

    upload_max_filesize = 20M(default) => change it as per you requirement.

    max_file_uploads = 20 (it is default limit) => change it as per you requirement.