Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to upload images to sql database php PDO

<?php 
include "database_connection.php";
    
if(isset($_POST['submit'])) {
   
    // Count total files
    $countfiles = count($_FILES['files']['name']);
    
    // Prepared statement
    $query = "INSERT INTO images (name,image) VALUES(?,?)";
   
    $statement = $conn->prepare($query);
   
    // Loop all files
    for($i = 0; $i < $countfiles; $i++) {
   
        // File name
        $filename = $_FILES['files']['name'][$i];
       
        // Location
        $target_file = 'upload/'.$filename;
       
        // file extension
        $file_extension = pathinfo(
            $target_file, PATHINFO_EXTENSION);
              
        $file_extension = strtolower($file_extension);
       
        // Valid image extension
        $valid_extension = array("png","jpeg","jpg");
       
        if(in_array($file_extension, $valid_extension)) {
   
            // Upload file
            if(move_uploaded_file(
                $_FILES['files']['tmp_name'][$i],
                $target_file)
            ) {
  
                // Execute query
                $statement->execute(
                    array($filename,$target_file));
            }
        }
    }
      
    echo "File upload successfully";
}
?>
  
<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Geeks for geeks Image Upload</title>
</head>
  
<body>
    <h1>Upload Images</h1>
  
    <form method='post' action='' 
        enctype='multipart/form-data'>
        <input type='file' name='files[]' multiple />
        <input type='submit' value='Submit' name='submit' />
    </form>
  
    <a href="view.php">|View Uploads|</a>
</body>
  
</html>
Comment

PREVIOUS NEXT
Code Example
Php :: php ?: 
Php :: php: foreach loop 
Php :: collection methods laravel 
Php :: how to get data from laravel api 
Php :: route laravel 
Php :: current date time in php for input 
Php :: read an email with php 
Php :: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/7.4.29 Server at localhost Port 8080 
Php :: laravel verification email 
Php :: laravel authentication 
Php :: simple php round example 
Php :: php catch mysqli_connect(): (HY000/1045): Access denied 
Php :: php abstract class static method 
Php :: how to increment date in php 
Php :: xrp to php 
Php :: schema add column laravel 
Php :: laravel edit 
Java :: import math java 
Java :: how to loop javafx media player 
Java :: regex java email validation 
Java :: java pause 
Java :: Share application “link” in Android 
Java :: swing disable button 
Java :: how to get the width and height of a string in java 
Java :: declaração de matriz em java 
Java :: java lowercase in a scanner 
Java :: java get creation date of file 
Java :: java uuid from string 
Java :: java remove first digit from int 
Java :: processing draw circle 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =