I am trying to upload photos to a specific folder and also storing their url to database but I am getting a problem of undefined index!
This is the code that is used to upload images
$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");
and html is
<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>
Can anyone look upon what's wrong with it?
Add enctype="multipart/form-data"
to your form.
<form action="" method="post" enctype="multipart/form-data"> ... ... </form>
In php, as your are store your files for an array for multiple upload, use loop. like:
$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); } } }
Check this,
<?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>
Added enctype="multipart/form-data"
to your form. This is required when you upload files to server.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.