在Slim Framework中使用POST发送文件


Send File with POST in Slim Framework

我需要上传文件以使用Slim在我的REST API上提供服务,在我的代码中我曾经使用:

$_datos = $app->request;

但相反,我使用了:

$_datos->post('FileInputName');

这没有分配任何东西。请帮帮我!完整代码:

$app->post('/aplicar',function() use($app) {
    try{
        //Code block for capture file and save it ¡Don't Work!
        if(basename($_FILES['_hojaVida']['name']) != null){
            if(!is_dir("./hojas_vida/"))
                mkdir("./hojas_vida/", 0777);
            $dir_subida = './hojas_vida/';
            $fichero_subido = $dir_subida . $POST['_nombre'].'-'.$POST['_apellido'].'-'.str_replace(' ', '-', basename($_FILES['_hojaVida']['name']));
            echo '<pre>';
            if (move_uploaded_file($_FILES['_hojaVida']['tmp_name'], $fichero_subido)) {
                echo "El fichero es válido y se subió con éxito.'n";
            } else {
                echo "¡Posible ataque de subida de ficheros!'n";
            }
        } //End Code block for capture file and save
        $app->response()->header("Content-Type", "application/json");
        $_datos = $app->request;
        $_connection = getConnection();
        $_connection->beginTransaction();
        $_dbh = $_connection->prepare(
            "INSERT INTO Aplicable(_id_VAC, Nombre_APL, Apellidos_APL, Curriculum_APL, Creacion_APL) 
            VALUES (:_id_VAC,:Nombre_APL,:Apellidos_APL,:Curriculum_APL,NOW());"
        );
        $_estado = $_dbh->execute(
            array(
                ':_id_VAC' => $_datos->post('_id_VAC'),
                ':Nombre_APL' => $_datos->post('_nombre'),
                ':Apellidos_APL' => $_datos->post('_apellido'),
                ':Curriculum_APL' => str_replace(' ', '-', $_datos->post('_curriculum'))
            )
        );
        if ($_estado){
            $app->response->headers->set("Content-type", "application/json");
            $app->response->status(200);
            $app->response->body(json_encode(array('estado'=>true,'mensaje'=>'Datos insertados correctamente.', 'Datos'=>var_dump($app->request))));
            $destinatario = $_datos->post('_correo');
            $asunto = "Subject text" ;
            //para el envío en formato HTML 
            $headers = "MIME-Version: 1.0'r'n";
            $headers .= "Content-type: text/html; charset=iso-8859-1'r'n";
            //dirección del remitente 
            $headers .= "From: name <info@domain.com>'r'n";
            //dirección de respuesta, si queremos que sea distinta que la del remitente 
            //$headers .= "Reply-To: mailto@domain.com'r'n"; 
            //ruta del mensaje desde origen a destino 
            $headers .= "Return-path: mail3@domain.com.co'r'n";
            //direcciones que recibián copia 
            $headers .= "Cc: copymail@domain.com'r'n";
            //direcciones que recibirán copia oculta 
            $headers .= "Bcc: mail1@domain.co,mail2@domain.co'r'n";
            mail($destinatario,$asunto,$cuerpo,$headers);
        }
        $_connection->commit();
        $_connection = null;
    }catch(PDOException $e){
        $app->response->status(500);
        echo "Error: Insertando persona: " . $e->getMessage();
    }
});

您可以使用此代码

        $tempFile = $_FILES['Filedata']['tmp_name'];
        $size = $_FILES['Filedata']['size']/1024;
        $pos = strrpos($_FILES['Filedata']['name'], ".");
        $label = $app->request->post('newFileName');
        $fileName = time()."-".$label.substr($_FILES['Filedata']['name'], $pos);