苗条的php框架图片上传数据库 [英] slim php framework image upload put database

查看:73
本文介绍了苗条的php框架图片上传数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是苗条的php框架的新手,我想上传图像并将文件名通过POST放入数据库中,有人可以给我一些示例代码.

I am new to slim php framework, I want to upload an image and put the file name in the database via POST, can some one kindly give me some example code.

推荐答案

这是路由器:

$app->post('/', 'uploadFile');

这将指向以下功能:

function uploadFile () {
    if (!isset($_FILES['uploads'])) {
        echo "No files uploaded!!";
        return;
    }
    $imgs = array();

    $files = $_FILES['uploads'];
    $cnt = count($files['name']);

    for($i = 0 ; $i < $cnt ; $i++) {
        if ($files['error'][$i] === 0) {
            $name = uniqid('img-'.date('Ymd').'-');
            if (move_uploaded_file($files['tmp_name'][$i], 'uploads/' . $name) === true) {
                $imgs[] = array('url' => '/uploads/' . $name, 'name' => $files['name'][$i]);
            }

        }
    }

    $imageCount = count($imgs);

    if ($imageCount == 0) {
       echo 'No files uploaded!!  <p><a href="/">Try again</a>';
       return;
    }

    $plural = ($imageCount == 1) ? '' : 's';

    foreach($imgs as $img) {
        printf('%s <img src="%s" width="50" height="50" /><br/>', $img['name'], $img['url']);
    }
}

如果有更好的答案,请欢迎更改我的内容.

If anyone have better answer, please be welcome to alter mine.

这篇关于苗条的php框架图片上传数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆