Searching...
Tuesday 9 October 2012

PHP File Uploading

10:54 pm

PHP File Uploading

PHP File Uploading :

The “enctype” attribute of the <form> tag specifies which content-type to use when submitting the form.” Multipart/form-data” is used when a form requires binary data, like the contents of a file , to be uploaded.
The <input type=”file” name=”t3”> tag specifies that the input should be processed as a file.default value of “enctype” attribute of <form> tag is “application/x-www-form-urlencoded”.
<html>
    <head>
      
    </head>
    <body style="background-image: url(Photo0104.jpg); color:white">
        <h1 align="center">submit form</h1><br><br>
        <form action="uploadtest.php" method="post" enctype="multipart/form-data">
            <table style="font-family: verdana; font-size: 20px;width: 50%">
                <tr height="50">
                    <td>Name</td>
                    <td>
                        <input type="text" name="t1" size="30">
                    </td>
                </tr>
                <tr height="50">
                    <td>Profile</td>
                    <td><input type="file" name="t2" size="30"></td>
                </tr>
                <tr height="50">
                    <td colspan="2" align="center">
                        <input type="submit" value="register">
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
$_FILES is a built-in global array of php which stores all the attached file details and is a multi-dimentional array. The first parameter is the form’s input name and the second index can be either “name”,”type”,”size”,”temp-name” or “error”.
$_FILES[‘t3’][‘name’] – the name of the uploaded file.
$_FILES[‘t3’][‘type’] – type of the uploaded file.
$_FILES[‘t3’][‘size’] – size in bytes.
$_FILES[‘t3][‘temp-name’] – the name of the temporary copy of the file stored on the server.
$_FILES[‘t3’][‘error’] – the error code resulting from the file upload. If the value is 0 means no errors in uploading.
*we can change the uploaded file size
Goto -> php.ini -> post-maxsize 100M
                              Upload-max-size  20M
<?php
echo "<h1>";
print_r($_POST);
echo "<br><br>";
print_r($_FILES);
if($_FILES['t3']['error']==0)
    if($_FILES['t3']['error']==0&&$_FILES['t3']['tyep']=="text/palin")
    {
        $res = move_uploaded_file($_FILES['t3']['type-name'], "upload/".$_FILES['t3']['name']);
        if($res)
            echo "file uploaded!!!";
        else
            echo "file not uplaoded";
            }
            else
                echo "uplaoding is not done!! please upload only text files..<br> u have uploaded <ins>".$_FILES['t3']['type']."<ins>file";
           

           
?>
OutPut:
Array ( [t1] => penna )

Array ( [t2] => Array ( [name] => DSC00020.JPG [type] => image/pjpeg [tmp_name] => C:\xampp\tmp\phpB060.tmp [error] => 0 [size] => 5349849 ) ) uplaoding is not done!! please upload only text files..
u have uploaded file.

0 comments:

Post a Comment