Custom Post Type: Upload Multiple Images

So I’m making this Property Listing theme for a client of mine, and on the Properties Custom Post Type (CPT) which I have created I need to add a meta box that allows him to upload multiple pictures – something similar to this:

Upload Meta Box

How could I do this? Everything I have seen so far recommends plugins like Advanced Custom Fields but I would like to avoid using 3rd party Plugins if I can.

Thanks 🙂

Solutions Collecting From Web of "Custom Post Type: Upload Multiple Images"

I recommend you these videos –>

In my opinion I prefer use the plugin ACF. The plugin helps to save time. It’s easy to use and the client will be happy. Also, the client won’t care if you used a plugin or you wrote the code by yourself, the client will care if the functionality works.

I am the first in not installing plugins but ACF plugin is necessary (mandatory). I use it in all my projects.

Add a security object for file uploads like so:

class FileSecure
    public resource $Allowed;
    private object $Info;
    public function __construct($allow)
        $this->Allowed = $allow;    
        $this->Info = new finfo();
    public function Check($file) : bool
        if(in_array($fileType = $this->Info->file($file, FILEINFO_MIME_TYPE, $this->Allowed))) { return true; } else { return false; }

$fileCheck = array(
     'Image' => new FileSecure(['image/bmp', 'image/gif', 'image/jpeg', 'image/png']),
     'Text' => new FileSecure(['text/plain']),
     'Compressed' => new FileSecure(['application/zip', 'application/x-rar-compressed'])

Click here to view all the MIME name’s to extensions

Then add a front-end form which allows file upload and just pass it through this object before use of the uploaded file.

// note it must exist on the server before checking
    // delete the file

You could use the Database to hold file locations or just have a default upload directory on your server and iterate through the files held within that directory.