• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Determine attachment folder from the database's attachment tables


New member
What I've done:

I'm creating a feed grabber for one of my forums on my site, to be displayed on my homepage, and I want to actually have access to the images that are attached to the posts.

Right now, the rss feed only shows an attachment link, so I've bypassed the rss feed since I'm on the same server, and am accessing the database directly, and parsing the bbcode to get the first attachment id, which I was able to use to generate an attachment url that would show the image.

The issue I ran into here is that only logged in users can view attachments.


So now, I've set up my script to access the attachment_data table directly, which is great because I can use the id and attachment hash to get the thumbnail sized image rather than the full sized image.

Where I need some clarification:

My attachment thumbnails are in /data/attachments/0/

Where is the zero folder being specified? Will all of my attachment thumbnails always go into that folder? Will there eventually be a /data/attachments/1/ or 2?

Looking at the attachment tables, none of the fields seem to specify that zero, so I'm not sure if hard coding that zero into my img src when calling the image attachments will eventually cause an issue.

Chris D

XenForo developer
Staff member
There are two tables pertaining to attachments.

xf_attachment and xf_attachment_data

The latter is important with regards to the actual files, the information about them and how they are stored, but of course the xf_attachment table contains some important data too.

The tables can be joined. The attachment is identified by the attachment_id and the data is identified by the data_id. These usually match, but not always, so if you run a query, then you should always join on the data_id value.

Anyway... the important thing here is the data_id.

Typically filesystems can only handle a maximum of 1000 files in a single directory. So after each 1000 files have been uploaded, a new directory is created.

So attachments with a data_id of 1-1000 will be in the 0 directory. 1001-2000 will be in the 1 directory. etc.

In PHP this is calculated simply:

floor($dataId / 1000);


New member

I had noted the relation between the id's and was able to work that out with:

SELECT xf_attachment_data.data_id, xf_attachment_data.file_hash
FROM xf_attachment, xf_attachment_data
WHERE (xf_attachment.attachment_id = $attachment_id) AND (xf_attachment.data_id = xf_attachment_data.data_id)
I'll incorporate your function for getting the folder number into my script.