php上传页面上的类别

时间:2018-11-20 18:31:13

标签: php mysql pdo

您好,我正在一个有上传页面的项目中,它在后台将dir /uploads/中的图片上传并保存到我的数据库中的user_images表(URL)中。 / p>

所以,我想拥有一个带有类别的画廊,但是当他们上传作品时,我需要一个dropdown menu之类的东西,他们在其中选择诸如汽车,房屋等之类的东西。

在我的画廊中,我需要显示类别。我在想所有带有ID的类别。

当我提交时,表格将是id和image_link,并且在图库页面中,我在此div中指定我要使用id=x的图像。

有人可以帮我吗?我真的很高兴! ps:对不起,我的英语不好

编辑:我需要使用pdo再次感谢!

upload.php;

    <center><form action="galleryupload.php" method="post" enctype="multipart/form-data">
            Select image to upload:
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="Upload Image" name="submit">
        </form></center>
</div>

galleryupload.php;

<?php
session_start();
if(isset($_SESSION['sess_user_id']) && $_SESSION['sess_user_id'] != "") {
} else {
    header('location:index.php');
}
?>
<body style="background-color: lightgray"></body>
<center><img src="../images/x.png"></center>

<?php
include "db.php";
$target_file2 = "random-dir/";
$target_dir = "random-dir/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$target_file3 = $target_file2 .$target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "<br>";
        echo "<br>";
        echo "<h1><center>File is an image - " . $check["mime"] . "." ."</center></h1>";
        $uploadOk = 1;
    } else {
        echo "<br>";
        echo "<h1><center>File is not an image.</center></h1>";
        $uploadOk = 0;
    }
}

if (file_exists($target_file)) {
    echo "<br>";
    echo "<h1><center>Sorry, file already exists.</center></h1>";
    echo "<h1><a href = gallery-edit.php>Go back </a></h1>";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "<br>";
    echo "<h1><center>Sorry, your file is too large.</center></h1>";
    echo "<h1><a href = gallery-edit.php>Go back </a></h1>";
    $uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
    echo "<br>";
    echo "<h1><center>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</center></h1>";
    $uploadOk = 0;
}
if ($uploadOk == 0) {
    echo "<br>";
    echo "<h1><center>Sorry, your file was not uploaded.</center></h1>";
    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo "<h1><center><a href = gallery-edit.php>Go back </a></center></h1>";


} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "<h1><center>The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.</center></h1>";
        echo " <meta http-equiv=\"refresh\" content=\"5;url=gallery-edit.php\" />";
        echo "<center><h1>You Will be redicted to user gallery in 5 seconds...</h1></center>";
        echo "<center><h1>If your browser doesn't support redict please<a href=gallery-edit.php> click here </h1></a></center>";
        $sql = "INSERT INTO user_image (image_link) VALUES (:image_link)";
        $stmt = $db->prepare($sql);

        $stmt->bindValue(':image_link', $target_file3);

        $result = $stmt->execute();
    } else {
        echo "<br>";
        echo "<center>Sorry, there was an error uploading your file.</center>";
        echo " <center><a href = gallery-edit.php>Go back </a></center>";
    }
}
?>

0 个答案:

没有答案