PHP File upload
- Friday, July 2, 2010, 5:38
- PHP
- Add a comment
|
|
1 st create html form to input the file to upload.The form need an enctype tag which specifies how form-data should be encoded before sending it to the server.Here we use “multipart/form-data” .That is no characters are encoded. This value is required when you are using forms that have a file upload control.
{code codetype=html}
<form action=”upload.php” enctype=”multipart/form-data” method=”POST”>
<input name=”MAX_FILE_SIZE” type=”hidden” value=”622000″ />
Upload this file: <input name=”userfile” type=”file” />
<input type=”submit” value=”Upload” />
</form> {/code}
Then create the php file upload script
{code codetype=php}
<?php
$uploaddir = ‘/upload/’;
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo “<p>”;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo “File uploaded successfully .\n”;
} else {
echo “Upload failed”;
}
?>
{/code}
The upload directory should have appropriate permissions to upload the file.


