简体   繁体   English

Slim Framework 3上传

[英]Slim Framework 3 Upload

I have problem upload files through slim framework 3 Slim\\Http\\UploadedFile. 我在通过苗条框架3 Slim \\ Http \\ UploadedFile上传文件时遇到问题。

My code: 我的代码:

$app->post('/upload', function ($req, $res, $args) {
    $setting = $this->settings;
    $uploadPath = $setting['upload']['path'];
    $file = $req->getUploadedFiles()['img'];
    $file->moveTo($uploadPath);
    return $res;
});

Result: 结果:

Slim Application Error
The application could not run because of the following error:

Details

Type: RuntimeException
Message: Error moving uploaded file hss.png to /home/xxx/web/slim3/app/../log
File: /home/xxx/web/slim3/vendor/slim/slim/Slim/Http/UploadedFile.php
Line: 237

I already found out the answer. 我已经找到答案了。 Thanks to @akrabat 感谢@akrabat

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Slim 3</title> <link rel="stylesheet" href="http://yegor256.github.io/tacit/tacit.min.css"> </head> <body> <h1>Upload a file</h1> <form method="POST" action="/upload" enctype="multipart/form-data"> <label>Select file to upload:</label> <input type="file" name="newfile"> <button type="submit">Upload</button> </form> </body> </html> 

 $app->post('/upload', function ($request, $response, $args) { $files = $request->getUploadedFiles(); if (empty($files['newfile'])) { throw new Exception('Expected a newfile'); } $newfile = $files['newfile']; // do something with $newfile }); 

使用此php 函数代替moveTo函数

//localhost store image

define('pic_root',"/var/www/html/api/app/photo"); define('pic_root',“ / var / www / html / api / app / photo”); define('pic_image'," http://localhost/api/app/photo "); define('pic_image',“ http:// localhost / api / app / photo ”);

$app->post('/adduser', function($request,$response){ $ app-> post('/ adduser',function($ request,$ response){

$post = $request->getParsedBody();

extract($post);

require 'db_connect.php';

$img =" ";
if($_FILES['photo']['error'] === 0){
    $files = $_FILES['photo'];
    $imgname = $files['name'];
    move_uploaded_file($files['tmp_name'], pic_image.'/'.$imgname);
    $img = pic_root.'/'.$imgname;
}

$q = "INSERT INTO users (name,phone,taluka,disticts,city,photo)
     VALUES ('".$name."','".$phone."','".$taluka."','".$disticts."','".$city."','".$img."')";

$user = $pdo->query($q);

$user = array(
                "status" => (bool)$user,
                "message" => "User Created"
            );
    return $response->withStatus(200)
        ->withHeader('Content-Type', 'application/json')
        ->write(json_encode($user,JSON_FORCE_OBJECT));        

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM