[英]Uploading a images in MySql using PHP
我正在嘗試通過聯系表單上傳圖像,以獲取特定文件夾中的服務器及其可視化數據庫的時間。 當我提交表單時,我收到此錯誤:
文件是圖像 - 圖像/jpeg。 Notice: Undefined index: fileToUpload in C:\xampp\htdocs\Web\form.php on line 68 Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\Web\form.php on line 68注意:未定義索引:C:\xampp\htdocs\Web\form.php 第 82 行中的圖像
這是代碼:HTML:
<div class="container-form">
<form action="form.php" method="post" enctype="multipart/form-data">
<div class="image">
Прикачете ваша снимка! (максимален размер 20МБ)<br>
<input type="file" name="image" id="image" size="40" required="true" />
</div>
<div class="submit">
<input type="submit" name="submit" value="Изпращане" id="submit" />
</div>
</form>
</div>
PHP:
// Connect to MyQSL
$link = mysqli_connect("localhost", "root", "", "form");
$link->query("SET NAMES 'UTF8'");
// Check our connection
if ($link === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 20000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" ) {
echo "Sorry, only JPG, JPEG and PNG files are allowed.";
$uploadOk = 0;
}
// Escape user inputs for security
$name = mysqli_real_escape_string($link, $_REQUEST['name']);
$date = mysqli_real_escape_string($link, $_REQUEST['date']);
$image = mysqli_real_escape_string($link, $_REQUEST['image']);
// Insert our data
$sql = "INSERT INTO form (name, date, image) VALUES ('$name', '$date', '$image')";
// Print response from MyQSL
if (mysqli_query($link, $sql))
{
echo "<div class='echo-complete'> <div class='echo-text'> Формата беше приета успешно. Благодарим ви.";
}
else
{
echo "<div class='echo-error'> <div class='echo-text'> ГРЕШКА: Не може да се изпълни $sql." .
mysqli_error($link);
}
// Close our connection
mysqli_close($link);
數據庫:圖像列是 BLOB 類型
<div class="container-form">
<form method="post" enctype="multipart/form-data">
<div class="image">
<input type="file" name="image" id="image" size="40" required="true" />
</div>
<div class="submit">
<input type="submit" name="submit" value="Upload" id="submit" />
</div>
</form>
</div>
<!--Now we will connect to the database.-->
<?php
// define file
if (isset($_POST["submit"])) {
$fname = $_FILES["image"]["name"];
$fsize = $_FILES["image"]["size"];
$ferror = $_FILES["image"]["error"];
$ftype = $_FILES["image"]["type"];
$ftmp = $_FILES["image"]["tmp_name"];
// Get the extension of the file and verify if we support it
$allowed = array(
"png",
"jpeg",
"jpg",
"gif"
); //make sure it's not capital letters.
$actual_extension = explode(".", $fname); // Get what we have after the . on the file name
$extension = end($actual_extension); // make sure we got the extension
//check if the extension is supported.
if (!in_array($extension, $allowed)) {
echo "Sorry! We do not support this format" . "<br>";
}
// Now check if we have a file error, else: upload
if ($ferror == 1) { // Check if we have an error
echo "Something went wrong" . "<br>";
} else {
if ($fsize > 100000) { // check if the uploaded file matches the size we allow.
echo "Your file is BIG" . "<br>";
} else {
$new_name = uniqid('', true); // we will give the uploaded file a unique id
$path = "uploads/" . $new_name . "." . $extension; // Define the path and upload the file with new name+ "." + the file extension.
if (move_uploaded_file($ftmp, $path)) {
echo "file uploaded succesfully";
} else {
echo "something went wrong with the file upload";
}
}
}
$link = mysqli_connect("localhost", "root", "", "sadik");
$query = "INSERT INTO `table` (`image`) VALUES ($new_name) ";
mysqli_query($link, $query);
// ...
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.