图像使用PDO和SQL上传到FILEPATH



我正在尝试将图像上传到文件路径,但我似乎无法使其工作。这是我以前使用过的代码,所有的都很好。我已经检查了权限,一切都很好。不知道我在做什么错。

检查了所有权限,所有权限似乎还可以服务器能够通过PHP上传

上传
 if (isset($_POST['file'])) {
$targetDir = "../images/gallery-images/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
$allowTypes = array('jpg','png','jpeg','gif','pdf');
if(in_array($fileType, $allowTypes)){
    move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath);
}
if (empty($fileName)) {
    $error = 'All fields are required!';
} else {
        $query = $pdo->prepare('INSERT INTO gallery gallery_image VALUES ?');
        $query->bindValue(1, $fileName);
        $query->execute();
        header('Location: gallery.php');
    }
}

/*表格 */

<form class="add-new-post" method="POST" action="gallery.php"  enctype="multipart/form-data">
    <div class="form-group">
        <label class="" for="featured">Add Image - 500px x 300px:</label>
         <input type="file" class="ml-3" name="file">
    </div>
    <input class="btn btn-sm btn-accent ml-auto" type="submit" value="Add Image" />
</form>

基本上什么都没有发生...

检查 file_uploads = off是否编辑到 php.ini

中的 file_uploads=On

和修复查询像这样

$sth = $dbh->prepare('INSERT INTO table_name (column1) VALUES (:calories) ');
$sth->bindParam(':calories', $calories);
$sth->execute();

也尝试使用strtolower();

$fileType = strtolower(pathinfo($targetFilePath,PATHINFO_EXTENSION));
$allowTypes = array('jpg','png','jpeg','gif','pdf');
if(in_array($fileType, $allowTypes)){
    move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath);
}

最新更新