Menu

PHP TUTORIALS - PHP File Uploading

PHP File Uploading

ADVERTISEMENTS

Creating an upload form:


<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="/php/file_uploader.php" method="post"
                        enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

ADVERTISEMENTS


File Upload:
Select a file to upload: 


NOTE: This is just dummy form and would not work.

ADVERTISEMENTS

Creating an upload script:


<?php
if( $_FILES['file']['name'] != "" )
{
   copy( $_FILES['file']['name'], "/var/www/html" ) or 
           die( "Could not copy file!");
}
else
{
    die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name'];  ?>
<li>File size: <?php echo $_FILES['file']['size'];  ?> bytes
<li>File type: <?php echo $_FILES['file']['type'];  ?>
</ul>
</body>
</html>


Uploaded File Info:
  • Sent file: uploadedfile.txt
  • File size: 2003 bytes
  • File type: image/jpg