使用PHP将多个图像上传到MYSQL数据库中 [英] Multiple Image Upload using PHP into MYSQL database
问题描述
我目前很困惑为什么我无法将一个图像数组插入到我的数据库中。
截至目前,我目前的多个文件上传能够将图像上传到我的默认目录,并且只能将第一个图像存储到我的SQL服务器数据库中,为什么这样呢? foreach命令不应该能够将我上传的多个文件分别分割并存储到数据库中吗?请解释一下这个,谢谢!
HTML代码
< form method =postenctype =multipart / form-dataaction =>
< input type =filename =files []id =filesmultiple />
< br />< br />
< button type =submit>上传选定的档案< / button>
PHP代码
foreach($ _FILES [files] [error] as $ key => $ error){
if($ error == UPLOAD_ERR_OK){
$ name = $ _FILES [ 文件] [ 名称] [$关键]
move_uploaded_file($ _ FILES [files] [tmp_name] [$ key],。$ _FILES ['files'] ['name'] [$ key]);
$ sql =INSERT INTO`test`(`image`)VALUES('。$ name。');
$ result = mysqli_query($ connection,$ sql);
回显文件。 basename($ _ FILES ['multiple_uploaded_files'] ['name'])。 已上传;
} else {
echo上传文件时出错,请重试!;
$ / code>
干杯,
技术新手学习
尝试使用此示例代码
$ path =imageuploads /;
for($ i = 0; $ i< count($ _ FILES ['file'] ['name']); $ i ++){
$ extension = explode('。',basename($ _FILES [ '文件'] [ '名称'] [$ I]));
$ path = $ path。 md5(uniqid())。 。 $分机[计数($分机)-1];
if(move_uploaded_file($ _ FILES ['file'] ['tmp_name'] [$ i],$ path)){
//插入查询
echo成功上传;
} else {
echoUpload in Error;
上面的代码不起作用,请告诉场景
I am currently very puzzled over why I am unable to insert an array of images over into my database. As of now, my current multiple file upload is able to upload images into my default directory, and able to store ONLY the first image into my SQL server database, why is this so? Shouldn't the foreach command be able to split all the multiple file that i upload and store them respectively into the database? Please shed some lights on this, thank you!
HTML Code
<form method="post" enctype="multipart/form-data" action="">
<input type="file" name="files[]" id="files" multiple />
<br /><br />
<button type="submit">Upload selected files</button>
PHP Code
foreach ($_FILES["files"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["files"]["name"][$key];
move_uploaded_file($_FILES["files"]["tmp_name"][$key], "" . $_FILES['files']['name'][$key]);
$sql = "INSERT INTO `test`(`image`) VALUES ('" . $name . "')";
$result = mysqli_query($connection, $sql);
echo "The file " . basename($_FILES['multiple_uploaded_files']['name']) . " has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
}
Cheers, A tech newbie learning in progress.
try with this example code,
$path = "imageuploads/";
for($i=0; $i<count($_FILES['file']['name']); $i++){
$extension = explode('.', basename( $_FILES['file']['name'][$i]));
$path = $path . md5(uniqid()) . "." . $extension[count($extension)-1];
if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $path )) {
//insert query
echo "uploaded successfully";
} else{
echo "Error in Upload";
}
}
Above code is not worked, please tell the scenario
这篇关于使用PHP将多个图像上传到MYSQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!