
正文
php多张图片插入数据表 php上传多张图片
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
新手请教多个图片上传,怎么写入数据库(PHP)
原理都一样,循环接参,你打印一下它的参数就够就明白了,存储数据库,可以将图片路径之间用|或者逗号,或者json 系列化来存储:
?php
print_r($_FILES);//你可以打印一下它,看看它的结构
if(!empty($_FILES)){
foreach($_FILES['file']['name'] as $k=$v){
//$_FILES["file"]都变成$_FILES["file"][$k]操作进行操作吧
if ($_FILES['file'][$k]['error'] == 0) {
$filetype = array("jpg","JPG");
$arr = explode(".", $_FILES["file"][$k]["name"]);
...//同样的代码
}
}
}
?
form name="" action="" method="post" enctype="multipart/form-data"
input type="file" name="file[]" /br /
input type="file" name="file[]" /br /
input type="file" name="file[]" /br /
input type="file" name="file[]" /br /
input type="file" name="file[]" /br /
input type="file" name="file[]" /br /
input type="submit" name="a" value="提交" /
/form
相关问答
Q1: 怎样把图片插入到数据库中 php
保存图片到数据库做什么?保存到本地使用起来也方便,真要保存通过base64字符串保存。
?php
header('Content-type:text/html;charset=utf-8');
//读取图片文件,转换成base64编码格式
$image_file = './image123.jpg';
$image_info = getimagesize($image_file);
$base64_image_content = "data:{$image_info['mime']};base64," . chunk_split(base64_encode(file_get_contents($image_file)));
// $base64_image_content 输入到数据库
//保存base64字符串为图片
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
$new_file = "./test.{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
echo '新文件保存成功:', $new_file;
}
}
?
img src="?php echo $base64_image_content;?" /
Q2: 请问:php如何把上传的多张图片存入数据库的同一个字段中呢?
input type="file[]" name="photo" /
input type="file[]" name="photo" /
input type="file[]" name="photo" /
这样会成为一个数组,存的时候把这个数组用implode() 函数分割成字符串,取出来的时候可以使用explode函数(这个方法存的是路径)
还有一种方法是直接存二进制的,这种方法几乎不用对数据库压力很大,而且删除操作会留下很大的磁盘碎片
但是要了解
给你个电子书上面都有
php多张图片插入数据表的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php上传多张图片、php多张图片插入数据表的信息别忘了在本站进行查找喔。







