XF 2.0 Sample of how to add an image picker in add-on?

ludak

Active member
I am trying to add an image (small image per item) in my addon.

I have something like

Player ID, Player Name, Player Image

and now I would like to be able when adding a new player, editing (to be able to use picker to select image to be placed there).
Something similar is with the Avatars on main xenforo

1527401189435.webp

So I see Avatar uses this

Code:
<xf:upload name="upload" class="js-uploadAvatar" accept=".gif,.jpeg,.jpg,.jpe,.png" />

is the <xf:upload> what I should be looking at?
Is there a sample of this somewhere, I was not able to find it in docs or searching through the forums.

Also, where is the image stored (as blob in DB, or on fileserver). Please share some samples.

I guess I am seeing it now in $_FILES.

But where do I store it? how to make sure its separate location from xenforo? And what does the process of saving it looks like/ Does db have just a path to image file?

I would really appreciate help on this. THANKS!!!
 
Last edited:
I figured out everything, just wondering if there is a specific path where we would put the new files in? Is that just based on what the addon is doing? Creating something like addons/myaddon/images folder and storing them there?

Can somebody give some input on this? Thanks
 
You need to have public access if you're planning something like an uploader. Other than that, well, if you place your files into your addons folder, people might delete and lose the files by accident when they uninstall your addon. I'd recommend data/ or internal_data/
 
THese things will be uploaed from the admin panel only, with moderators and users who have trusted access.

Where is internal data? Oh I see the folder. I read some post from @Mike saying that we store images in both locations.

I am wondering if there is a simple way to get the image from $_FILES and store it to data/addon-name/ location. What would be the easiest way to achieve this?

Thanks
 
Last edited:
Can somebody help?

I am looking at this and cannot make sence of it. Is there any example of how to do this.

https://xenforo.com/community/threads/writing-images-to-the-server.134331/#post-1176026

I am just trying in admin side to have the admin/moderator select for some field image using <xf:uploadrow />

I get the image in the php under the $_FILES as well as under this $this->request->getFile('image_to_be_uloaded', false);

I saved the image in the column in the databse fine usaing getFileName from the code above. and that is stored in the db, however how do I store this image in the /data folder.

I tried using \XF\Util\File::copyFileToAbstractedPath() but getting errors. Should I use just php move_uploaded_file method insted?

Please help, I have been stucked for few days now, and hating it :)
 
ok I managed to get further, getting this error now.

Code:
InvalidArgumentException: No prefix detected in path: /data/11938991.png in src\vendor\league\flysystem\src\MountManager.php at line 138
 
Do you have any samples, or are there any samples of copyFileToAbstractedPath or similar in code?
I am having hard time understanding this process, I can butcher it probably but I wanna do it proper way. nobody seems from Xenforo to help.
 
So this worked, I am just wondering if some of the XF guys can tell me if this is a correct way of doing this. I was really hoping to use something that is already existing to do such functionality

Code:
 $filetmp = $_FILES['pool_logo']['tmp_name'];
            $filename = $_FILES['pool_logo']['name'];
            $uploaddir = 'data/' . $filename;

            if (move_uploaded_file($filetmp, $uploaddir)) {
                $this->poolSaveProcess($pool)->run();
            } else {
                echo "Upload failed";
            }
 
Back
Top Bottom