gpt4 book ai didi

javascript - 通过 PHP 将图像文件(不是通过表单选择,只是在一个文件夹中)上传到 mysql?

转载 作者:行者123 更新时间:2023-11-29 06:12:40 25 4
gpt4 key购买 nike

我只是不清楚如何格式化它 - 过去我让用户通过表单和 javascript 上传他们从计算机中选择的图像,如下所示:

$("#uploadimage").on('submit',(function(e) {
e.preventDefault();


$.ajax({
url: "../php/upload.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data) // A function to be called if request succeeds
{

}
});
}));

将文件发送到 php 脚本:

if(isset($_FILES["file"]["type"]))
{
$validextensions = array("jpeg", "jpg", "png");
$maxsize = 99999999;
$temporary = explode(".", $_FILES["file"]["name"]);
$file_extension = end($temporary);
if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")
) && ($_FILES["file"]["size"] < $maxsize)//Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {

$size = getimagesize($_FILES['file']['tmp_name']);
$type = $size['mime'];
$imgfp = fopen($_FILES['file']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['file']['name'];

$sql = new mysqli("localhost","username","password","sqlserver");
$imgfp64 = base64_encode(stream_get_contents($imgfp));
$update = "UPDATE sqlserver.imageblob set image='".$imgfp64."', image_type='".$type."', image_name='".$name."', image_size='".$size."' where user_id=".$account['id'];
$sql->query($update);

然后我就可以像这样显示图像并回显 HTML:

$imgdata = $array['image']; //store img src
$src = 'data:image/jpeg;base64,'.$imgdata;

但现在我需要上传一个我已经存储在文件夹中的图像文件,即 ../images/image1.png 不是从表单上传的文件。

理想情况下我会写:

$imgfile = "../images/image1.png"

然后将其插入我的 php 以代替 $_FILES['file']['name'] 但我不知道如何正确地写出它。我是 mysql 的新手,只是通过上面的文件名收到错误消息。

如何将文件夹中已有的图像上传到 mysql 表?

我尝试过的:

enter image description here

最佳答案

您可以使用 DirectoryIterator :

将此文件保存到您的 images 文件夹并运行它:

<?php

$validextensions = array("jpeg", "jpg", "png");
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {

$extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION)); /* GET EXTENSION OF FILE */

if(in_array($extension, $validextensions)){ /* IF FILE IS IMAGE; JPEG, JPG, OR PNG */

/* CHECK IF IMAGE IS ALREADY IN THE DATABASE */
$check = $sql->query("SELECT * FROM image_table WHERE image_col = ?"); /* REPLACE NECESSARY TABLE AND COLUMN NAME */
$check->bind_param("s", $fileinfo->getFilename());
$check->execute();
$check->store_result();
$noofrows = $check->num_rows;
$check->close();

if($noofrows == 0){ /* IF IMAGE NAME IS NOT YET IN THE DATABASE */
/* INSERT FILE NAME TO DATABASE */
$stmt = $sql->query("INSERT INTO image_table (image_col) VALUES (?)"); /* REPLACE NECESSARY TABLE AND COLUMN NAME */
$stmt->bind_param("s", $fileinfo->getFilename());
$stmt->execute();
$stmt->close();
}

}
}
}

?>

上面会将图像名称保存到您的数据库中。

当你想显示图像时,只需运行这个查询:

$getimg = $sql->prepare("SELECT image_col FROM image_table"); /* REPLACE NECESSARY TABLE AND COLUMN NAME */
$getimg->execute();
$getimg->bind_result($image);
while($getimg->fetch()){
echo '<img src="images/'.$image.'">';
}
$getimg->close();

关于javascript - 通过 PHP 将图像文件(不是通过表单选择,只是在一个文件夹中)上传到 mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37517681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com