无法通过PHP将图像上传到数据库

时间:2014-08-05 15:45:14

标签: php jquery html css

我试图通过php上传图片并将其存储在我项目的另一个文件夹中。我确保设置了所有权限。以前,它工作正常,但现在我收到消息"没有插入!"。我的文件夹结构如下

我的项目中有3个文件夹(管理员,图片和包含),等级不是兄弟姐妹

发布的是我的代码。

  <?php

  session_start();

   if(!$_SESSION['admin_username'])

  {
    header("location:login.php");
  }

  ?>

  <html>
  <head>


    <title>insert your comments here.</title>

   <link rel = "stylesheet" href="admin_style.css">


  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-

   ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <style>
   #resizable 

    { 
    width: 150px;
   height: 150px; 
  padding: 0.5em; 
    }
  #resizable h3 
  {  
   text-align: center;
     margin: 0; 
  }
  </style>
 <script>


  $(function() {
  $( "#cname").resizable();
  $("#postauthor").resizable();
  $("#postkeywords").resizable();
  $("#postcontent").resizable();

 });
 </script>
 </head>
  <body>
 <div class = "header">

   <h1><a href = "index.php">WELCOME TO THE ADMIN PANEL </a></h1>
    </div>
   <div class = "main">
   <h1 align="center">this is where you can write your content</h1>     
   <form action = "insert_posts.php" method="post"  class = "cmxform"    
   enctype="multipart/form-data" id = "posts">
   <fieldset>
   <table width = "600" align = "center" border="10">
     <tr>

        <h1 align="center">insert your posts here </h1>
    </tr>
    <tr>

     <!--<label for="cname">post_title (required, at least 2 characters)</label>-->

        <td align="center">post_title</td>
        <td><input type = "text" size="30" name ="posttitle" id = 'cname' minlength =  

            '2' required/></td>
        </tr>
         <tr>
        <td align="center">post_date </td>
        <td><input type="text" size="30" name="postdate"  id= 'datepicker' required/>  
        </td>
    </tr>
    <tr>
        <td align="center">post_author</td>
        <td><input type="text" size="30" name="postauthor" id='postauthor' required/>  

     </td>
    </tr>
    <tr>
        <td align="center">post_image</td>
        <td><input type= "file"  name="postimage" required/></td>
    </tr>
    <tr>
        <td align="center">post_keywords</td>
        <td><input type="text" size="30" name="postkeywords" id = "postkeywords" 

        required/></td>
    </tr>
    <tr>
        <td align="center">post_content</td>
        <td><textarea cols="20" rows="20" name = "postcontent" id = "postcontent"  

      required/></textarea></td>
    </tr>
    <tr>
        <td colspan="5" align="center"><input type="submit" value="submit" name =  

       "submit" /></td>
    </tr>
   </table>
  </fieldset>
  </form>
  <script>
    $("#posts").validate();
    </script>
    <script>
    $(function() {
$( "#datepicker" ).datepicker();
 });
</script>
<script>
    $(function())
    {
        $("#datepicker").validate();
    }
</script>
  <?php
   include("../includes/connect.php");
   ?>
 <?php
 if(isset($_POST['submit']))
  { 
  $post_title = $_POST['posttitle'];
  $post_date  =  explode('/',$_POST['postdate']);
  $new_date = $post_date[2].'-'.$post_date[0].'-'.$post_date[1];

    $post_author = $_POST['postauthor'];

  $post_keywords = $_POST['postkeywords'];
  $post_content = $_POST['postcontent'];
 $post_image = $_FILES['postimage']['name'];
 $image_tmp = $_FILES['postimage']['tmp_name'];




  if(move_uploaded_file($_FILES['postimage']['tmp_name'],'../images/'.$post_image))
      {
     echo "inserted";
      }
  else 
     { 

      echo "not inserted";
      }
       ?>
     </div>
      <div class = "side">
            <h3>this is side bar</h3>
           <div class = "small_side">
        <h2><a href = "insert_posts.php"><input type = "button" value = "insertposts">  
   </a></h2>
        <h2><a href = "logout.php"><input type = "button" value="logout"></a></h2>
        <h2><a href = "view_posts.php"><input type = "button" value="view posts"  

         height="50"></a></h2>
        <h2><a href = "remove_posts.php"><input type = "button" value="remove_posts">
        </a></h2>
        </div>
        </div>
   <div class = "footer">
        <h3>this is the footer</h3>
    </div>
    </body>
   </html>

1 个答案:

答案 0 :(得分:0)

尝试打印 - var_dump() - $ _FILES数组,检查是否发布了任何内容,或者客户端是否有错误。

在您的脚本中,在下一行之前:

if(move_uploaded_file($_FILES['postimage']['tmp_name'],'../images/'.$post_image))

使用以下代码添加一行:

var_dump($_FILES);

运行脚本,您将获得该阵列包含的内容的打印版本。然后,检查文件是否正确接收(检查tmp_namename是否合适)。

如果这些参数看起来不错,则必须是权限问题(或磁盘已满)。确保apache在/ images /文件夹中具有写入权限