Need help on file upload

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
scorpion
Forum Newbie
Posts: 1
Joined: Mon Jul 24, 2006 12:29 pm

Need help on file upload

Post by scorpion »

I don't really know what's the real file upload script on php. I'm confused often.....

can anyone plezz tell me what's the script to upload image files to my server through php script ?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

there is not ONE script that magically uploads files to your server. You can build one... or you can search hotscripts.com
kaYak
Forum Commoner
Posts: 65
Joined: Mon Feb 02, 2004 2:43 pm
Location: USA

Post by kaYak »

A file upload is not difficult. It is comprised of just a few things. You need an HTML form with an input field with the type 'file'.

Code: Select all

<form enctype="multipart/form-data" action="http://www.example.com/path/to/script.php" method="POST">

Upload File: <input type="file" name="file1" />

<br /><br />

<input type="submit" value="Upload File" />

</form>
It is important to also have the enctype be "multipart/form-data" as shown above. The action attribute of the form must point to the php script that will process the upload.

With php you can use the function move_uploaded_file which as its name suggests, will move the uploaded file from the server's temporary file directory to a directory of your choice.

Code: Select all

$new_file = '/path/to/new/directory/' . basename($_FILES['file1']['name']);
move_uploaded_file($_FILES['file1']['tmp_name'], $new_file);
The "/path/to/new/directory/" is where you want the file placed on the server, this is not a URL but instead the server's file path. The basename function take the file's name which may be something like /tmp/10240353/your_file.doc and just takes your_file.doc. Combining the two gives you the desired path. I think that is it. Please make an effort to understand the code. I tried to help you as much as possible, without just giving you full files you can just take and use. You will have to change the variables to suit your server.
Post Reply