PHP多个图像存储记录到数据库

时间:2015-09-06 09:38:19

标签: php mysql database phpmyadmin php-5.3

我为多个图像上传器制作了一个代码,但只是第一个图像存储到mysql表并且都存入路径,有些帮助吗?

这是我的代码:

if(!empty($_FILES['file'])){
        foreach($_FILES['file']['name'] as $key => $name){
            if($_FILES['file']['error'][$key] == 0){
                $temp = $_FILES['file']['tmp_name'][$key];

                $ext = explode('.', $name);
                $ext = strtolower(end($ext));

                $file = md5_file($temp) . time() . '.' . $ext;              

                if(in_array($ext, $allowed) === true && move_uploaded_file($temp, "product_images/{$file}") === true){
                    $path = 'product_images';
                     mysql_query("INSERT INTO `products` (img_path, img_name) VALUES ('$path', '$file')");                                                  
                }else{
                    $errors[] = 'Format slike mora biti .jpg ili .png';
                }           
            }       
        }                       
}

0 个答案:

没有答案