doufan8805 2017-02-22 04:00 采纳率: 0%
浏览 59

如何将文件上传到亚马逊EC2

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

?>
  • 写回答

1条回答 默认 最新

  • douzhan1238 2017-02-22 06:03
    关注

    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.

    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:

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度