2013-08-16 50 views
0

我已經編寫了以下代碼來更新存儲在「文件」中的圖像。當我使用它來上傳圖片時,代碼工作正常,但是當我使用它來更新圖片時,它給了我'無效文件'的錯誤消息。它在第一個'if'語句中出現問題,並直接轉到其他語句消息'無效文件'。請檢查並引導我。

感謝

<?php 

var_dump($_REQUEST); 
session_start(); 
error_reporting(E_PARSE); 
if (isset ($_SESSION['username'])) 
{ 

    //echo "<div id='nav'"; 
    echo "<ul><hr> 
    <li><a href='insert_product.php' >Add Product | </a></li> 
    <li><a href='add_category.php'> Add Category </a></li> 
     <li><a href='sub_categories.php'> Add Sub-Category </a></li> 
    <li><a href = 'view_products.php' >View All Products</a> </li> 
    <li><a href = 'all_categories.php' >View All Categories</a> </li> 
     <li><a href='view_all_sub_categories.php'>View All Sub Categories</a></li> 
    </ul></hr>"; 

    include 'connect.php'; 


    $category_id= $_GET['category_id']; 

    $query=   "select * from category where category_id= $category_id"; 
    $query_run=  mysql_query($query); 
    $fetch=   mysql_fetch_array($query_run); 
    $name=   $fetch['name']; 


echo " 
      <form action='update_category.php?category_id=$category_id' method='POST' > 
      <table border=1> 

      <tr><td> 
       Category Name:</td><td><input type='text' name='category' value='$name' /> 
      </td></tr> 

      <tr><td>  
       Image1:</td><td> <input type='file' name= 'image' > 
      </td></tr> 

      <tr><td>    
       <input type='submit' value='Update' /> 

      </td></tr> 

      </form> </table> "; 

      echo "<form action='delete_category.php?category_id=$category_id' method='POST'> 
       <input type='submit' value='Delete'> 
       </form> 
      "; 

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

      $category_name =   $_POST['category']; 

      $query_update="UPDATE category SET name = '$category_name' 
           WHERE category_id =$category_id "; 


         if (mysql_query($query_update)) 
         { 
          echo "Records updated"; 

          } 
          else 
          { 

           echo mysql_error(); 
           } 




        /*------------------- 
         IMAGE QUERY 
        --------------------*/ 
        $allowedExts = array("gif", "jpeg", "jpg", "png"); 
        $extension = end(explode(".", $_FILES["image"]["name"])); 

         if ((($_FILES["image"]["type"] == "image/gif") 
         || ($_FILES["image"]["type"] == "image/jpeg") 
         || ($_FILES["image"]["type"] == "image/jpg") 
         || ($_FILES["image"]["type"] == "image/pjpeg") 
         || ($_FILES["image"]["type"] == "image/x-png") 
         || ($_FILES["image"]["type"] == "image/png")) 
         //&& ($_FILES["file"]["size"] < 200000) 
         && in_array($extension, $allowedExts)) 
         { 
          if ($_FILES["image"]["error"] > 0) 
          { 
          echo "Return Code: " . $_FILES["image"]["error"] . "<br>"; 
          } 
          else 
          { 
           echo "Upload: " . $_FILES["image"]["name"] . "<br>"; 
           echo "Type: " . $_FILES["image"]["type"] . "<br>"; 
           echo "Size: " . ($_FILES["image"]["size"]/200000) . " kB<br>"; 




            $image_name=  $_FILES["image"]["name"]; 
            $random_name=  rand().$_FILES["image"]["name"]; 

            $path=    move_uploaded_file($_FILES["image"]["tmp_name"], 
                   "upload/categories/" . $random_name); 


            $folder="upload/categories/" .$random_name;   

             echo "Stored in: "."upload/categories/". $random_name; 

            echo $sql= "update category_images set name='$image_name' , 
               location='$folder' where category_id= $category_id  "; 


             $result = mysql_query($sql); 
             if ($result) 
             { 

             echo "successfull"; 
             } 
             else { 
               echo mysql_error(); 
               } 


          } 

         } 

        else 
         { 
         echo "Invalid file". $_FILES["image"]["error"]; 
         } 


         /*----------------- 
         IMAGE QUERY END 
         ------------------*/      

      } 

      /* 
       echo $sql= "update category_images set name='$image_name' , 
          location='$folder' where category_id= $category_id  "; 
       */   
} 

else 
{ 
    echo "You Must need to Log in to Visit this Page"; 

    } 


?> 
+1

證明$ _POST ['category']實際上是設置了var_dump()它。 – Cups

+0

var_dump的輸出($ _ POST ['category'])= string(2)「12」 –

+0

它更新類別的名稱,但不更新類別的圖像。 –

回答

1

我只是改變了這種

<form action='update_category.php?category_id=$category_id' method='POST' > 

<form enctype="multipart/form-data" action='update_category.php?category_id=$category_id' method='POST' > 

它解決了這個問題。

1

請參見該代碼:

<?php 
include 'connect.php'; 
extract($_POST); 
$category_id= $_GET['category_id']; 
if(!empty($_FILES['image']['name'])) 
    { 
    $filename = $_FILES['image']['name']; 
    $filesize = $_FILES['image']['size']; 
    $ext = strtolower(substr(strrchr($filename, "."), 1)); 
    $image_size = ($filesize/1024); 
    if($filename != '') 
    { 
    if($ext == 'jpg' or $ext == 'jpeg' or $ext == 'gif' or $ext == 'png') 
{ 
    $max_qry = mysql_query("select max(category_id) category_id from category"); 
    $max_row = mysql_fetch_array($max_qry); 
    $id=$max_row['category_id']+1; 
    $fname= $id.str_replace(" ","_",$filename); 
    $image_path = "img/".$fname; 
    $up_path = "img/".$fname; 
    copy($_FILES['image']['tmp_name'],$image_path); 
    chmod($image_path,0777); 
    }  
    } 
$query=mysql_query("UPDATE category SET img='$fname' WHERE category_id='$category_id'"); 
if($query) 
    { 
    echo "Updated"; 
    } 
} 
} 

?> 
+0

我剛剛在我的文件中複製並粘貼了你的代碼,但它仍然沒有更新圖像,如果我在else語句中添加echo「Invalid File」,那麼它將打印「無效文件」消息。 –