[Tutorial] How to use the upload functions in phpBB

Discussion forum for MOD Writers regarding MOD Development.
Locked
User avatar
RaythXC
Registered User
Posts: 150
Joined: Wed Jun 13, 2012 1:23 pm

[Tutorial] How to use the upload functions in phpBB

Post by RaythXC » Sun Nov 23, 2014 3:33 pm

I took a little while working this out since there isn't a guide for it (that I could find) and I can't for the life of me work out how to create a wiki page (doh?)

Anyway here is a small guide for when you want to upload files with phpbb without using $_FILES.

Let's say for example you have an admin module which requires an "upload" part as the "action" in the $_GET variable.

Code: Select all

$action = request_var('action', '');
This will store the action from the url in the variable. Now we'll use a switch to decide it:

Code: Select all

switch ($action)
{
    case 'upload':
        //Our main code goes in here
    break;
}
Now we have to do a couple of things. First we check if the class fileupload exists so under the commented line (begins with //) we add the following:

Code: Select all

if (!class_exists('fileupload'))
{
    include($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
}
Now the main part of the code. Get all the information from the form.
After the code above. In this example we will allow zip or txt files to be uploaded.:

Code: Select all

//Set upload directory
$upload_dir = 'files/';
//Upload file
$upload = new fileupload();
$upload->set_allowed_extensions(array('zip', 'txt'));
$file = $upload->form_upload('UPLOAD_FORM_NAME');
Before we proceed we must note a couple of things about this code. We have set an upload directory as 'files'. This means when we move the file later, it will be moved to phpbb_root/files/.
Secondly, we've specified we can upload zip or txt files. This prevents malicious files from being uploaded. You can add or remove an extension by editing that part of the code, remembering that each extension needs to be separated by a comma since this is an array.
Next, when you make your file upload part of your form (HTML that I won't go into just now), you will give it a "name" variable. This must be exactly the same as where it has 'UPLOAD_FORM_NAME' above.

After this we need the following code:

Code: Select all

if (empty($file->filename))
{
    trigger_error( 'File upload failed.' . adm_back_link($this->u_action), E_USER_WARNING);
}
This simply checks to ensure that the file actually uploaded, and if not, trigger an error.

Next:

Code: Select all

$file->move_file($upload_dir, true);
This is the final part of code that moves the file to the files directory.
You may notice that when we specified the upload directory, we didn't put in $phpbb_root_path. This is because the move_file function of the fileupload class automatically adds this in for us.

The full code should be as such:

Code: Select all

$action = request_var('action', '');
switch ($action)
{
    case 'upload':
        //Our main code goes in here
        if (!class_exists('fileupload'))
        {
            include($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
        }
        //Set upload directory
        $upload_dir = 'files/';
        //Upload file
        $upload = new fileupload();
        $upload->set_allowed_extensions(array('zip', 'txt'));
        $file = $upload->form_upload('UPLOAD_FORM_NAME');
        if (empty($file->filename))
        {
            trigger_error( 'File upload failed.' . adm_back_link($this->u_action), E_USER_WARNING);
        }
        $file->move_file($upload_dir, true);
    break;
}
Enjoy coding and I hope I helped :)

Locked

Return to “[3.0.x] MOD Writers Discussion”