我能够读取已经存在/下载到文件夹中的图像名称,但需要在数据库中插入,如何才能完成 我不希望通过上传图像将其插入数据库
<?php
// to fetch images name from directory in array format
$directory = glob('C:/xampp/htdocs/mail'); // your directory
foreach (glob("*.{jpg,png,gif}", GLOB_BRACE) as $filename)
{
echo $filename;
}
答案 0 :(得分:1)
$conn = mysqli_connect( $hostname, $user, $pass, $dbname );
例如:
foreach (glob("*.{jpg,png,gif}", GLOB_BRACE) as $filename)
{
$query = "INSERT INTO demo (fileName) VALUES ('$filename')";
$res = mysqli_query($conn,$query);
if($res){
echo "$filename was inserted";
}
else{
echo "$filename was not inserted";
}
}
其中,demo =您的表名和fileName是您要在其中存储文件名的列。
注意:我使用过MySQLi程序。您也可以使用MySQLi面向对象或PDO。
外部参考:
答案 1 :(得分:0)
感谢您的帮助和建议...... 我得到了所需的输出
$servername="Localhost";
$dbname = "email_tab";
$username="root";
$pwd="root";
// To check the established connection
$conn = new mysqli( $servername,$username,$pwd,$dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
//fetch images in the folder
$dir = glob('C:/xampp/htdocs/mail');
$images = glob("*.{jpg,png,gif}", GLOB_BRACE);
$filename=array(); // to assign images to array
foreach($images as $image)
{
$filename[]=$image;
}
//使用$ cnt递增以获取所有内容 图片
for($cnt=0;$cnt<=3;$cnt++)
{
$emailimage=$filename[$cnt]; // passing $cnt as to fetch all images
// insert to the database
echo $sql= "INSERT INTO email_attachment ( `ino`, `is_deleted`, `status`, `email_master_sno`, `attachment_path`, `filename`) VALUES ('1', '0', '1', '1', 'NULL', '".$emailimage."')";
$conn->query($sql);
}
echo $emailimage;