php - Getting error of allowed memory size after uploading the image to database in mysql
one text
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.';
}
}
Source