U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files...

13
UNIT 4 FILE UPLOAD

Transcript of U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files...

Page 1: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

UNIT 4

FILE UPLOAD

Page 2: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

I. CREATE AN UPLOAD-FILE FORM- With PHP, it is possible to upload files to the

server.To allow users to upload files from a form can be very useful.

- <form action="upload_file.php" method="post"enctype="multipart/form-data"><label for="file">Filename:</label><input type="file" name="file" id="file" /> <br /><input type="submit" name="submit" value="Submit" /></form>

Page 3: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

I. (CONT..) Notice the following about the HTML form above:

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 type="file" attribute of the <input> tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field

Page 4: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

II. CREATE THE UPLOAD SCRIPT The "upload_file.php" file contains the code for uploading a

file:

<?phpif ($_FILES["file"]["error"] > 0)  {  echo "Error: " . $_FILES["file"]["error"] . "<br />";  }else  {  echo "Upload: " . $_FILES["file"]["name"] . "<br />";  echo "Type: " . $_FILES["file"]["type"] . "<br />";  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";  echo "Stored in: " . $_FILES["file"]["tmp_name"];  }?>

Page 5: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

II. CREATE THE UPLOAD SCRIPT (CONT. )

By using the global PHP $_FILES array you can upload files from a client computer to the remote server.

The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this: $_FILES["file"]["name"] - the name of the uploaded file $_FILES["file"]["type"] - the type of the uploaded file $_FILES["file"]["size"] - the size in bytes of the uploaded file $_FILES["file"]["tmp_name"] - the name of the temporary

copy of the file stored on the server $_FILES["file"]["error"] - the error code resulting from the file

upload This is a very simple way of uploading files. For security

reasons, you should add restrictions on what the user is allowed to upload.

Page 6: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

III. RESTRICTIONS ON UPLOAD In this script we add some restrictions to the file upload. The user

may only upload .gif or .jpeg files and the file size must be under 20 kb:

<?phpif ((($_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"] . "<br />";    }  else    {    echo "Upload: " . $_FILES["file"]["name"] . "<br />";    echo "Type: " . $_FILES["file"]["type"] . "<br />";    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";    echo "Stored in: " . $_FILES["file"]["tmp_name"];    }  }else  {  echo "Invalid file";   }?>

Page 7: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

IV. SAVING THE UPLOADED FILE The temporary copied files disappears when the script ends. To store

the uploaded file we need to copy it to a different location:<?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 "Return Code: " . $_FILES["file"]["error"] . "<br />";    }  else    {    echo "Upload: " . $_FILES["file"]["name"] . "<br />";    echo "Type: " . $_FILES["file"]["type"] . "<br />";    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))      {      echo $_FILES["file"]["name"] . " already exists. ";      }    else      {      move_uploaded_file($_FILES["file"]["tmp_name"],      "upload/" . $_FILES["file"]["name"]);      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];      }    }  }else  {  echo "Invalid file“;  }?>

Page 8: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

IV. SAVING THE UPLOADED FILE (CONT.)

The script above checks if the file already exists, if it does not, it copies the file to the specified folder.

Note: This example saves the file to a new folder called "upload"

Page 9: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

V. EXAMPLE Create DB that have table name : tblupload

CREATE TABLE `tblupload` ( `ID` int(11) NOT NULL auto_increment, `ImagePath` varchar(100) NOT NULL, `Description` varchar(100) default NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

Page 10: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

1. UPLOAD TO DIRECTORY $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']

['name']); if(move_uploaded_file($_FILES['uploadedfile'] ['tmp_name'],

$target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo “error uploading the file, please try again!"; } <form enctype="multipart/form-data" action="<?php echo

$_SERVER['PHP_SELF'];?>" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name=uploadedfile type="file"

/><br /> <input type="submit" value="Upload File" /> </form>

Page 11: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

2. UPLOAD TO DB & DIRECTORY (SCRIPT)

Script : <?php if (isset($_POST['filename'])) {

$file=$_POST['filename']; $target_path="images/"; $target_path = $target_path . basename( $_FILES['myfile']['name']); $myfile=$_FILES['myfile']['name']; if (!($_FILES['myfile']['name'])) { echo "Error: No file name is uploaded!browse to file !"; } else { if(move_uploaded_file($_FILES['myfile']['tmp_name'],$target_path)){

$imagePath=$target_path; $description= $_FILES['myfile']['name']; $conn=mysql_connect("localhost","root","") or die("Cannot connect to Mysql Server!"); mysql_select_db("registration1"); $sql="Insert into tblupload values('','$imagePath','$description')"; if(mysql_query($sql,$conn)){ echo"New Image has been inserted successfully!"; } else{ echo"There is a problem inserting new record!"; } } else echo "Cannot upload file!problems, try again!"; } } ?>

Page 12: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

2. UPLOAD TO DB & DIRECTORY (HTML)

<html> <body text="blue"> <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">

<table bgcolor="pink" align="center"> <tr><td colspan="2" align="center"> What do you want to upload?</tr>

<tr><td>Image Path:<td><input type="file" size="30" name=myfile></td>

</tr> <tr><td>Image Name:<td><input type="text" name="filename"></tr> <tr><td>Image Description:<td><input type="text" name="txtDescription"></tr> <tr><td colspan="2" align="center"><input type="submit" value="Upload"></tr> </table></form> </body></html>

Page 13: U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.

2. SHOW IMAGE OR SHOW FILE <body> <p>File List</p> <?php $conn=mysql_connect("localhost","root",""); mysql_select_db('registration1',$conn); $query="select ImagePath,Description from tblupload"; $result=mysql_query($query); while($row=mysql_fetch_array($result)){ $image=$row['ImagePath']; $img=$row['Description']; echo "<a href='./images/$img' target='_blank'>$img</a>"."</br>"; // echo "<tr><td><img src=\"$image\" width=100

height=200/><td>"; }

echo "</tr></table>"; mysql_close($conn);

?> </body>