图片上传到博客系统问题

时间:2019-09-28 13:51:49

标签: php

我的1个php文件有问题,它没有上传它添加到数据库的图像好,但是../images文件夹中没有图像?而且我也收到以下错误

  

[28-Sep-2019 14:29:36欧洲/伦敦] PHP注意:未定义的变量:   在第176行的/home/blog/admin/add-post.php中签入

    <?php //include config
require_once('../includes/config.php');

//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); }
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Admin - Add Post</title>
  <link rel="stylesheet" href="../style/normalize.css">
  <link rel="stylesheet" href="../style/main.css">
  <script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
  <script>
          tinymce.init({
              selector: "textarea",
              plugins: [
                  "advlist autolink lists link image charmap print preview anchor",
                  "searchreplace visualblocks code fullscreen",
                  "insertdatetime media table contextmenu paste"
              ],
              toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
          });
  </script>
</head>
<body>

<div id="wrapper">

    <?php include('menu.php');?>
    <p><a href="./">Blog Admin Index</a></p>

    <h2>Add Post</h2>

    <?php

    //if form has been submitted process it
    if(isset($_POST['submit'])){

        // location where initial upload will be moved to
        $target = "images/" . $_FILES['postImage']['name'];
        $path = '../'.$target;

        //collect form data
        extract($_POST);

        //very basic validation
        if($postTitle ==''){
            $error[] = 'Please enter the title.';
        }

        if($postDesc ==''){
            $error[] = 'Please enter the description.';
        }

        if($postCont ==''){
            $error[] = 'Please enter the content.';
        }

        if(isset($_FILES['postImage'])){

            // find thevtype of image
            switch ($_FILES["postImage"]["type"]) {
            case $_FILES["postImage"]["type"] == "image/gif":
                move_uploaded_file($_FILES["postImage"]["tmp_name"], $path);
                break;
            case $_FILES["postImage"]["type"] == "image/jpeg":
                   move_uploaded_file($_FILES["postImage"]["tmp_name"], $path);
                break;
            case $_FILES["postImage"]["type"] == "image/pjpeg":
                   move_uploaded_file($_FILES["postImage"]["tmp_name"], $path);
                break;
            case $_FILES["postImage"]["type"] == "image/png":
                move_uploaded_file($_FILES["postImage"]["tmp_name"], $path);
                break;
            case $_FILES["postImage"]["type"] == "image/x-png":
                move_uploaded_file($_FILES["postImage"]["tmp_name"], $path);
                break;

            default:
                $error[] = 'Wrong image type selected. Only JPG, PNG or GIF accepted!.';
            }

        }

        if(!isset($error)){

            try {

                $postSlug = slug($postTitle);

                //insert into database
                $stmt = $db->prepare('INSERT INTO blog_posts_seo (postTitle,postSlug,postDesc,postCont,postDate) VALUES (:postTitle, :postSlug, :postDesc, :postCont, :postDate)') ;
                $stmt->execute(array(
                    ':postTitle' => $postTitle,
                    ':postSlug' => $postSlug,
                    ':postDesc' => $postDesc,
                    ':postCont' => $postCont,
                    ':postDate' => date('Y-m-d H:i:s'),
                ));
                $postID = $db->lastInsertId();

                if(isset($_FILES['postImage'])){

                    $stmt = $db->prepare('UPDATE blog_posts_seo SET postImage = :image WHERE postID = :postID') ;
                    $stmt->execute(array(
                        ':postID' => $postID,
                        ':image' => $target
                    ));
                }

                //add categories
                if(is_array($catID)){
                    foreach($_POST['catID'] as $catID){
                        $stmt = $db->prepare('INSERT INTO blog_post_cats (postID,catID)VALUES(:postID,:catID)');
                        $stmt->execute(array(
                            ':postID' => $postID,
                            ':catID' => $catID
                        ));
                    }
                }

                //redirect to index page
                header('Location: index.php?action=added');
                exit;

            } catch(PDOException $e) {
                echo $e->getMessage();
            }

        }

    }

    //check for any errors
    if(isset($error)){
        foreach($error as $error){
            echo '<p class="error">'.$error.'</p>';
        }
    }
    ?>

    <form action='' method='post' enctype="multipart/form-data">

        <p><label>Title</label><br />
        <input type='text' name='postTitle' value='<?php if(isset($error)){ echo $_POST['postTitle'];}?>'></p>

        <p><label>Image</label><br />
        <input type='file' name='postImage'></p>

        <p><label>Description</label><br />
        <textarea name='postDesc' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postDesc'];}?></textarea></p>

        <p><label>Content</label><br />
        <textarea name='postCont' cols='60' rows='10'><?php if(isset($error)){ echo $_POST['postCont'];}?></textarea></p>


        <fieldset>
            <legend>Categories</legend>

            <?php   

            $stmt2 = $db->query('SELECT catID, catTitle FROM blog_cats ORDER BY catTitle');
            while($row2 = $stmt2->fetch()){

                if(isset($_POST['catID'])){

                    if(in_array($row2['catID'], $_POST['catID'])){
                       $checked="checked='checked'";
                    }else{
                       $checked = null;
                    }
                }

                echo "<input type='checkbox' name='catID[]' value='".$row2['catID']."' $checked> ".$row2['catTitle']."<br />";
            }

            ?>

        </fieldset>

        <p><input type='submit' name='submit' value='Submit'></p>

    </form>

</div>

我尝试谷歌搜索错误,但是无法解决,我检查了重新更改的代码,但是仍然没有图像上传,数据库正在显示图像,但是没有显示文件夹

还是有类似176的问题,但是代码对我来说似乎是对的,有人可以给我一些提示吗?

0 个答案:

没有答案