Sunday, December 4, 2011

Uploading an image file on server

Code Example

upload.html 
<html>
    <head>
        <title>Upload an image</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="uploadimage.php"  enctype="multipart/form-data" method="post">
            <input type="file" name="file" value="" /><br/>
            <input type="submit" value="   Upload   " />
            
        </form>
    </body>
</html>
uploadimage.php
<?php
$image = $_FILES['file']['name'];
$img_type = $_FILES['file']['type'];
$img_size = $_FILES['file']['size'];
$img_error = $_FILES['file']['error'];
if(!empty ($image) && ($img_type == 'image/gif' || $img_type == 'image/png' || $img_type == 'image/jpeg')
 && $img_size > 0 && $img_size < 40000 && $img_error == 0)
{
    $target="image/".$image;
    if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
    {
        echo 'image file uploaded successfully';
    }
 else {
        echo 'error';
    }
}
?>

No comments:

Post a Comment