file uploads with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

file uploads with php

Post by snpo123 »

I know this is a pretty basic question, but I havent ever had to use file uploads. What is the basic syntax to upload a file from a php page? Do you need a special type of html form? And how to I tell php where to store the file? Thanks.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Yes. you first need to make a form like so:

Code: Select all

<form method="POST" action="" name="file_upload" enctype="multipart/form-data">
            File: <input type="file" name="upload" size="25">
            <input type="submit" name="submit" value="Upload">
         </form>
Notice the enctype="multipart/form-data" in the form tag.

Then when you submit the form the file will be uploaded to a tmp directory and the details are held in $_FILES.

In this case $_FILES['upload'] will hold an array of values to do with the file. Its called 'upload' as the form field is called this.

Do a print_r($_FILES['upload']) to see what values you have access to - which are tmp_name, size, type, name etc.

Check out my file upload class in the code snippet section to learn more, its fairly simple when youve got the basics. Just pratice.
Post Reply