Wednesday, January 12, 2011

How To upload A image file in php

If you Want to upload an image in php or save any folder or database you can use the upload script

for this you first have to create the upload html form that is like
with html tag :




input type="file" name="file" id="file" /


input type="submit" name="submit" value="Submit" /



after that you have to use the php script to upload the perticular
image file
i.e

?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "
";
}
else
{
echo "vikas: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"vikas/" . $_FILES["file"]["name"]);
echo "Stored in: " . "vikas/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

here we check the condition for the file the file is of the type jpeg
gif , or pjpg type by which other file show invalid
the give file image uploaded by pressing the submit button or it save in the folder vikas as you already created

4 comments:

  1. What is meaning of ($_FILES["file"]["size"] < 20000) 2000 in above code ????

    ReplyDelete
  2. $_file["file"] is the name of the given file and ["size] is the size of file which is not more than 20000 kb so its simply mean condition you can upload file of size less than 20000kb by this function


    thank for the comment

    ReplyDelete
  3. SO what it is means. .. Is this stop the file more then 20000kb on client side or it first uploaded to server then one can known about size ???

    ReplyDelete
  4. yes if you try to upload the file of size more than 20000kb then you unable to upload that check show invalid file so you unable to upload the file more than 20000kb

    ReplyDelete