Big files take unjustly long time to submit from the form Laravel PHP
I am creating a project using Laravel PHP, which requires selecting a video file from form and submitting it to controller function for further processing. But I came across this issue that those video files take a lot of time just to submit, e.g. 600MB video takes about 10-15 minutes just to reach the controller, and I am required to upload video files up to 3GB ...
Before writing this question I studied multiple threads on different forums and came across the solutions where it would be required to increase values of post_max_size
, upload_max_filesize
& memory_limit
, so I did that. With the help of phpinfo()
I found out that in my Laravel project has "Loaded Configuration File" from D:\wamp\bin\php\php8.1.10\php.ini
. So I opened the file and changed all the aforementioned fields to 3000M:
post_max_size = 3000M
upload_max_filesize = 3000M
memory_limit = 3000M
I made sure that those settings applied by checking in phpinfo()
.
Here's the config: phpinfo()
But this did not fix the issue, file submission still takes an unjustly long time.
web.php
Route::post('/add-video', [App\Http\Controllers\VideosController::class, 'createVideo'])->name('createVideo');
Form:
{{ Form::open(['route' => 'createVideo', 'method' => 'post', 'id' => 'videoForm', 'files' => true, 'enctype' => 'multipart/form-data']) }}
{{ Form::file('video'); }}
{{ Form::submit('Add Video') }}
{{ Form::close() }}
Controller function (basically does nothing at this point, since the problem is with file submission and NOT with processing logic):
public function createVideo(Request $request)
{
var_dump($request->video);
}
Would also like to note that the same action in a simple WAMP project (with the same php.ini
config), would usually take not more than 5 seconds. Both projects would run on the same hardware, with the same configuration, the only difference is that one of them is based on Laravel ...
Answers
It seems like you've taken the necessary steps to adjust the PHP configuration settings to allow for larger file uploads. Since you've already increased post_max_size
, upload_max_filesize
, and memory_limit
, and you're still experiencing slow upload times, there may be other factors at play.
Here are a few things you can consider:
-
Network Speed: Slow upload times could be due to network limitations, especially if you're testing locally. Make sure your network connection is stable and has sufficient bandwidth.
-
Server Environment: Check if there are any server-related issues that could be impacting upload speeds. This could include server load, disk I/O performance, or network congestion.
-
Debugging Tools: Use debugging tools to analyze the upload process and identify any bottlenecks. You can use browser developer tools to monitor network activity during the file upload process. Additionally, you can use server-side logging to track the progress of the file upload and identify any slow steps in the process.
-
Optimize File Upload Process: Consider optimizing the file upload process to minimize the time it takes to transfer large files. This could involve implementing features like resumable uploads or chunked uploads to break the file into smaller segments and upload them sequentially.
-
File Compression: If feasible for your use case, consider compressing the video files before uploading them. This can reduce the file size and improve upload speeds, especially for large video files.
-
Server Configuration: Double-check the PHP configuration settings to ensure that the changes you made are being applied correctly. Sometimes, changes to the
php.ini
file may not take effect immediately, or there may be overrides in other configuration files.
If you've tried these steps and are still experiencing slow upload times, it may be helpful to consult with a server administrator or a PHP expert to further diagnose the issue.
It seems like the issue might not be with the PHP configuration settings, but rather with the Laravel setup or the server environment. Here are a few things you can try to troubleshoot and optimize the file upload process in your Laravel project:
-
Check Server Configuration: Ensure that your server environment, such as Apache or Nginx, is configured to allow large file uploads. Look for any restrictions or timeouts that might be affecting the upload process.
-
Optimize Laravel Code: Review your Laravel code to ensure that it's not causing any unnecessary delays or bottlenecks during the file upload process. Make sure there are no excessive database queries or processing logic that might slow down the request handling.
-
Test with Different File Sizes: Try uploading files of different sizes to see if the issue is consistent across all file sizes or specific to larger files. This can help narrow down the problem and identify any patterns or trends.
-
Use Chunked Uploads: Consider implementing chunked file uploads, where large files are split into smaller chunks and uploaded asynchronously. This can help improve upload speed and reliability, especially for very large files.
-
Optimize Network Connection: Check the network connection between the client and server to ensure that there are no bandwidth limitations or network issues causing slow upload speeds.
-
Enable Compression: Enable compression for file uploads to reduce the amount of data transferred over the network. This can help improve upload speed, especially for large files.
-
Monitor Server Resources: Monitor server resources such as CPU and memory usage during file uploads to identify any resource bottlenecks or performance issues.
-
Use Queues for Processing: If the file upload process involves time-consuming tasks such as video processing, consider using Laravel queues to offload processing tasks and improve response times for file uploads.
By following these steps and carefully analyzing the file upload process in your Laravel project, you should be able to identify and address any issues causing slow upload speeds for large files.