$request->file('files'); not working ?

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
exxos
Registered User
Posts: 139
Joined: Fri Sep 10, 2010 12:37 pm

$request->file('files'); not working ?

Post by exxos »

Not sure if anyone can offer any advice to this..

The script was working until I started integrating phpbb request variables.

Basically I have a simple form which takes some files..

Code: Select all

<form action="upload_util.php" method="POST" enctype="multipart/form-data">
    <div align="center">
        <input type="file" name="files[]" multiple>
        <button type="submit" name="submit">Upload</button>
    </div>
</form>
The snippet from the php script..

Code: Select all

if ($request->is_set_post('submit')) {
    // Retrieve uploaded files using the request class
    $files = $request->file('files');
		
    // Debug: print the $files array
    echo '<pre>';
    print_r($files);
    echo '</pre>';
But it does not return my filenames..

Code: Select all

Array
(
    [name] => none
    [type] => none
    [tmp_name] => none
    [error] => none
    [size] => none
)
Exactly
I should also note the phpbb session seems to be working because I can obtain the username from the user logged into the forum with echo $user->data['username_clean'];

Before phpbb integration it was working fine and I got the filenames. Now it seems to "forget" what was POSTed..

I've been looking around the Internet all morning and I cannot see what I am doing wrong here ?

I have tried passing a variable and that seems to work fine..

Code: Select all

    // Retrieve the submitted variable using the request class
    $myVariable = $request->variable('myVariable', '');

Code: Select all

<input type="hidden" name="myVariable" value="example_value">
I can only assume it's because I am trying to upload files to my server and phpbb doesn't like me doing that. I took out all the phpbb stuff and files upload fine. So what I am trying to do, I assume simply can't work.
exxos
Registered User
Posts: 139
Joined: Fri Sep 10, 2010 12:37 pm

Re: $request->file('files'); not working ?

Post by exxos »

I have found that uploading a single file works just by doing...

Code: Select all

<input type="file" name="files" multiple>
Where I had "files[]" before.

Code: Select all

Array
(
    [name] => tos104uk.img
    [type] => application/octet-stream
    [tmp_name] => /tmp/php3to6zX
    [error] => 0
    [size] => 196608
)
I guess as a workaround I could just upload both files individually and see if that works. At this point I can only assume it is some limitation within the request class itself or some bug.
User avatar
Mike-on-Tour
Registered User
Posts: 537
Joined: Wed Jan 15, 2020 3:51 pm
Location: Germany
Name: Michael

Re: $request->file('files'); not working ?

Post by Mike-on-Tour »

I suspect that this may be a bug within the request.php file because I am getting the same result but I am not certain at the moment.

To solve your proplem just use name="files[]" in your HTML file and in your php script the following line to get the variable:

Code: Select all

$files = $request->raw_variable('files', [], \phpbb\request\request_interface::FILES);
This works for me, even with multiple files.
Watz fo lunch?
If you like my extensions or my support please consider a donation: Image
exxos
Registered User
Posts: 139
Joined: Fri Sep 10, 2010 12:37 pm

Re: $request->file('files'); not working ?

Post by exxos »

Thanks for replying.

I went ahead and used 2 seperate upload variables to get around the problem. It works fine that way. Just a little annoying having to select 2 files individually rather than just selecting 2 at once. I can't try your method as I had to do a fair bit of code-rework since. But glad if you found some better workaround to the problem and it works for you :)
User avatar
Mike-on-Tour
Registered User
Posts: 537
Joined: Wed Jan 15, 2020 3:51 pm
Location: Germany
Name: Michael

Re: $request->file('files'); not working ?

Post by Mike-on-Tour »

I just took a closer look into the code and it is working as intended, so no bug. The $request->file() method does accept only one file per call, you would have to parse multiple files through e.g. a loop but if you have found another way that's working for you go ahead with it.
Watz fo lunch?
If you like my extensions or my support please consider a donation: Image

Return to “phpBB Custom Coding”