phpamazon-web-services

PHP move_upload_file function returns an error


<?php
        $uploaddir = 'media/';
        $file = basename($_FILES['photo']['name']);

        $uploadfile = $uploaddir . $file;

        if (move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile)) {
        echo "GOOD";
        } else {
        echo "ERROR";
        }
?>

I have this PHP script on my AWS EC2 Linux server. And it always return ERROR. I did chmod 777 and chmod 755 but still complains.

What is the problem? This code works just fine with another web server not Amazon EC2. Is there any limitation that I can not upload files (images) through PHP on Amazon EC2?


Solution

  • You can check it by function file_exists..

    you can modify your code for better understanding..

    if($_FILES['photo']['error'] == ''){
        $uploaddir = 'media/';
        $file = basename($_FILES['photo']['name']);
    
        $uploadfile = $uploaddir . $file;
        if(file_exists($_FILES['photo']['tmp_name'])){
           if (move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile)) {
              echo "GOOD";
           }
        } 
        else {
           echo "ERROR";
        }
    
    }
    else{
        echo "Error In Uploading File";
    }