I Just changed this
<form action='update_category.php?category_id=$category_id' method='POST' >
to
<form enctype="multipart/form-data" action='update_category.php?category_id=$category_id' method='POST' >
and it solved the problem.
Pergunta
I have written the following code to update Images that are stored in Files. The code is working perfectly when I am using it to Upload image, but when I am using it to Update Images, it is giving me 'Invalid file' error message. It is getting problem in the first 'if' statement and directly going to its else statement message 'Invalid File'. Kindly check it and guide me.
Thanks
<?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";
}
?>
Solução 2
I Just changed this
<form action='update_category.php?category_id=$category_id' method='POST' >
to
<form enctype="multipart/form-data" action='update_category.php?category_id=$category_id' method='POST' >
and it solved the problem.
Outras dicas
Refer to this code:
<?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";
}
}
}
?>