Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shadkamel's avatar

filesystem for large files

hey everyone, i want to build an application for storing large files, for example 4GB or more, as you know PHP has limit for uploading files, so what is the best way for storing big files ? and using services like Amazon s3 is required or not ?

0 likes
5 replies
LaryAI's avatar
Level 58

When dealing with large file uploads in a web application, there are several considerations and strategies you can employ to handle files efficiently and effectively. Here’s a solution that addresses your needs:

1. Use a Cloud Storage Service

Using a cloud storage service like Amazon S3 is highly recommended for storing large files. It provides scalability, reliability, and security, which are crucial for handling large data. Here’s how you can integrate Amazon S3 with your application:

  • Install AWS SDK for PHP: Use Composer to install the AWS SDK.

    composer require aws/aws-sdk-php
    
  • Configure AWS SDK: Set up your AWS credentials and configuration.

    use Aws\S3\S3Client;
    
    $s3 = new S3Client([
        'version' => 'latest',
        'region'  => 'your-region',
        'credentials' => [
            'key'    => 'your-access-key-id',
            'secret' => 'your-secret-access-key',
        ],
    ]);
    
  • Upload Files to S3: Use the SDK to upload files directly to S3.

    $result = $s3->putObject([
        'Bucket' => 'your-bucket-name',
        'Key'    => 'your-file-key',
        'SourceFile' => '/path/to/your/large-file',
    ]);
    
    echo "File uploaded successfully. File URL: " . $result['ObjectURL'];
    

2. Handle Large File Uploads

To handle large file uploads, you need to adjust PHP and server settings:

  • PHP Configuration: Update your php.ini file to allow larger uploads.

    upload_max_filesize = 5G
    post_max_size = 5G
    max_execution_time = 300
    
  • Nginx/Apache Configuration: Ensure your web server is configured to handle large file uploads. For Nginx, you might need to set:

    client_max_body_size 5G;
    

3. Use Chunked Uploads

For very large files, consider implementing chunked uploads. This involves splitting the file into smaller parts and uploading each part separately. This can be done using JavaScript on the client-side and handling each chunk on the server-side.

4. Direct Uploads to S3

To bypass PHP limitations, you can implement direct uploads to S3 from the client-side using pre-signed URLs. This allows the client to upload files directly to S3 without passing through your server.

  • Generate a Pre-signed URL:

    $cmd = $s3->getCommand('PutObject', [
        'Bucket' => 'your-bucket-name',
        'Key'    => 'your-file-key',
    ]);
    
    $request = $s3->createPresignedRequest($cmd, '+20 minutes');
    
    $presignedUrl = (string) $request->getUri();
    
  • Upload Using the Pre-signed URL: Use JavaScript to upload the file directly to S3 using the pre-signed URL.

Conclusion

Using a cloud storage service like Amazon S3 is a robust solution for handling large files. It offloads the storage and bandwidth requirements from your server, allowing you to focus on building your application. Additionally, consider using chunked uploads or direct uploads to S3 to efficiently manage large file transfers.

shadkamel's avatar

@Tray2 actually i never tested file uploading on server and in the local development it always needed to increase the upload_max_filesize variable, i think this link will help me a lot i will try, so thank you very much for your replay.

martinbean's avatar

@shadkamel For files larger than a few megabytes, you should be using multi-part uploading, whether the client-side (i.e. JavaScript) sends the file in “chunks”.

Services like S3 support this type of upload natively. To build it yourself would be a pain as you’d need to maintain state on the server such as which “file” an incoming chunk belongs to, and ensuring the chunks get written to the correct “part” of the file (i.e. if chunk 3 finishes before chunk 2).

1 like
shadkamel's avatar

@martinbean do you know any title or concept or a package for upload, please ? i want to do a research for it.

Please or to participate in this conversation.