php mysql多文件上传,将多个图像文件上传到php mysql库

我得到了这个厨房约占我想要它的65%.我想知道是否有人可以查看以下代码并告诉我如何将多个图像上传到我的图库.

这是代码.

简单的管理表单代码:

Category:

treeremoval

treetrimming

treebracing

stumpgrinding

firewood

cleanup

Caption:

Image to upload:

Category:

treeremoval

treetrimming

treebracing

stumpgrinding

firewood

cleanup

Caption:

Image to upload:

uploader.php代码:

include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");

$dataType = mysql_real_escape_string($_POST["dataType"][$i]);

$title = mysql_real_escape_string($_POST["title"][$i]);

$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));

$fileName = uniqid() . '.' . $fileData['extension'][$i];

$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);

for($i=0;$i

$dataType = mysql_real_escape_string($_POST["dataType"][$i]); // get the dataType with the same key - $i

$title = mysql_real_escape_string($_POST["title"][$i]); // get the title with the same key - $i

$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));

while(file_exists($target_path))

{

$fileName = uniqid() . '.' . $fileData['extension'];

$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);

}

if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path))

{ // The file is in the images/gallery folder. Insert record into database by

// executing the following query:

$sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";

$retval = mysql_query($sql);

echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery

Add another image
";

}

else

{

echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!
";

}

} // close your foreach

?>

我尝试重复4次表单代码,但它只会将1张图片上传到图库.

任何帮助将不胜感激.

谢谢!

解决方法:

在表单中,添加多个文件输入.一种方法是使用数组名称 – image []

Image to upload:

Image to upload:

Image to upload:

.... // as many as you want. Just be aware of upload_max_filesize, memory_limit, post_max_size etc.

然后在你的uploader.php中,用for循环包装你的文件上传代码

for($i=0;$i

$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));

...

if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path))

{

...

echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery
";

}

else

{

echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!
";

}

} // close your foreach

如果你想做其他的多个,可以用同样的方式完成(我缩写选择以减少复制/粘贴) –

// 1st set

Category:

...

Caption:

Image to upload:

// 2nd set

Category:

...

Caption:

Image to upload:

// and so on, as many as you want

...

和你的PHP,围绕所有元素的for循环

for($i=0;$i

$dataType = mysql_real_escape_string($_POST["dataType"][$i]); // get the dataType with the same key - $i

$title = mysql_real_escape_string($_POST["title"][$i]); // get the title with the same key - $i

$fileData = pathinfo(basename($_FILES["image"]["name"][$i]));

...

if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path))

{

...

echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery
";

}

else

{

echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!
";

}

} // close your foreach

编辑

你快到了.删除for循环上方的重复代码.删除basename(),因为这会导致扩展失败,而pathinfo()将返回[‘basename’].

include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");

for($i=0;$i

if($_FILES["image"]["name"][$i] != ''){ // don't insert if file name empty

$dataType = mysql_real_escape_string($_POST["dataType"][$i]);

$title = mysql_real_escape_string($_POST["title"][$i]);

$fileData = pathinfo($_FILES["image"]["name"][$i]);

$fileName = uniqid() . '.' . $fileData['extension'];

$target_path = $_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName;

while(file_exists($target_path)){

$fileName = uniqid() . '.' . $fileData['extension'];

$target_path = $_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName;

}

if (move_uploaded_file($_FILES["image"]["tmp_name"][$i], $target_path)){ // The file is in the images/gallery folder.

// Insert record into database by executing the following query:

$sql="INSERT INTO images (data_type, title, file_name) "."VALUES('$dataType','$title','$fileName')";

$retval = mysql_query($sql);

echo "The image {$_FILES['image']['name'][$i]} was successfully uploaded and added to the gallery

Add another image
";

}

else

{

echo "There was an error uploading the file {$_FILES['image']['name'][$i]}, please try again!
";

}

}

} // close your foreach

?>

标签:php,mysql,multifile-uploader

来源: https://codeday.me/bug/20190725/1533842.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值