Google Community Chat
 
File upload with PHP

You're visiting Google Community as a guest.
In order to post, you'll need to register and log in.

(If you were registered and logged in, these advertisements wouldn't be here)
Post new topic   Reply to topic    Google Community Forum Index // Web Design, Coding & Programming Forum
   
Author Message
puccavn
Noogle
Noogle


Joined: 12 Apr 2005

1021.15 GC$

Items

PostPosted: Thu May 05, 2005 11:41 pm    Post subject: File upload with PHP Reply with quote
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");
}
?>
Back to top
View user's profile Send private message
Sponsored Links
Posted: 5 Dec 2008 10:03 am    Post subject: Advertisements
Back to top
Post new topic   Reply to topic    Google Community Forum Index // Web Design, Coding & Programming Forum All times are GMT - 8 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Host your free forums with Invision Plus.net forum web hosting with your own subdomain.

alexisBlue v1.2 // Theme Created By: Andrew Charron // Icons in Part By: Travis Carden

© 2005-2006 Google Community

Powered by phpBB

Privacy Policy | Contact Us

Powered by Google Search blog

This website is not affiliated in any way with Google, Inc.
Google™ is a registered trademark of Google, Inc.