超薄框架3上传

我有问题上传文件通过超薄框架3 Slim \ Http \ UploadedFile。</ p>

我的代码:</ p>

  $ app-&gt; post('/ upload',function($ req,$ res,$ args){
$ setting = $ this - &gt;设置;
$ uploadPath = $ setting ['upload'] ['path'];
$ file = $ req-&gt; getUploadedFiles()['img'];
$ file-&gt; moveTo ($ uploadPath);
返回$ res;
});
</ code> </ pre>

结果:</ p>

  Slim 应用程序错误
由于以下错误,应用程序无法运行:

详细信息

Type:RuntimeException
Message:将上传的文件hss.png移动到/home/xxx/web/slim3/app/../log时出错
文件:/home/xxx/web/slim3/vendor/slim/slim/Slim/Http/UploadedFile.php
Line:237
</ code> </ pre>
</ div>

展开原文

原文

I have problem upload files through slim framework 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

3个回答



我已经找到了答案。 感谢@akrabat </ p>

</ p>





 &lt;!DOCTYPE html&gt;

&lt; html&gt;

&lt; head&gt ;

&lt; meta charset =“utf-8”&gt;

&lt; title&gt; Slim 3&lt; / title&gt;

&lt; link rel =“stylesheet”href =“http:// yegor256.github.io/tacit/tacit.min.css">
<&lt; / head&gt;

&lt; body&gt;

&lt; h1&gt;上传文件&lt; / h1&gt;

&lt; form method =“POST”action =“/ upload”enctype =“multipart / form-data”&gt;

&lt; label&gt;选择要上传的文件:&lt; / label&gt;

&lt; ; input type =“file”name =“newfile”&gt;

&lt; button type =“submit”&gt; Upload&lt; / button&gt;

&lt; / form&gt;

&lt; / body&gt; ;

&LT; / HTML&GT; </代码> </ PRE>

</ DIV>

</ DIV>

</ p>





  $ app-&gt; post('/ upload',function($ request,$ response,$ args){ 
$ files = $ request- &gt; getUploadedFiles();

if(empty($ files ['newfile'])){
throw new Exception('Expected a newfile');

}

$ newfile = $ files ['newfile'];

//用$ newfile做一些事情
}); </ code> </ pre>

</ div> \ n </ div>

</ div>

展开原文

原文

I already found out the answer. Thanks to @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
});

</div>

doujiaoben28596
doujiaoben28596 我实现完全相同,但我仍然无法通过邮递员上传文件。 总是抛出密钥丢失的异常
3 年多之前 回复



使用此php function 而不是moveTo函数</ p>
</ div>

展开原文

原文

use this php function instead moveTo function

dongyou8701
dongyou8701 我已经找到了akrabat.com/psr-7-file-uploads-in-slim-3
4 年多之前 回复



  // localhost store image 
</ code> </ pre>

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

$ app-&gt; post('/ adduser',function($ request,$ response) ){</ p>

  $ post = $ request-&gt; getParsedBody(); 

extract($ post);

require'db_connect.php';
\ n $ 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用户(姓名,电话,taluka,disticts,城市,照片)
VALUES('”。$ name。“','”。$ phone。“','”。$ taluka。“','” 。$ disticts。“','”。$ city。“','”。$ img。“')”;

$ user = $ pdo-&gt; query($ q);

$ user = array(
“status”=&gt;(bool)$ user,
“message”=&gt;“用户创建”
);
返回 $ response-&gt; withStatus(200)

  • &gt; withHeader('Content-Type','application / json')
  • &gt; write(json_encode($ user,JSON_FORCE_OBJECT));

    </ code> </ pre>

    }); </ p>
    </ div>

展开原文

原文

//localhost store image

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

$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));        

});

drruhc4944
drruhc4944 你可以添加一行或两行,说明这段代码在多大程度上解决了问题,或者它与问题中的代码有多远? 要获得正确的代码限制,您可以使用Ctrl + k。
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐