上传并存储多重文件,然后插入html

时间:2018-10-04 19:21:43

标签: php

我想允许用户上传他们的图片以制作一个灯箱。 因此,我需要将一列中的多个图像存储到数据库中,并将这些图像附加到SELECT SUM(AggTotal) AggTotal,GLAccount FROM ( select amount_split1 as AggTotal, account as GLAccount, expheaderid -- <-- add this column from Expenses union all select amount_split2 as AggTotal, account2 as GLAccount , expheaderid from Expenses ) t1 where expheaderid = 57 GROUP BY GLAccount 中。

我认为我必须为每个图像分配一个唯一的键,但是我不知道该怎么做。

| AggTotal | GLAccount | |----------|-----------| | 0 | 0 | | 5.9 | 70410 | | 261.36 | 80400 | | 30 | 80401 | | 105 | 80402 | | 277.96 | 80440 | | 67.35 | 80870 | 返回以下错误:

致命错误:未捕获的PDOException:SQLSTATE [HY000]:C:\ xampp \ htdocs \ SAE \ yaute_love \ content \ account.php:190中的常规错误:堆栈跟踪:#0 C:\ xampp \ htdocs \ SAE \ yaute_love \ content \ account.php(190):PDOStatement-> fetchAll(2)#1 C:\ xampp \ htdocs \ SAE \ yaute_love \ index.php(38):include('C:\ xampp \ htdocs ...' )#2 {main}在第190行的C:\ xampp \ htdocs \ SAE \ yaute_love \ content \ account.php中抛出

这是数据库:

enter image description here

HTML:

HTML

PHP:

php script

请注意,我是php新手,谢谢。

2 个答案:

答案 0 :(得分:0)

对于初学者来说,此代码看起来不正确...

$files = $_FILES;
$count = count($_FILES['image-user']['name']);
for ($i = 0; $i < $count; $i++) {
    $_FILES['image-user']['name']= $files['image-user']['name'][$i];
    $_FILES['image-user']['type']= $files['image-user']['type'][$i];
    $_FILES['image-user']['tmp_name']= $files['image-user']['tmp_name'][$i];
    $_FILES['image-user']['error']= $files['image-user']['error'][$i];
    $_FILES['image-user']['size']= $files['image-user']['size'][$i];
};

您可能是想让它看起来像这样...

$files = $_FILES;
$count = count($_FILES['image-user']['name']);
for ($i = 0; $i < $count; $i++) {
    $files['image-user']['name'][$i]= $_FILES['image-user']['name'];
    $files['image-user']['type'][$i]= $_FILES['image-user']['type'];
    $files['image-user']['tmp_name'][$i]= $_FILES['image-user']['tmp_name'];
    $files['image-user']['error'][$i]= $_FILES['image-user']['error'];
    $files['image-user']['size'][$i]= $_FILES['image-user']['size'];
};

这也许有些道理,但是很奇怪的是,在循环之后的任何地方都没有使用$ files!

可能还有其他一些问题-特别是如果您不熟悉PHP并从其他地方修改代码。

答案 1 :(得分:0)

好吧,我明白了。

PHP:

<?
    function reArrangeFiles($arr) {
        foreach($arr as $key => $all) {
            foreach($all as $i => $val) {
                $new[$i][$key] = $val;
            };
        };
        return $new;
    };
    if (isset($_POST['mosaic-form'])) {
        $file_ary = reArrangeFiles($_FILES['image-user']);
        foreach ($file_ary as $file) {
            if (is_uploaded_file($file['tmp_name'])) {
                $valid_ext = array('jpg', 'jpeg', 'png');
                $valid_type = array('image/jpg', 'image/jpeg', 'image/png');
                $image_uploadedName = basename($file['name']);
                $name = strtolower(explode('.', $image_uploadedName)[0]);
                $ext = strtolower(explode('.', $image_uploadedName)[1]);
                if (in_array($ext, $valid_ext)) {
                    if (in_array($file['type'], $valid_type)) {
                        if (in_array('image/jpg', $valid_type) || in_array('image/jpeg', $valid_type)) {
                            $image = imagecreatefromjpeg($file['tmp_name']);
                        }
                        else if (in_array('image/png', $valid_type)) {
                            $image = imagecreatefrompng($file['tmp_name']);
                        }
                        $imgSize = getimagesize($file['tmp_name']);
                        $resizeSizeImg = 800;
                        if ($imgSize[0] > $imgSize[1]) {
                            $thumbnailWidth = $resizeSizeImg;
                            $thumbnailHeight = round($imgSize[1] / $imgSize[0] * $resizeSizeImg);
                        } else {
                            $thumbnailWidth = round($imgSize[0] / $imgSize[1] * $resizeSizeImg);
                            $thumbnailHeight = $resizeSizeImg;
                        }
                        $thumbnail = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
                        imagealphablending($thumbnail, false);
                        imagesavealpha($thumbnail, true);
                        imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $imgSize[0],$imgSize[1]);
                        if (in_array('image/jpg', $valid_type) || in_array('image/jpeg', $valid_type)) {
                            imagejpeg($thumbnail, 'public/img/upload/'.$name.'.'.$ext);
                            $name_image = $name.'.'.$ext;
                            $image_user = $sql_connection->prepare('INSERT INTO images (name, users_id) VALUES (:name, :users_id)');
                            $image_user->bindValue(':users_id', $user['id']);
                            $image_user->bindValue(':name', $name_image);
                            $image_user->execute();
                        }
                    } else {
                        echo '<p class="uk-text-danger">Ce n\'est pas une image !</p>';
                    }
                } else {
                    echo '<p class="uk-text-danger">Ce n\'est pas une image !</p>';
                }
            }
        };
    }
    $req_img = $sql_connection->prepare('SELECT * FROM images WHERE users_id = :users_id');
    $req_img->bindValue(':users_id', $user['id']);
    $req_img->execute();
    $img = $req_img->fetchAll(PDO::FETCH_ASSOC);
    if ($img) {
        foreach ($img as $val) { ?>
            <a href="public/img/upload/<? echo $val['name']; ?>">
                <img src="public/img/upload/<? echo $val['name']; ?>" alt="user image" />
            </a>
    <?  };
    } else {
        echo '<p class="uk-padding uk-padding-small uk-width-1-1">Vous n\'avez pas encore ajouté de photos.</p>';
    }
?>