FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


File upload with PHP, Build an upload database for your site





thaonguyenxanh
In later versions of PHP (starting from 4) there is a way of handling file uploads using special global array ($HTTP_POST_FILES), but we describe the older way in this tutorial.



Let's consider that we have this HTML code in file upload form:

<form method="post" enctype="multipart/form-data" action="script.php">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000">
<input type="file" name="userfile">
<input type="submit">
</form>

The hidden field MAX_FILE_SIZE (must be specified before <input type=file> tag) is used for setting maximum file size for upload. So the browser will not send the file if its size is more than the value specified here. When form has been submitted PHP creates and fills special variables associated with file transfer data. There is no need in parsing HTTP request manually, PHP would manage it and provide upload variables. Note that variable names consist of two parts and the first part is the same as the specified name of input field in HTML code.
Suppose we have this HTML code:
<input type="file" name="userfile">
in your PHP script these variables have been automaticaly created:

$userfile: File name of temporary file on server machine(created after upload). You can use this variable for copying temporary file into any location to store it.

$userfile_name: Name of the file that user located on client machine to upload it into server

$userfile_size: Size of uploaded file in bytes.

$userfile_type: MIME type of uploaded file. Set only if browser has specified it. Example: "image/gif".

There is function to check temporary file name variable.
is_uploaded_file($userfile), returns TRUE if $userfile contains the name of temporary uploaded file, and FALSE otherwise.

move_uploaded_file($userfile, "/place/to/put/uploaded/file/name.it"): Function used to move temporary file into any location on server machine (of course according to writing rights on that location).

Here is the PHP code that stores uploaded file in some location.

<?
if (is_uploaded_file($userfile)) {
move_uploaded_file($userfile, "/place/file.new");
}
?>

And also we can do it with open function.

<?
if (is_uploaded_file($userfile)) {
copy($userfile, "/place/file.new");
}
?>
Related topics
How to become a millionaire on the internet?
Introducing ROBIN!
HUGE FREE LINK EXCHANGE!!
uploading system
quite simple file upload
File upload problem :'(
File upload class problem.
how to compress a file using php ?
Can you have upload scripts on frihost sites?
Uploading Files to the Database
Integrating VC++ and PHP
File upload
File upload error
File : emailer.php yeah... it happened again.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.