0

I know I can create a folder in uploads directory using wp_mkdir_p() function of WordPress. I'm working on a file manager that will use the rest api to get a list of files since it's vue based for the front-end, is possible to get also all the folders that are created inside the uploads folder?

6
  • 2
    while there isn't a WordPress API for this, there are generic PHP functions that can do this. Also note that the WP media library isn't reading the filesystem to display attachments, it's reading the database. This means they're posts with post meta and can have taxonomies. You could also read the media endpoint and inspect the URL paths to see the folders which would let you build this in a way that works with any WP site even ones you don't own and run yourself, albeit with an initial local indexing step upfront in Vue. Note that traversing the folder structure can get slow
    – Tom J Nowell
    Aug 31, 2023 at 13:38
  • 1
    Thinking about this further, there is WP Filesystem which acts as a wrapper, primarily to make it easier to write when direct file access may not be available. For maximum compatibility you might need to look at this since sites that use plugins to provide S3 bucket based media etc may require it, but there are performance costs associated. If PHP APIs will work for your client and you aren't sharing this then check those work first
    – Tom J Nowell
    Aug 31, 2023 at 15:28
  • Since the front-end is made using vue, I've found a component that will act as a file manager github.com/n1crack/vuefinder it also have a php server side backend github.com/n1crack/vuefinder-php, I think it can be integrated with WP filesystem to use the permissions and let only certain users see files thy allowed to see?
    – ICTDEV
    Sep 1, 2023 at 8:42
  • Anyway thank you for the tips. Please feel free to leave an answer so I can accept it.
    – ICTDEV
    Sep 1, 2023 at 8:43
  • 1
    you'd need to look up the vuefinder php docs to figure out how to write an adapter, though if you can point it at your uploads folder that would suffice. Note that integration with WP user permissions etc will be a different ballgame and problematic since WP media aren't files but posts in the database, and you'd need to figure out which file belongs to which attachment. Likewise you could use this to rename/move/delete files, breaking attachments in the process. WP's media library doesn't inspect the uploads folder os it will show 404s if those files are moved. New files won't appear either
    – Tom J Nowell
    Sep 1, 2023 at 13:37

1 Answer 1

1

The closest is the WP Filesystem API, but that's mainly intended for compatibility purposes, e.g. to enable files to be written when the only access is via FTP and direct filesystem write access is unavailable.

Particularly: https://developer.wordpress.org/reference/classes/wp_filesystem_base/dirlist/

However, if you are not distributing this code and only using it for yourself or known environments, you can use standard PHP APIs.

Note that you will need to perform some recursion to achieve this task, and there are costs to doing this, especially if the filesystem specified for WordPress involves remote requests such as FTP/S3/etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.