2017-07-21 75 views
0
<?php //include config 
require_once('../includes/config.php'); 

include("function.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(isset($_POST['submit'])) 
    { 



     $cn=makeconnection(); 


    $target_dir = "subcatimages/"; 
    $target_file = $target_dir.basename($_FILES["t4"]["name"]); 
    $uploadok = 1; 
    $imagefiletype = pathinfo($target_file, PATHINFO_EXTENSION); 
    //check if image file is a actual image or fake image 
    $check=getimagesize($_FILES["t4"]["tmp_name"]); 
    if($check!==false) { 
     echo "file is an image - ". $check["mime"]. "."; 
     $uploadok = 1; 
    }else{ 
     echo "file is not an image."; 
     $uploadok=0; 
    } 
    //check file size 
    if($_FILES["t4"]["size"]>5000000){ 
     echo "sorry, your file is too large."; 
     $uploadok=0; 
    } 


    //aloow certain file formats 
    if($imagefiletype != "jpg" && $imagefiletype !="png" && $imagefiletype !="jpeg" && $imagefileype !="gif"){ 
     echo "sorry, only jpg, jpeg, Png & gif files are allowed."; 
     $uploadok=1; 
    }else{ 
     if(move_uploaded_file($_FILES["t4"]["tmp_name"], $target_file)){ 



    $s="insert into picture(Filename)values('" . basename($_FILES["t4"]["name"]) . "')"; 
    mysqli_query($cn,$s); 

    echo "<script>alert('Record Save');</script>"; 


     } else{ 
      echo "sorry there was an error uploading your file."; 
     }} 
    //if form has been submitted process it 


     //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($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(); 

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

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


     <input type='file' name='t4'> 

     <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']."' > ".$row2['catTitle']."<br />"; 
      } 

      ?> 

     </fieldset> 

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

    </form> 

</div> 

這是我的PHP與HTML表單的代碼示例,請建議我 我能當我使用的文件和表單代碼分別 但不工作上傳文件一起。文件上傳不工作的結合PHP的形式,而是分開工作

顯示錯誤如下

注意:未定義指數:T4在 F:\ XAMPP \ htdocs中\ new_travel \博客\ ADMIN \上線48加-post.php中

注意:未定義指數:T4在 F:\ XAMPP \ htdocs中\ new_travel \博客\ ADMIN \上線外接post.php中52

警告:和getimagesize():文件名不能 F爲空:\ XAMPP \ htdocs中\ new_travel第52行文件中的\ blog \ admin \ add-post.php爲 不是圖像。通知:未定義指數:中的t4 F:\ XAMPP \ htdocs中\ new_travel \博客\管理員\上的行添加-post.php中61

通知:未定義變量:imagefileype在 F:\ XAMPP \ htdocs中\ new_travel \博客\管理\ add-post.php在線68對不起, 只有jpg,jpeg,Png & GIF文件是允許的。

+1

[PHP:「注意:未定義的變量」,「注意:未定義的索引」和「注意:未定義的偏移量」的可能重複](https://stackoverflow.com/questions/4261133/php-notice-undefined-變量通知未定義索引和通知undef) – Jens

回答

0

從HTML格式的文件上傳不了,除非你正確設置表單的內容類型:

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

沒有這一點,文件數據將不會被髮送到服務器。

+0

謝謝ADyson –

+0

@ Drgeek沒問題。如果答案有幫助,請記住將其標記爲「已接受」的答案 - 謝謝:-) – ADyson