[英]slim php framework image upload put database
我是瘦身php框架的新手,我想通过POST
上传图片并将文件名放在数据库中,有人可以给我一些示例代码。
Here's the router: 这是路由器:
$app->post('/', 'uploadFile');
this will point to the function below: 这将指向以下功能:
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. 如果有人有更好的答案,欢迎改变我的。
Slim的创建者创建了一个库,通过Slim处理文件上传: https : //github.com/brandonsavage/Upload
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.