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.

Lesson 10 file system using PHP.

Retweet this button on every post blogger
The task is to input a drive name ,folder name , file name, file contents and file extension from the user and save it into the server hard drive.we will learn some php file handling function here.
Program :
<?php
/*php  file system scipt */
if(isset($_POST['submit'])) {
   $errors=''; //an null string
   if ($_POST['folder']=="") {
   $errors.= "<li>Please give a folder name</li>";
    }
 
    if ($_POST['file]=="") {
   $errors.= "<li>Please give a file name</li>";
    }
 
    if ($_POST['text]=="") {
   $errors.= "<li>Please give a file contents</li>";
    }
 
 
   if ($errors=='') {
   $path = $_POST['drive']."/".$_POST['folder'];
   if  (!file_eixsts( $path)) {
   mkdir($path);
   }
  $file_path = $path."/".$_POST['file'].$_POST['ext'];


    if  (!file_eixsts( $file_path)) {
    $fp = fopen($path,"w");  //opening file in write mode at the given path and return a pointer to it.
    fwrite($fp,$_POST['text']); // writing file to directory
   fclose($fp);
   echo "Your file has benn saved.";
   echo "<br/>";
    }


   else
   echo "file exists already, try again";
}
else
echo "<ul>".$errors."</ul>";


?>


<html>
<head>
<title>PHP file handling</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="form"><
<center>
<table>
<tr>
<td>Drive name</td><td><select name="drive">
<option name="c:\">C:\</option>
<option name="d:\">D:\</option>
</select></td>
</tr>

<tr>
<td>Folder name</td><td><input type="text" name="folder" /></td>
</tr>


<tr>
<td>File name</td><td><input type="text" name="file" /></td>
</tr>


<tr>
<td>File contents</td><td><textarea name="text" cols="4" rows="15"></textarea></td>
</tr>


<tr>
<td>File type</td><td><select name="ext">
<option name=".txt">*.txt</option>
<option name=".html">*.html</option>
<option name=".ppt">*.ppt</option>
<option name=".doc">*.doc</option>

</select></td>
</tr>


<tr>
<td>&nbsp;</td><td><input type="submit" name="submit" value="save" /></td>
</tr>
</table>
</center>
</form>
</body>
</html>