WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Uploading files in PHP
Categories : PHP, Filesystem Picture not available
Ahmed Saifullah Shafiq
Date : 2003-05-20
Grade : 5 of 5 (graded 1 times)
Viewed : 18869
Search : More Articles by Ahmed Saifullah Shafiq
Action : Grade This Article
Tools : My Favotite Articles


Submit your own code examples 
 


Download Source Code (1 kb)


Think of an application where you want your users to upload files to your machine. Uploading files in PHP is very easy. You can upload any type of file from client to server. Files are uploaded from the browser using an "input" tag, set the type parameter in the "input" tag as "File". This is supported by all popular browsers currently available on the market. Important thing is to set the ENCTYPE attribute of the form to "multipart/form-data" and set the form's action element to the file upload page. The file upload page will handle the file uploading. Here is the code:

Create a simple html form for submitting the file:


<form action="myupload.php" method=post enctype="multipart/form-data">
submit this file: <input type=file name="userfile" size="20">
<input type=submit>
</form>


Now, create a php file named as "myupload.php":


<?
if(!empty($userfile)) {

//copy the file to some permanent location
copy($userfile, "/php/www/files/myfile.txt");

//destroy the file
unlink($userfile);

//display message
echo("file uploaded");
}
?>




Details:




Handling uploaded file is very easy in PHP. First, the file is saved in the temporary directory of your operating system (if you have not set the path to your own temporary directory in the php.ini file). You can copy the uploaded file from the temporary directory to the location of your choice. The file in the temporary directory is automatically destroyed at the end of the request. File name is accessible in the same way as all form data. In our code, we have set the name for the uploaded file as "userfile". You can access this variable in the upload file page as $userfile. We copy the file to a permanent location using the copy() function. Although, the uploaded file is automatically destroyed at the end of the request but its a good practice to destroy the file explicitly. You can use the unlink() function to destroy the file.

You can check if the file has been uploaded to the server using different attributes provided by the PHP. For example, if you dont want to allow your users to upload the same file again and again, you can put a restriction on the user to upload new files only. We can set limit on the size of the file to be uploaded by adding an <input type=hidden size="20"> element with the NAME attribute set to MAX_FILE_SIZE and the VALUE to the upper limit. I havent demonstrated this in my sample code but here the piece of code for your reference:


<input type=hidden name=MAX_FILE_SIZE value=1024>
<input type=file name=userfile size="20">


Remember, the hidden <input type=hidden name=MAX_FILE_SIZE value=1024> tag should precede the file <input type=file name=userfile size="20"> tag.

Place a check before displaying a message, use the empty() function to check if $userfile variable contains something or not, if its empty then display the message telling the user that the file hasn't been uploaded otherwise display the message that the file has been uploaded successfully.









File And Directory Manipulation In PHP (part 2)
Categories : PHP, Filesystem, Directories
PHP 101 Part 5 of 15 : Rank And File
Categories : PHP, Beginner Guides, Filesystem
10 PHP Functions I Bet You Didn`t Know About
Categories : PHP, PHP Functions, Filesystem, Arrays, Errors and Logging
File And Directory Manipulation In PHP (part 1)
Categories : PHP, Directories, Filesystem
Multiple pages of data from a text file
Categories : PHP, Filesystem
String Theory - A discussion of PHP string function
Categories : PHP, Strings
Simple Connection to PostgreSQL with PHP
Categories : PHP, PostgreSQL, Databases
Making PHP Forms Object-Oriented
Categories : PHP, HTML and PHP, Object Oriented
Extracting Elements from a Database into a Select Form Field
Categories : PHP, HTML
Introduction to using PHP 5.0 and SQLite
Categories : PHP, SQLite, Databases
PHP And Regular Expressions 101
Categories : PHP, Regexps
An Alternative to Perl: Shell Scripting With PHP
Categories : PHP, Shell Scripting
Building An FTP Client With PHP
Categories : PHP, FTP
Webstatistics with Redirectors
Categories : PHP, HTML, HTML and PHP
Getting Intimate With PHP's Mail() Function
Categories : PHP, Mail, PHP Functions
T Grourier wrote : 363
I`ve copied and pasted the code exactly as posted here. Yet,
whenever I submit the form, the $userfile parameter is
ALWAYS empty when myupload.php is generated.

Is there any special server settings required to enable this
functionality?
Strong Yuan wrote : 364
php.ini setting must be:
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system
default if not
; specified).
upload_tmp_dir = C:\PHP\uploadtemp ; temporary directory for
HTTP uploaded files (will use system default if not specified)

; Maximum allowed size for uploaded files.
upload_max_filesize = 2M