Unable to open using mode r: fopen(): AWS Elastic Beanstalk

Viewed 5473

Error:Unable to open using mode r: fopen(): Filename cannot be empty I keep getting this error when I try to upload larger files (more than 5MB). I have uploaded the PHP app to AWS Elastic Beanstalk and I upload the files to AWS S3. I don't even have fopen() in the code.

Alson when I test the site using XAMPP I don't get this error.

This is the code I use to upload file to S3:

<?php

session_start();
require 'vendor/autoload.php';

  use Aws\S3\S3Client;
  use Aws\S3\Exception\S3Exception;

  // AWS Info
  $bucketName = 'tolga20.images';
  $IAM_KEY = '******************';
  $IAM_SECRET = '*************************';

  $feedback = '';
  $unqiue_num = mt_rand(1000, 9999);


  if(isset($_FILES['fileToUpload'])) {

    $user_set_id = $_POST['user_set_id'];



    // Connect to AWS
    try {
      // You may need to change the region. It will say in the URL when the bucket is open
      // and on creation.
      $s3 = S3Client::factory(
        array(
          'credentials' => array(
            'key' => $IAM_KEY,
            'secret' => $IAM_SECRET
          ),
          'version' => 'latest',
          'region'  => 'eu-west-2'
        )
      );
    } catch (Exception $e) {
      // We use a die, so if this fails. It stops here. Typically this is a REST call so this would
      // return a json object.
      die("Error: " . $e->getMessage());
    }

    $temp_name = explode(".", $_FILES["fileToUpload"]["name"]);
    $newfilename = $unqiue_num . "-" . $user_set_id . '.' . end($temp_name);

    // For this, I would generate a unqiue random string for the key name. But you can do whatever.
    $keyName = 'images/' . basename($newfilename);
    $pathInS3 = 'https://s3.eu-west-2.amazonaws.com/' . $bucketName . '/' . $keyName;

    // Add it to S3
    try {
      // Uploaded:
      $file = $_FILES["fileToUpload"]['tmp_name'];
      $s3->putObject(
        array(
          'Bucket'=>$bucketName,
          'Key' =>  $keyName,
          'SourceFile' => $file,
          'StorageClass' => 'REDUCED_REDUNDANCY'
        )
      );
    } catch (S3Exception $e) {
      die('Error:' . $e->getMessage());
    } catch (Exception $e) {
      die('Error:' . $e->getMessage());
    }
    //$feedback = 'File uploaded! Custom name: ' . '<b><i>' . $newfilename;
    $_SESSION['newfilename'] = $newfilename;
    header("Location: next.php");

  }
?>
2 Answers

Try to increase the POST_MAX_SIZE and UPLOAD_MAX_FILESIZE values in your php.ini!

$file = $_FILES["fileToUpload"]['tmp_name'];

change to

$file = $_FILES["fileToUpload"]['name'];

Hope it will solve your problem.

Related