-
-
Save codeforfun-jp/9a9c1a6a15c5907813fa47955c52ba30 to your computer and use it in GitHub Desktop.
Save Image File with PHP & MySQL - #6-1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('functions.php'); | |
$pdo = connectDB(); | |
if ($_SERVER['REQUEST_METHOD'] != 'POST') { | |
// 省略 | |
} else { | |
// 画像を保存 | |
if (!empty($_FILES['image']['name'][0])) { | |
$files = $_FILES['image']; | |
for ($i = 0; $i < count($files['name']); $i++) { | |
$name = $files['name'][$i]; | |
$type = $files['type'][$i]; | |
$content = file_get_contents($files['tmp_name'][$i]); | |
$size = $files['size'][$i]; | |
$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(); | |
} | |
} | |
header('Location:list.php'); | |
exit(); | |
} | |
?> | |
<!DOCTYPE html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment