Page 1 of 1

Need help on file upload

Posted: Mon Jul 24, 2006 12:42 pm
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 ?

Posted: Mon Jul 24, 2006 12:54 pm
by Luke
there is not ONE script that magically uploads files to your server. You can build one... or you can search hotscripts.com

Posted: Mon Jul 24, 2006 3:25 pm
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.