我想使用输入类型'file'作为多个添加图像goa.jpg,kerala.jpg,delhi.jpg。以下代码完美地插入单个图像。
现在我想使用for循环执行多个图像上传,但我没有得到没有错误的实际代码。任何人都有这方面的经验吗?
$file_name=$_FILES["packageimage"]["name"];
$temp_name=$_FILES["packageimage"]["tmp_name"];
$imgtype=$_FILES["packageimage"]["type"]; if($imgtype!='image/jpeg'&&$imgtype!='image/jpg'&&$imgtype!='image/gif'&&$imgtype!='image/png'&&$imgtype!='image/bmp')
{ $msg = "Please upload only Image file"; }
else{
$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y")."--".time().$ext;
$target_path = "../upload/products/".$imagename;
move_uploaded_file($temp_name, $target_path);
// query to insert images.
$query=mysql_query("insert into tbl_products set catid='".$catid."', packageimage='".$imagename."', add_date=now()");
if($query)
{
$msg="Product Added Successfull";
}
当我在上面的代码中尝试使用for循环时,它只存储第一个图像并仅显示第一个图像。
答案 0 :(得分:3)
请看看你可以了解多重上传的所有想法 http://php.net/manual/en/features.file-upload.multiple.php
见下文示例
searchReleases($("[name=release]").val());
function searchReleases(givenName) {
var list;
var data = givenName;
@foreach (var a in @Model.Releases) {
if (@a.Name.Contains(givenName)) {
list+= a.Name + "=";
}
}
}
参见结果如下
<?php
print_r($_FILES);
?>
<form action="" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
以下是foreach的问题
Array
(
[userfile] => Array
(
[name] => Array
(
[0] => cancelled booking - PAYG.png
[1] => cancelled booking - PAYG.png
)
[type] => Array
(
[0] => image/png
[1] => image/png
)
[tmp_name] => Array
(
[0] => C:\xampp\tmp\php402A.tmp
[1] => C:\xampp\tmp\php402B.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 99134
[1] => 99134
)
)
)
当结果看到Bellow:
<?php
foreach ($_FILES['userfile'] as $position => $file){
print_r($file);
}
?>
所以现在你可以理解了
由于 PRATIK
答案 1 :(得分:1)
您可以使用multiple
属性上传多个文件,如下所示:
<强> HTML 强>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" multiple="multiple" name="packageimage[]" />
<input type="submit" value="Upload" name="submit" />
</form>
<强> PHP 强>
<?php
if(isset($_POST['submit'])){
// directory where the files will get uploaded
$target_dir = "upload" . DIRECTORY_SEPARATOR . "products";
// count the number of files uploaded
$num_of_files = count($_FILES["packageimage"]["name"]);
// loop through each file to upload
for($i = 0; $i < $num_of_files; ++$i){
// valid file extensions
$valid_extensions = array("gif", "png", "jpg", "jpeg", "bmp");
// get the file extension
$ext = strtolower(pathinfo($_FILES["packageimage"]["name"][$i],PATHINFO_EXTENSION));
// now check against permissible extensions
if(in_array($ext, $valid_extensions)){
// target file name
$target_file = $target_dir . DIRECTORY_SEPARATOR . date("d-m-Y") . "--" . time() . "." . $ext;
// upload file
if (move_uploaded_file($_FILES["packageimage"]["tmp_name"][$i], $target_file)) {
echo "The file ". basename($_FILES["packageimage"]["name"][$i]). " has been uploaded. <br />";
// do your database operations here
} else {
echo "Sorry, there was an error uploading your file.";
}
}else{
echo "Please upload only Image file";
}
}
}
?>
旁注:为了提供跨平台兼容性,您应该使用PHP的DIRECTORY_SEPARATOR
常量来编写路径字符串,例如: "..".DIRECTORY_SEPARATOR."foo"
,因为在Windows上执行此操作的方式为"..\foo"
,而在其他所有内容(Linux,UNIX,Mac OS X)上,它将为"../foo"
。