使用PDO和sql上传图像上传到文件路径

问题描述 投票:0回答:1

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

检查所有权限,所有权限似乎都正常服务器能够通过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 * /

<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>

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

php sql image pdo upload
1个回答
0
投票

检查file_uploads = off是否在file_uploads=On中编辑php.ini

并修复查询是这样的

$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);
}
© www.soinside.com 2019 - 2024. All rights reserved.