将多张照片上传到特定位置

时间:2017-03-16 14:15:21

标签: php html mysql pdo

我一直试图创建一些可以将多张照片上传到特定位置并更改其名称的内容,尝试循环浏览文件但是某些内容无法正常工作我无法理解那是什么!所以请看一看,告诉我出了什么问题,这样我就可以学习,不会再犯同样的错误!感谢

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if(isset($_POST['product_submit']))
    {
        if(!empty($_POST['product_name']) && !empty($_POST['product_author']) && !empty($_POST['product_price']) && empty($_POST['product_search']))
        {
            if(is_numeric($_POST['product_price']))
            {

                $auth_key = round(microtime(true));

                if(isset($_FILES['photos']) && !empty($_FILES['photos']))
                {
                    $image_path = "product_images";

                    foreach ($_FILES['photos']['name'] as $name => $value)
                    {

                        $filename = stripslashes($_FILES['photos']['name'][$name]);
                        $extension = getExtension($filename);
                        $extension = strtolower($extension);

                        if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
                        {
                            $_SESSION['sucess'] = "Invalid extension.";

                            header("location: index.php");
                            exit();
                        }
                        else
                        {
                            $size = filesize($_FILES['photos']['tmp_name'][$name]);

                            if($size > 5120000)
                            {
                                $_SESSION['sucess'] = "You have exceeded the size limit.";

                                header("location: index.php");
                                exit();
                            }

                            $temp = explode('.', $filename);
                            $newfilename = mt_rand() . '_product.' . end($temp);
                            $name_path = "product_images/".$newfilename;

                            $suc = move_uploaded_file($_FILES['photos']['tmp_name'][$name], $name_path);        


                            if($suc)
                            {
                                $stmt = $connection->prepare("INSERT INTO images (auth_id, photo_location) VALUES (:code, :location)");
                                $stmt->bindParam(':code', $auth_key, PDO::PARAM_STR);
                                $stmt->bindParam(':location', $name_path, PDO::PARAM_STR);
                                $stmt->execute();       
                            }
                            else
                            {
                                $_SESSION['sucess'] = "Something went wrong!";

                                header("location: index.php");
                                exit();                                         
                            }
                        }   
                    }   
                }

                $query = "INSERT INTO products (name, author, price, date, code) VALUES (:name, :author, :price, NOW(), :code)";
                $stmt = $connection->prepare($query);

                $stmt->bindParam(':name', $_POST['product_name'], PDO::PARAM_STR);
                $stmt->bindParam(':author', $_POST['product_author'], PDO::PARAM_STR);
                $stmt->bindParam(':price', $_POST['product_price'], PDO::PARAM_STR);
                $stmt->bindParam(':code', $auth_key, PDO::PARAM_STR);

                $stmt->execute();

                if($stmt)
                {
                    $_SESSION['sucess'] = "Data inserted to database.";

                    header("location: index.php");
                    exit();
                }
                else
                {
                    $_SESSION['error'] = "Error while submiting data to database.";

                    header("location: index.php");
                    exit();
                }
            }
        }
        elseif (empty($_POST['product_name']) && empty($_POST['product_author']) && empty($_POST['product_price']) && !empty($_POST['product_search']))
        {
            $_SESSION['error'] = "You can't leave anything empty!";

            header("location: index.php");
            exit();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果输入文件的名称是照片[],则应使用:

for($i = 0; $i < count($_FILES['photo']); $i++)

然后:

$_FILES['photo'][$i]['tmp_name']

等等