To upload a file using php

Steps:

1. Create new html file and copy upload.html code and save it as upload.html.

2. Create new php file and copy uploader.php code and save it as uploader.php.

3. Run upload.html, then it will display the html data.

4. Click browse button and select any file or image and press upload button.

5. Then it will transfer that specific file or image to the created folder and display the output as file is uploaded.

upload.html:

[php]

<!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>upload</title>
</head>

<body>
<form enctype="multipart/form-data" method="post" action="uploader.php">
<input type="file" name="file" />
<input type="submit" name="submit" value="upload" />
</form>
</body>
</html>

[/php]

It displays the output as:


uploader.php
[php]</form><form action="uploader.php" method="post" enctype="multipart/form-data"><!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>Untitled Document</title>
</head><body>
<?php
if(isset($_REQUEST[‘submit’]))
{
$to="uploaded/".$_FILES[‘file’][‘name’];
move_uploaded_file($_FILES[‘file’][‘tmp_name’],$to);
echo "file is uploaded";

}
?>
</body>
</html>

[/php]

//create a new folder uploaded

No comments yet.

Leave a comment

Request a Free SEO Quote