将php native转换为slim框架样式

时间:2016-03-22 11:14:00

标签: php slim

我在PHP中有这个功能,我从本教程http://www.andevindo.com/upload-file-volley

获得
<?php

// array untuk json
$response = array();

if (isset($_FILES['image'])) {
    if(isset($_POST['directory'])){
        $directory = $_POST['directory'];
        $full_directory_path = '../' . $directory;

        //Pengecekan folder, sudah tersedia atau belum
        if(!is_dir($full_directory_path)){

            //Pembuatan folder baru
            mkdir($full_directory_path, 0777, true);
        }
        //Menentukan tempat file akan disimpan
        $target_path = $full_directory_path . '/' . basename($_FILES['image']['name']);
        if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {

            //File gagal dipindahkan ke server, biasanya karena folder yang dituju tidak tersedia
            $response['kode'] = 1;
            $response['pesan'] = "File tidak dapat dipindahkan ke server";
            echo json_encode($response);
        }else{
            // File berhasil diupload
            $response['kode'] = 2;
            $response['pesan'] = "File berhasil diupload";
            $response['gili'] = $full_directory_path;

            echo json_encode($response);
        }
    }else{

    }
} else {

    //Jika file tidak terkirim dari android
    $response['kode'] = 0;
    $response['pesan'] = 'File tidak terkirim ke server';
    echo json_encode($response);
}
?>

实际上我想将它转换为纤薄的框架风格,因此它将具有api风格。我已经尝试过这个

$app->post('/gambar', function() use($app) {
            // check for required params

            $response = array();

            if (isset($_FILES['image'])) {
                if(isset($_POST['directory'])){
                    $directory = $_POST['directory'];
                    $full_directory_path = '../' . $directory;

                    //Pengecekan folder, sudah tersedia atau belum
                    if(!is_dir($full_directory_path)){

                        //Pembuatan folder baru
                        mkdir($full_directory_path, 0777, true);
                    }
                    //Menentukan tempat file akan disimpan
                    $target_path = $full_directory_path . '/' . basename($_FILES['image']['name']);
                    if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {

                        //File gagal dipindahkan ke server, biasanya karena folder yang dituju tidak tersedia
                        $response['kode'] = 1;
                        $response['pesan'] = "File tidak dapat dipindahkan ke server";
                        echo json_encode($response);
                    }else{
                        // File berhasil diupload
                        $response['kode'] = 2;
                        $response['gili'] = $target_path;
                        $response['pesan'] = "File berhasil diupload";
                        echo json_encode($response);
                    }
                }else{

                }
            } else {

                //Jika file tidak terkirim dari android
                $response['kode'] = 0;
                $response['pesan'] = 'File tidak terkirim ke server';
                echo json_encode($response);
            }});

但它给了我错误。任何帮助,我会很感激

1 个答案:

答案 0 :(得分:2)

实际上它只是愚蠢的错误,你不能只使用$ _POST苗条,你可以试试改变这个$directory = $_POST['directory']; 在这个$directory = $app->request->post('directory');中,请阅读如何处理苗条v2 http://docs.slimframework.com/ 让我知道它是否有效