Content/Posts Management | CMS Tutorial Part 7
HTML-код
- Опубликовано: 1 мар 2023
- In this part of the tutorial let's build the post-management and add content!
Make sure to subscribe & ring a bell 🔔 to get notified about new lessons.
📰 Source code: github.com/mdbootstrap/knowle...
______________________________________
📥 MDB UI KIT FREE download - mdbootstrap.com/
💎 MDBootstrap Pro - mdbootstrap.com/docs/standard...
📤 FREE hosting - mdbgo.com/
______________________________________
👨👩👧👦 If you have any questions - don't hesitate to ask on our Facebook group:
/ 682245759188413
⭐ Support the creation of open-source packages with a STAR on GitHub
github.com/mdbootstrap/mdb-ui...
_______________________________
Table of Contents
0:00 Intro
Thanks a bunch for this tutorial, Helping me put together a portfolio! :)
Thanks, but what if I want to add an image on a post, how can I do it?
I hope this help. I am not sure which part is a problem on your side, so here is a longer and more detail explanation:
Update the Database: Add a column in your posts table to store image file names. You can do this with the SQL command: ALTER TABLE posts ADD COLUMN image VARCHAR(255) NULL;.
Modify the CMS Form: Include an image upload field in your post form. Make sure your form tag includes enctype="multipart/form-data" for handling file uploads. Here's a snippet:
html
Post Image:
Submit
Handle Image Upload in PHP: In your script for adding or editing posts (e.g., post_add.php), add logic to handle the uploaded file. Check if the file is set, validate it, and move it to your desired directory. Here's a basic approach:
php
if (isset($_FILES['postImage']) && $_FILES['postImage']['error'] == 0) {
$allowed = ['jpg', 'jpeg', 'png', 'gif']; // Allowed file types
$fileName = $_FILES['postImage']['name'];
$fileTmp = $_FILES['postImage']['tmp_name'];
$fileExt = strtolower(end(explode('.', $fileName)));
$uploadPath = 'uploads/' . basename($fileName);
if (in_array($fileExt, $allowed)) {
if (!file_exists('uploads')) {
mkdir('uploads', 0777, true);
}
if (move_uploaded_file($fileTmp, $uploadPath)) {
// Image uploaded, now save $uploadPath with your post in the database
} else {
// Handle upload error
}
} else {
// Handle invalid file extension
}
}
Display the Image: When showing the post, include the image using the path stored in the database. For example:
php
if (!empty($post['image'])) {
echo '';
}
Remember to handle security carefully, especially validating uploaded files to prevent security issues.
Hope that helps! Keep Coding :)
sir. how i want to make . textarea just drag and drop mdboostrap component. can we do it sir in tynymce.
Can you expand pls?