我编写了以下代码来更新存储在文件中的图像。当我使用它来上传图像时,该代码运行良好,但是当我使用它来更新图像时,它给了我“无效文件”错误消息。它在第一个“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";
}
?>