Unable to upload files to EC2 Amazon Linux 2023

0

Hi,

I have just deployed an EC2 Amazon Linux 2023 Instance. I have installed PHP8.2 FPM/FastCGI, Apache and uploaded a simple PHP script to upload images. It's a very simple script, really.

The issue is that I'm getting this message "Failed to open stream: No such file or directory in ". Which means that the temp file was not uploaded to /tmp folder.

The print_r($_FILES) displays this:

Array ( [name] => 14955828_1517805064899902_6497754076576078705_n.jpg [full_path] => 14955828_1517805064899902_6497754076576078705_n.jpg [type] => image/jpeg [tmp_name] => /tmp/phpxuSNXU [error] => 0 [size] => 72072 )

The error result is 0, which means that the file was uploaded. However, when I check the /tmp folder, the uploaded file is not there.

I have followed these instructions https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2023.html

Please let me know why I am not able to run that simple script. It works on other servers.

I look forward to hearing from you.

Thanks

FranZ
asked 6 months ago539 views
3 Answers
0
Accepted Answer

Hello.

The /tmp folder is a temporary area.
So, it will disappear after some time or when you restart the OS.
Therefore, the file may have been deleted.

In addition, even if there appears to be no problem on the program side, it may actually not be possible to upload due to a permission error, etc.
So please check the php logs and Apache logs for any errors.

profile picture
EXPERT
answered 6 months ago
  • thanks for your reply. yeah, I know this is a temporary area. The main issue is that the file is not being uploaded. I have checked SELinux as well. There are no error logs. This is a simple php script to upload files.

  • I would also like to check if it works, so could you please share the php script etc.

  • thanks for your your help. I appreciate it. The HTML code is this <form action="fileUploadScript.php" method="post" enctype="multipart/form-data"> Upload a File: <input type="file" name="the_file" id="fileToUpload"> <input type="submit" name="submit" value="Start Upload"> </form>

  • I have a question about PHP code. Looking at the code, it seems that the file is uploaded to "/uploads/" in the current directory where the PHP script is located, but have you checked "/uploads/" in the directory where the PHP script is located?

0

In what directory is this PHP script running?
This PHP script creates a directory called "/uploads/" in the same directory as the script and saves images in that directory.
I think you are misunderstanding something.

profile picture
EXPERT
answered 6 months ago
  • Please change the permissions of "/var/www/html" using the following command.

    sudo chown apache:apache /var/www/html -R
    
  • When the PHP script is executed after executing this command, the image will be uploaded to "/var/www/html/uploads/".

  • Thank you for your message. The issue is not the target folder but the $source_file = $_FILES["fileToUpload"]["tmp_name"]; retrieves the temporary location of the uploaded file. This issue is related to the temporary file to upload. The script works perfect on other server providers. I have been using it for years. I think the issue is that PHP is not able to write on the temporary folder.

  • Does this mean that "/tmp/phpgbXS3n" remains if it is a server from another provider? When I run it on my EC2, I have confirmed that images are saved normally in "/var/www/html/uploads/", but is this not normal operation?

0

I modified the code as below.
I think the cause was that there was no "/uploads/" directory in the "/tmp" directory.
So I modified the code to create the directory if it doesn't exist.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

    $currentDirectory = getcwd();
    $uploadDirectory = "/uploads/";

    if (!file_exists($currentDirectory . $uploadDirectory)) {
      mkdir($currentDirectory . $uploadDirectory, 0755, true);
    }

    $errors = []; // Store errors here

    $fileExtensionsAllowed = ['jpeg','jpg','png', 'txt']; // These will be the only file extensions allowed 

    $fileName = $_FILES['the_file']['name'];
    $fileSize = $_FILES['the_file']['size'];
    $fileTmpName  = $_FILES['the_file']['tmp_name'];
    $fileType = $_FILES['the_file']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));

    $uploadPath = $currentDirectory . $uploadDirectory . basename($fileName); 

    if (isset($_POST['submit'])) {

      if (! in_array($fileExtension,$fileExtensionsAllowed)) {
        $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
      }

      if ($fileSize > 4000000) {
        $errors[] = "File exceeds maximum size (4MB)";
      }

      if (empty($errors)) {
        $didUpload = copy($fileTmpName, $uploadPath);

        if ($didUpload) {
          echo "The file " . basename($fileName) . " has been uploaded";
        } else {
          echo "An error occurred. Please contact the administrator.";
        }
      } else {
        foreach ($errors as $error) {
          echo $error . "These are the errors" . "\n";
        }
      }

    }
?>
profile picture
EXPERT
answered 6 months ago
  • Thank you for your reply. Unfortunately, the tweak is not resolving the issue. I am getting [tmp_name] => /tmp/phpgbXS3n when I print_r($_FILES). The temp file is not being uploaded to the PHP temp folder. I am getting this message "Failed to open stream: No such file or directory i" which means that the phpgbXS3n file does not exist.

  • To make it more clear $fileTmpName = $_FILES['the_file']['tmp_name'] does not exist. This file is created by PHP and it should be placed in /tmp folder

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions