简体   繁体   English

如何将文件上传到Amazon EC2

[英]how to upload to files to amazon EC2

I am trying to upload files to an amazon EC2 from android. 我正在尝试将文件从android上传到Amazon EC2。 however whenever i go to upload the files I get a Unexpected response code 500 error. 但是,每当我上传文件时,都会收到意外的响应代码500错误。 From what I uderstand this is because the user doesnt have the correct permissions to upload files to the database? 据我了解,这是因为用户没有正确的权限将文件上传到数据库? I know that the problem is with the amazon EC2 instance rather than the code. 我知道问题出在亚马逊EC2实例,而不是代码。 but below is my php code for uploading to the server. 但是下面是我的php代码,用于上传到服务器。 first of all is that the correct way to enter the upload folder (/var/www/html/uploads) ? 首先是进入上传文件夹(/ var / www / html / uploads)的正确方法吗? Any help on how i can get this working would be great. 我如何获得此工作的任何帮助将是巨大的。

<?php
if(isset($_POST['image'])){
    echo "in";
    $image = $_POST['image'];
    upload($_POST['image']);
    exit;
}
else{
    echo "image_not_in";
    exit;
}


function upload($image){
    $now = DateTime::createFromFormat('U.u', microtime(true));
    $id = "pleeease";

    $upload_folder = "/var/www/html/upload";
    $path = "$upload_folder/$id.jpg";

    if(file_put_contents($path, base64_decode($image)) != false){
        echo "uploaded_success"
    }
    else{
        echo "uploaded_failed";
    }
}

?>

Just a few notes: 只是一些注意事项:

First, you should make sure to send your data from the app with the enctype of multipart/form-data before submitting to the server. 首先,在提交到服务器之前,您应确保使用multipart/form-data的enctype从应用程序发送数据。

Second, try variants of this simplified code: 其次,尝试以下简化代码的变体:

if(isset($_FILES['image']))
{
   $fileTmp = $_FILES['image']['tmp_name'];
   $fileName = $_FILES['image']['name'];

    move_uploaded_file($fileTmp, "/var/www/html/uploads/" . $fileName);
    echo "Success";
}
else
{
    echo "Error";
}

And finally, assuming you're using Apache and the user name is www-data, you'll need to make sure it can write to the upload folder: 最后,假设您使用的是Apache并且用户名为www-data,则需要确保它可以写入上载文件夹:

sudo chown www-data:www-data /var/www/html/uploads
sudo chmod 755 /var/www/html/uploads

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM