I'm sure you have seen sites where the webmaster lets you upload your own files onto his server and I am sure that most of you have went Wow I wanna do that. Sure there are TONS of scripts already pre-coded so all you need to do is download them, but the problem with them is that they don't always do what you want them to do. This tutorial will teach you how to create a basic Upload script, you are welcome to expand on it.
First we will want to create a new page where the user can select the file from their computer. Create a new file and insert the following into it:
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Title here!</title>
</head>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE"
value="1000000">
// this is set in bytes so keep that in mind when changing it Select a file to
upload! <input type="file" name="userfile"><br>
<input type="submit" value="upload!">
</form>
</body>
</html>
Save this file as upload.html or what ever else you wish to call it.
Now we need to create upload.php, the page that will actually take the selected file and upload it to a specified directory. Lets begin by creating a new file and insert the following into it:
<?php
ini_set("upload_max_filesize", "8mb");
// $userfile is where file went on webserver
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
// $userfile_name is original file name
$userfile_name = $HTTP_POST_FILES['userfile']['name'];
// $userfile_size is size in bytes
$userfile_size = $HTTP_POST_FILES['userfile']['size'];
// $userfile_type is mime type e.g. image/gif
$userfile_type = $HTTP_POST_FILES['userfile']['type'];
// $userfile_error is any error encountered
$userfile_error = $HTTP_POST_FILES['userfile']['error'];
// userfile_error was introduced at PHP
4.2.0
// use this code with newer versions
// put the file where we'd like it
$upfile = 'upload/'.$userfile_name;
// is_uploaded_file and
move_uploaded_file
if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
} else {
echo 'Problem: Possible file upload attack. Filename: '.$userfile_name;
}
echo 'File uploaded successfully<br /><br />';
// show what was uploaded
echo '<a href="upload.html"><img
src="images/buttons/return.gif" width="43"
height="14" border="0"></a>';
?>
Lets save this file as upload.php. Now that we are done with this we need to
create a new folder in our FTP and call it upload. CHMOD that folder to 777. Now
take the picture below and upload it to your server: (The Pic is not available)
just upload any pic u already have.
So there you go, you have now created your very own Upload script. No go play
around with it!