Load photos into module.

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
lychniscuddly
New Member
New Member
Posts: 1
Joined: Tue Jun 25, 2024 2:37 am

Load photos into module.

Post by lychniscuddly »

Hello, I am a new member.

I want the admin user to be able to add photos and data to my module's back end. I'm wondering if I need to write all of the code for uploading, sanitizing, converting, and resizing myself, or if the fileManager module handles it effectively enough.

Thank you for your help.
User avatar
magallo
Dev Team Member
Dev Team Member
Posts: 121
Joined: Thu Mar 24, 2011 12:37 am

Re: Load photos into module.

Post by magallo »

You can leverage the fileManager module in CMS Made Simple to handle most of the file uploading and management tasks, which will save you from writing a lot of custom code for uploading, sanitizing, converting, and resizing images. Below is an example of how you can integrate file upload functionality using the fileManager module in your custom module's back end.

Step 1: Add a file upload field to your form
In your module's admin template, add a file upload field using the cms_file_upload tag.

Code: Select all

{* admin_template.tpl *}
   {form_start}
    
    {* Other form fields *}
    
    <div class="pageoverflow">
        <p class="pagetext">Upload Image:</p>
        <p class="pageinput">{cms_file_upload name="uploaded_image"}</p>
    </div>
    
    <div class="pageoverflow">
        <p class="pageinput">
             <input type="submit" name="{$actionid}submit" value="{$mod->Lang('submit')}"/>
        </p>
    </div>
</form>
Step 2: Handle the file upload in your module's PHP code
In your module's action.defaultadmin.php or equivalent, handle the file upload and process the image as needed.

Code: Select all

// action.defaultadmin.php
if (isset($params['submit'])) {
    $uploads_dir = cms_join_path($config['uploads_path'], 'mymodule');
    if (!is_dir($uploads_dir)) {
        mkdir($uploads_dir, 0755, true);
    }

    // Check if file is uploaded
    if (isset($_FILES['uploaded_image']) && $_FILES['uploaded_image']['error'] == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES['uploaded_image']['tmp_name'];
        $name = basename($_FILES['uploaded_image']['name']);
        
        // Sanitize the file name
        $name = preg_replace('/[^a-zA-Z0-9\._-]/', '_', $name);

        $target = cms_join_path($uploads_dir, $name);
        
        // Move the uploaded file to the target directory
        if (move_uploaded_file($tmp_name, $target)) {
            // Save file information to the database or perform other actions
            // ...
            echo 'File uploaded successfully.';
        } else {
            // Handle the error
            echo 'Failed to move uploaded file.';
        }
    } else {
        // Handle the error
        echo 'No file uploaded or upload error.';
    }
}
** not tested, but the principle should work
Magal Hezi
Pixel Solutions, Technology Partners
magal@pixelsolutions.biz | pixelsolutions.biz

Image
Post Reply

Return to “Developers Discussion”