Monday 13 June, 2011

Lesson 11PHP file upload.

Retweet this button on every post blogger
File upload to server computer is a very task to do in PHP to see how it happens lets see a example.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File upload using PHP</title>
</head>
<?php
if(isset($_POST['submit']))
{
     if(isset($_FILES['upload']['name']))
{
    echo "<b>Attributes of file upload:</b><br/>";
echo "<b>File name:</b>" .$_FILES['upload']['name'];
echo "<br/><b>File type:</b>" .$_FILES['upload']['type'];
echo "<br/><b>File Size:</b>" .$_FILES['upload']['size']."bytes";
echo "<br/><b>File Temp:</b>" .$_FILES['upload']['tmp_name'];
echo "<br/><b>File error:</b>" .$_FILES['upload']['error'];

$path1 = $_FILES['upload']['tmp_name'];
$path2 = "uploaded_files/".$_FILES['upload']['name'];
if($_FILES['upload']['type']=="image/pjpeg" || $_FILES['upload']['type']=="image/gif" || $_FILES['upload']['type']=="text/plain" || $_FILES['upload']['type']=="application/x-php"  || $_FILES['upload']['type']=="application/msword")
{
move_uploaded_file($path1,$path2);
echo "<br/>file uploaded to server.";
}
else
echo "<br/><b>select only jpeg,gif,text or  php files to upload.</b>";
}
else
echo "No file choosed.";

}

?>
<body>
<form name="form" method="post"  action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
<h1>File upload using PHP</h1>
<center>
<table>
<tr>
<td>Choose file</td><td><input type="file" name="upload"  /></td>
</tr>

<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="upload"  /></td></tr>
</table>

</center>
</form>
</body>
</html>
 1.Where uploaded file folder created at the server holds the user uploaded files.
2.To upload a file the HTML input type must be "file"
3. form enctype="multipart/form-data" must be set that shows that form data must be send as MIME file encoded as binary file.
4. Uploaded file parameters  should use $_FILE global array  to retrieve different file abstract
5. There are five different parameters associated with it. these are ,
    file name
   file type
  file size
 file tmp_name which is temporary storage of file uploaded to server.
file error.

what we to do simply move our file from temporary storage to path  specified by the developer.

No comments:

Post a Comment