I am trying to send multiple images to the database I tried to do the and after sending images to the database I am getting error (Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 32768 bytes) in C:\xampp\htdocs\blog\admin\add_blog.php on line 1). please help me to solve this. I am new to PHP and MYSQL.
add_blog.php
<?php include 'header.php';
ini_set('memory_limit', '44M');
if (isset($_SESSION['user_data'])) {
// This will fetch the author id that is stored in index['0'].
$author_id = $_SESSION['user_data']['0'];
$sql = "SELECT * FROM categories";
$query = mysqli_query($config, $sql);
}
?>
<div class="container">
<h5 class="mb-2 text-gray-800">School</h5>
<div class="row">
<div class="col-xl-8 col-lg-6">
<div class="card">
<div class="card-header">
<h6 class="font-weight-bold text-primary mt-2">Add School</h6>
</div>
<div class="card-body">
<form action="" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<input type="text" name="blog_title" placeholder="Title" class="form-control">
</div>
<div class="mb-3">
<input type="text" name="school_tagline" placeholder="Tagline" class="form-control">
</div>
<div class="mb-3">
<input type="text" name="school_rating" placeholder="Rating" class="form-control">
</div>
<div class="mb-3">
<input type="text" name="school_projects" placeholder="Projects" class="form-control">
</div>
<div class="mb-3">
<input type="text" name="school_events" placeholder="School Events" class="form-control">
</div>
<div class="mb-3">
<input type="text" name="school_mobile" placeholder="School Number" class="form-control">
</div>
<div class="mb-3">
<input type="text" name="school_email" placeholder="School Email" class="form-control">
</div>
<div class="mb-3">
<label for="">About School Infomartion</label>
<textarea class="form-control" name="blog_body" id="blog" rows="2"></textarea>
</div>
<div class="mb-3">
<label for="">School Activities Infomartion</label>
<textarea class="form-control" name="school_activities" id="blog" rows="2"></textarea>
</div>
<div class="mb-3">
<label for="">If you have mission or vision please describe</label>
<textarea class="form-control" name="school_mission_vision" id="blog" rows="2"></textarea>
</div>
<div class="mb-3">
<input type="file" name="blog_image" multiple="multiple" class="form-control">
</div>
<div class="mb-3">
<select class="form-control" name="category">
<option value="" name="category">Select Category</option>
<!-- $cats will fetch all all the data that is store in the categories. -->
<?php while ($cats = mysqli_fetch_assoc($query)) { ?>
<option value="<?= $cats['cat_id'] ?>">
<?= $cats['cat_name'] ?>
</option>
<?php } ?>
}
</select>
</div>
<div class="mb-3">
<input type="submit" name="add_blog" value="Add" class="btn btn-primary">
<a class="btn btn-secondary" href="index.php">Back</a>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php
include 'footer.php';
if (isset($_POST['submit'])) {
$destination = "upload/";
$allow_type = ['jpg', 'png', 'jpeg', 'svg', 'webp'];
$filename = array_filter($_FILES['blog_image']['name']);
if (!empty($filename)) {
foreach($filename as $key=>$val){
$filename = basename($_FILES['blog_image']['name'])[$key];
$targetFilePath = $targetDir . $filename;
$filetype = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if (in_array($fileType, $allow_type)) {
if (move_uploaded_file($_FILES["blog_image"]["tmp_name"][$key], $targetFilePath)) {
$insertValuesSQL .= "('".$filename."', NOW()),";
}
else {
$errorUpload .= $_FILES['blog_image']['name'][$key].' | ';
}
}
else{
$errorUploadType .= $_FILES['blog_image']['name'][$key]. ' | ';
}
}
$errorUpload = !empty($errorUpload)?'upload Error: '.trim($errorUpload. ' | '): '';
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType. ' | '):'';
$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
if (!empty($insertValuesSQL)) {
$insertValuesSQL = trim($insertValuesSQL, ',');
$insert = $db->query("INSERT INTO blog (blog_image) VALUES $insertValuesSQL");
if ($insert) {
$statusMsg = "Files are uploaded successfully".$errorMsg;
}
else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}
else{
$statusMsg = "Upload failed!".$errorMsg;
}
}
else{
$statusMsg = 'Please select a file to upload.';
}
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.