Skip to content

Instantly share code, notes, and snippets.

@codingwithsara codingwithsara/list.php Secret
Created Apr 5, 2019

Embed
What would you like to do?
Save Image File with PHP & MySQL - #2
<?php
require_once 'functions.php';
$pdo = connectDB();
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
// 画像を取得
} else {
// 画像を保存
if (!empty($_FILES['image']['name'])) {
$name = $_FILES['image']['name'];
$type = $_FILES['image']['type'];
$content = file_get_contents($_FILES['image']['tmp_name']);
$size = $_FILES['image']['size'];
$sql = 'INSERT INTO images(image_name, image_type, image_content, image_size, created_at)
VALUES (:image_name, :image_type, :image_content, :image_size, now())';
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':image_name', $name, PDO::PARAM_STR);
$stmt->bindValue(':image_type', $type, PDO::PARAM_STR);
$stmt->bindValue(':image_content', $content, PDO::PARAM_STR);
$stmt->bindValue(':image_size', $size, PDO::PARAM_INT);
$stmt->execute();
}
unset($pdo);
header('Location:list.php');
exit();
}
unset($pdo);
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Image Test</title>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.