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.
Load photos into module.
-
- New Member
- Posts: 1
- Joined: Tue Jun 25, 2024 2:37 am
Re: Load photos into module.
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.
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.
** not tested, but the principle should work
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>
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.';
}
}