Monday, 5 September 2011

How to upload files int to server using Php?

You should make two pages in WebBuilder: the form page and the processing page. The latter holds the PHP script and will report all successful uploads.

I - The form page
1. Draw a Form Area and change the Form Properties to:
- Action: upload_plus.php
- Method: POST
- Encoding type: multipart/form-data

2. Make up your form. Remarks:
- include one or more File Upload objects and name all of them userfile[]
- include a Push Button and set the Button Type to Submit
- do not include any other form elements.

II - The processing page
1. Make up a nice page according the style of your site. Remarks:
- tell your visitors that the listed files are uploaded succesfully.
- include a link or menu to other pages of your site.

2. Rename the page to upload_plus using the Site Manager.
3. Bring up the Page Properties dialogue and change it to:
- File Extension: php

4. Draw an HTML object.
- The object size should be enough for the list of uploaded files.
- Bring up the HTML Properties dialogue and insert this:

$uploaddir = './uploads/';
$allowed = array('jpg','jpeg','gif','pdf');
$max_size = 1024 * 1024;
# No edits beyond this line

if (isset($_FILES['userfile'])) {
foreach ($_FILES['userfile']['error'] as $i => $error) {
if ($error == 0 && $_FILES['userfile']['size'][$i] <= $max_size) {
$file_ext = pathinfo($_FILES['userfile']['name'][$i],PATHINFO_EXTENSION);
$file_name = basename($_FILES['userfile']['name'][$i],'.'.$file_ext);
if (in_array(strtolower($file_ext),$allowed)) {
$new_base = $_FILES['userfile']['name'][$i];
$t = 1;
while (file_exists($uploaddir.$new_base)) {
$new_base = $file_name.'['.$t.'].'.$file_ext;
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$uploaddir.$new_base)) {
chmod($uploaddir.$new_base, 0644);
echo 'Successful upload: '.$_FILES['userfile']['name'][$i].'<br>'."\n";

III - Final steps
1. Publish your site.
2. Create an 'upload directory' named uploads on your server.
- This can be done using the FTP Manager or any other FTP client.
- Be sure this directory is on the same level as the processing page.
- Set the directory's permissions to: 777.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...