Well first there is a new attribute to the form tag.
enctype="multipart/form-data" Basically allows all data types including Files to come from the form.
So we have a form
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" name="submit" value="Send File" />
</form>
Basically we must wait for userfile to get to the next part as it uploads the file as a tmp file to the server.
The max_file_size can be defined in the php.ini so its not exactly needed and im sure its max is 2mb for most webhosts.
No we will upload it from the root directory e.g.
http://ourdomain.com and we are in file upload.php so http://ourdomain.com/upload.php
to a folder called downloads.
Now the folder downloads must have permissions to allow the next script we will have access to store a new file(s). To do this we go into our FTP program and right click on the folder downloads.
Now in SmartFTP it will be slightly different to others. Right Click on the folder than selected Properties a menu dialog will popup and change the folder permissions from usually set as 755 to 777 basically that gives the right to Write, Read, Execute to all Groups.
Now for the next part of the script.
$directory='downloads';//define the upload directory
if (@move_uploaded_file($_FILES['userfile']['tmp_name'],$directory."/".$_FILES['userfile']['name'])) {
//it uploaded ok
} else {
//an error occured
}
So lets just explain
$_FILES['userfile']['tmp_name'] -> this is the tmp file we were talking about earlier its in a defined folder set by the php.ini as something like 0012320.tmp and our file name is fileupload.exe
$_FILES['userfile']['name'] -> this is the actual file name so fileupload.exe is its value.
move_uploaded_file -> basically gets one file which is $FILES['userfile']['tmp_name'] to directory downloads/ and moves the file in that directory with the name $FILES['userfile']['name'] which is called fileupload.exe. Now move_uploaded_file will overwrite any file with the same name so you can get the file renamed i wont explain how that is done
http://www.php.net/manual/en/function.move-uploaded-file.php has more on move_uploaded_file
Now instead of move_uploaded_file we could have used copy(); which has the same arguments as move_uploaded_file however works slightly differently.
Now lets put the two scripts to work together in the same file
if (!isset($_POST['submit'])) {
?>
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" name="submit" value="Send File" />
</form>
<?php
} else {
//submit was pressed lets upload the file
$directory='downloads';//define the upload directory
if (@move_uploaded_file($_FILES['userfile']['tmp_name'],$directory."/".$_FILES['userfile']['name'])) {
//it uploaded ok
} else {
//an error occured
}
}
Now you can read more at http://www.php.net/manual/en/features.file-upload.php and on copy http://www.php.net/manual/en/function.copy.php
I hope this little demonstration helped you