PDA

View Full Version : File Upload in Php



mad-eyemoody
02-14-2008, 04:55 PM
If you want users to upload files you can do this by using php

HTML FILE
<form action=”upload.php” method=”post”
enctype=”multipart/form-data”>
<input type=”file” name=”upload” />
<br />
<input type=”submit” name=”submit” value=”Submit” />
</form>

The file of yours will now consist of a browse button through which a user can select the file he wants to upload

The upload.php will display the file information and upload the file to the folder upload

UPLOAD.PHP
<?php
if(isset($_FILES[’upload’])){if ($_FILES[”file”][”error”] > 0)
{
echo “Contain error ” . $_FILES[”file”][”error”] . “<br />”;
}
else
{
echo “File name ” . $_FILES[”file”][”name”] . “<br />”;
echo “File Type: ” . $_FILES[”file”][”type”] . “<br />”;
echo “File 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 ‘where have you come from ha’;
}

?>

Create both the files and upload to the server then create a folder named upload [important] Or else you will get error.

Edited, removed wrong info due to copying

Source: DNTemple.com

darkman
02-16-2008, 12:15 PM
Thanks for the tutorial

Collin1000
02-17-2008, 02:29 AM
Can you try to use a codebox next time so that the forum does not strip it all out? But thank you very much for this helpful post. I am sure it will help newbies!