我正在尝试将照片上传到特定文件夹,并将其网址存储到数据库,但我遇到了未定义索引的问题!
这是用于上传图片的代码
$time = time();
$_SESSION['storeid'] = 11;
$target_dir = "uploads/store/products/";
$target_file1 = $target_dir . basename($_FILES["productimages1"]["name"]) . $_SESSION['storeid'] . $time;
$target_file2 = $target_dir . basename($_FILES["productimages2"]["name"]) . $_SESSION['storeid'] . $time;
$target_file3 = $target_dir . basename($_FILES["productimages3"]["name"]) . $_SESSION['storeid'] . $time;
$uploadOk = 1;
$imageFileType1 = pathinfo($target_file1,PATHINFO_EXTENSION);
$imageFileType2 = pathinfo($target_file2,PATHINFO_EXTENSION);
$imageFileType3 = pathinfo($target_file3,PATHINFO_EXTENSION);
move_uploaded_file($_FILES["productimages1"]["tmp_name"] . $_SESSION['storeid'] . $time, $target_file1);
move_uploaded_file($_FILES["productimages1"]["tmp_name"] . $_SESSION['storeid'] . $time, $target_file2);
move_uploaded_file($_FILES["productimages1"]["tmp_name"] . $_SESSION['storeid'] . $time, $target_file3);
$image1=basename( $_FILES["productimages1"]["name"] . $_SESSION['storeid'] . $time,".jpg");
$image2=basename( $_FILES["productimages2"]["name"] . $_SESSION['storeid'] . $time,".jpg");
$image3=basename( $_FILES["productimages3"]["name"] . $_SESSION['storeid'] . $time,".jpg");
和html是
<form action="" method="post">
<input type="file" name="productimages1[]" id="product1" />
<input type="file" name="productimages2[]" id="product1" />
<input type="file" name="productimages3[]" id="product1" />
</form>
任何人都可以看看它有什么问题吗?
答案 0 :(得分:1)
将enctype="multipart/form-data"
添加到您的表单。
<form action="" method="post" enctype="multipart/form-data">
...
...
</form>
在php中,由于您要存储多个上传数组的文件,请使用循环。像:
$time = time();
$_SESSION['storeid'] = 11;
$target_dir = "uploads/store/products/";
if(!empty($_FILES["productimages1"])){
foreach($_FILES["productimages1"] as $image1){
if(!empty($image1['tmp_name'])){
$target_file1 = $target_dir . basename($image1["name"]) . $_SESSION['storeid'] . $time;
move_uploaded_file($image1["tmp_name"] . $_SESSION['storeid'] . $time, $target_file1);
}
}
}
答案 1 :(得分:0)
检查一下,
<?php
if(isset($_POST['submit'])){
$time = time();
$_SESSION['storeid'] = 11;
$target_dir = "uploads/store/products/";
$target_file1 = $target_dir . $_SESSION['storeid'] . $time.$_FILES["productimages1"]["name"];
$target_file2 = $target_dir . $_SESSION['storeid'] . $time.$_FILES["productimages2"]["name"];
$target_file3 = $target_dir . $_SESSION['storeid'] . $time.$_FILES["productimages3"]["name"];
move_uploaded_file($_FILES["productimages1"]["tmp_name"], $target_file1);
move_uploaded_file($_FILES["productimages2"]["tmp_name"], $target_file2);
move_uploaded_file($_FILES["productimages3"]["tmp_name"], $target_file3);
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="productimages1" id="product1" />
<input type="file" name="productimages2" id="product1" />
<input type="file" name="productimages3" id="product1" />
<input type="submit" value="submit" name="submit"/>
</form>
在表单中添加了enctype="multipart/form-data"
。将文件上载到服务器时需要这样做。