如何将文件上传到Amazon EC2 [英] how to upload to files to amazon EC2

查看:937
本文介绍了如何将文件上传到Amazon EC2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从android将文件上传到Amazon EC2.但是,每当我上传文件时,都会收到意外的响应代码500错误.从我的理解来看,这是因为用户没有将文件上传到数据库的正确权限?我知道问题出在亚马逊EC2实例,而不是代码.但是下面是我的php代码,用于上传到服务器.首先是进入上传文件夹(/var/www/html/uploads)的正确方法吗?任何有关如何使它正常工作的帮助都将非常有用.

I am trying to upload files to an amazon EC2 from android. however whenever i go to upload the files I get a Unexpected response code 500 error. 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. but below is my php code for uploading to the server. first of all is that the correct way to enter the upload folder (/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";
    }
}

?>

推荐答案

几点注意事项:

首先,在提交到服务器之前,您应确保从编码为multipart/form-data的应用程序发送数据.

First, you should make sure to send your data from the app with the enctype of multipart/form-data before submitting to the server.

第二,尝试以下简化代码的变体:

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";
}

最后,假设您使用的是Apache并且用户名是www-data,则需要确保它可以写入上载文件夹:

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:

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

这篇关于如何将文件上传到Amazon EC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆