UPDATED: How can PHP upload files to my server?

Frequently Asked Questions about WYSIWYG Web Builder
Locked
User avatar
Pablo
 
Posts: 21569
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

UPDATED: How can PHP upload files to my server?

Post by Pablo »

This article describes how to make a form that uploads one or more files onto a specified directory on your server. Your webhost/server must support PHP for the following method.

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:

Code: Select all

<?php
$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;
          $t++;
        }
        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.

Notes
1. The variable $uploaddir holds the target directory for the uploaded files. Be sure that the directory name is terminated with a slash '/'.
2. The variable $allowed holds all accepted extensions. Note that extensions are quoted, comma separated and in lower case.
Important: Never accept files with these extensions: php, asp, htm, html, js, etc. Once uploaded they can be executed, with unpredictable consequences.
3. The variable $max_size holds the max. size of an uploaded file. 1024*1024 means 1 MB. The max. upload size is also limited by server settings.
4. If the uploaded file already exists on the server, it will be renamed as follows: 'animal.jpg' => 'animal[1].jpg'.
5. The permissions of the uploaded file are set to the default value: 644.
6. All successful uploads are listed. If you don't want this, you can remove the line 'echo ...' or insert a preceding hash '#'.
7. This method of file uploading is not intended for very large files. For larger files you should use the FTP protocol.

Article submitted by Kees Versluis
Updated: 14-07-2007 (New text and code)
User avatar
Pablo
 
Posts: 21569
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: UPDATED: How can PHP upload files to my server?

Post by Pablo »

Please note that if you use the built-in PHP form processor then you do not have to use the code above. The code for uploading files will then automatically be generated for you!
See also the help/manual.
Locked