使用PDO和SQL无法将图片上传到文件路径

时间:2019-02-14 13:30:20

标签: php sql image pdo upload

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

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

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

1 个答案:

答案 0 :(得分:0)

检查.as-console-wrapper { max-height: 100% !important; top: 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();