【问题标题】:AWS EC2 file upload with php使用 php 上传 AWS EC2 文件
【发布时间】:2016-11-09 07:56:17
【问题描述】:

我在将图像上传到位于 aws ec2 实例中的 wamp 服务器时遇到一些问题,当我尝试将一些参数上传到我的表中时,除了图像本身之外,所有参数都成功上传,wamp 服务器安装在 Microsoft Windows amazon提供的Server 2012 R2 Base,80端口打开,httpd.conf文件也有权限。

这是来自 android 的代码,用于上传图像和参数:

public void uploadProduct() {
    //getting name for the image
    String name = editText.getText().toString().trim();
    String Description = editText2.getText().toString().trim();
    String Serial = editText3.getText().toString().trim();
    String price = editText4.getText().toString().trim();

    //getting the actual path of the image
    String path = getPath(fileUri);

    //Uploading code
    try {
        String uploadId = UUID.randomUUID().toString();

        //Creating a multi part request
        new MultipartUploadRequest(this, uploadId, URL_INDEX)
                .addFileToUpload(path, "image") //Adding file
                .addParameter("name", name)
                .addParameter("product_description", Description)
                .addParameter("product_serial", Serial)
                .addParameter("product_price", price)//Adding text parameter to the request
                .setNotificationConfig(new UploadNotificationConfig())
                .setMaxRetries(2)
                .startUpload(); //Starting the upload

    } catch (Exception exc) {
        Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

图片Uri获取代码:

 public String getPath(Uri uri) {
    String filePath = "";

    String wholeID = DocumentsContract.getDocumentId(uri);

    // Split at colon, use second item in the array
    String id = wholeID.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String sel = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = getApplicationContext().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            column, sel, new String[]{ id }, null);

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }
    cursor.close();
    return filePath;

}

和 php:

require_once 'db_config.php';

$upload_path = '上传/';

$upload_url = 'http://55.26.0.105/android/uploads/';

$response = 数组();

if($_SERVER['REQUEST_METHOD']=='POST'){

//checking the required parameters from the request
if( isset($_FILES['image']['name']) and isset($_POST['name']) and isset($_POST['product_description']) and isset($_POST['product_serial']) and isset($_POST['product_price'])){

    //connecting to the database
    $con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die('Unable to Connect...');

    //getting values from the request
    $product_serial_num = (int) $_POST['product_serial'];
    $product_price = (int) $_POST['product_price'];
    $product_title = $_POST['name'];
    $product_description = $_POST['product_description'];


    //getting img info from the request
    $fileInfo = pathinfo($_FILES['image']['name']);

    //getting the file extension
    $extension = $fileInfo['extension'];

    //file url to store in the database
    $file_url = $upload_url . $product_title . '.' . $extension;

    //file path to upload in the server
    $file_path = $upload_path . $product_title . '.'. $extension;

    //trying to save the file in the directory
    try{
        //saving the file
        move_uploaded_file($_FILES['image']['tmp_name'],$file_path);
        $sql = "INSERT INTO product_tbl(product_serial_num, product_price, product_title, product_description, product_img) VALUES ('$product_serial_num','$product_price','$product_title','$product_description', '$file_url');";

        //adding the path and name to database
        if(mysqli_query($con,$sql)){

            //filling response array with values
            $response['error'] = false;
            $response['url'] = $file_url;
            $response['name'] = $product_title;
        }
        //if some error occurred
    }catch(Exception $e){
        $response['error']=true;
        $response['message']=$e->getMessage();
    }
    //displaying the response
    echo json_encode($response);

    //closing the connection
    mysqli_close($con);
}else{
    $response['error']=true;
    $response['message']='Please choose a file';
}

} ?>

如有任何帮助,将不胜感激!

【问题讨论】:

    标签: php android file-upload amazon-ec2 apache-commons-fileupload


    【解决方案1】:

    可能的原因:

    1. 保存上传文件的 PHP 脚本位于没有“上传”目录的其他位置。
    2. 没有为“上传”目录授予适当的访问权限(在 Windows Server 中不太可能)。

    解决方案:

    1. 尝试使用静态路径作为上传位置。看到您的代码,您最好在“db_config.php”文件本身中定义路径。
    2. 通过转到“上传”目录的属性设置适当的访问权限。

    要确认文件是否上传成功,您应该将move_uploaded_file($_FILES['image']['tmp_name'],$file_path); 包装在if() 条件中。

    因为PHP move_uploaded_file()函数如果文件上传成功返回true,否则返回false

    【讨论】:

    • 谢谢你,工作,忘了文件夹权限:)
    猜你喜欢
    • 2016-03-11
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2018-02-03
    • 2015-11-19
    • 1970-01-01
    相关资源
    最近更新 更多