Friday, May 6, 2022

[SOLVED] PHP Fileupload to AWS EC2

Issue

I'm currently working on a PHP-based fileupload to an AWS EC2 instance. Basically I just want to upload zip-files to a subdirectory called /uploads/. Let me show you my code so far:

<form enctype="multipart/form-data" action="index.php" method="POST">
   <input id="file-0a" class="file" name="zipfile" type="file" data-show-preview="false">
</form>

So any uploaded file should be in $_FILES['zipfile']

When it comes to PHP, my code looks as followed:

<?php 

if(isset($_FILES[zipfile])){
    error_reporting(E_ALL);

    $uploaddir = '/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['zipfile']['name']);

    echo '<pre>';
    if (move_uploaded_file($_FILES['zipfile']['tmp_name'], $uploadfile)) {
        echo "Success\n";
    } else {
        echo "Error!\n";
}

echo 'Debugging:';
print_r($_FILES);

print "</pre>";
}

?>

Print_r shows all necessary information, error-level is 0 and I get a tmp_name like /tmp/phpy3lQBV. But I still get the error from the else part. So I assume move_uploaded_files is returning false.

When it comes to run the script locally on XAMPP, everything works properly. I have set the chmod to 777 on the EC2 instance. The EC2 instance is running Apache 2.4.7 and PHP 5.5.9

I hope someone can help me with that.

Cheers, Hendrik


Solution

I solved the problem by assigning www-data as the user for /var/www/html directory. Before it was set to 'ubuntu', which is default assigned by AWS.

Basically sudo chown -R www-data var/www/html worked for me.



Answered By - Baloolaoo
Answer Checked By - David Goodson (WPSolving Volunteer)