0

我编写了以下代码来更新存储在文件中的图像。当我使用它来上传图像时,该代码运行良好,但是当我使用它来更新图像时,它给了我“无效文件”错误消息。它在第一个“if”语句中出现问题,并直接转到其 else 语句消息“无效文件”。请检查并指导我。

谢谢

<?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";

    }


?> 
4

2 回答 2

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";  
  }
}
}

?>
于 2013-08-16T13:12:32.047 回答
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' >

它解决了这个问题。

于 2013-08-17T09:11:07.897 回答