PHP – 通过一种形式的多个输入上传多个文件
发布时间:2020-12-13 16:56:42 所属栏目:PHP教程 来源:网络整理
导读:我有一个脚本,允许我上传数据库中的产品的照片.我想通过相同的表单上传多个产品的照片.我构建了一个动态表单,每个产品有一个文件输入,产品ID作为标识符.出于某种原因,当我尝试接收另一端的文件时,我没有得到所有东西.这是我的代码……任何想法? form class=
我有一个脚本,允许我上传数据库中的产品的照片.我想通过相同的表单上传多个产品的照片.我构建了一个动态表单,每个产品有一个文件输入,产品ID作为标识符.出于某种原因,当我尝试接收另一端的文件时,我没有得到所有东西.这是我的代码……任何想法?
<form class="form-signin" action="products_csv.php" method="POST" enctype="multipart/form-data" style="max-width:600px !important"> <h2 class="form-signin-heading">Upload Photos</h2> <h4>Product #1</h4> <input type="hidden" name="product_ids[]" value="23" ?> <input type="file" class="form-control" name="photos[23][]" id="photos" multiple><br /><br /> <h4>Product #2</h4> <input type="hidden" name="product_ids[]" value="24" ?> <input type="file" class="form-control" name="photos[24][]" id="photos" multiple><br /><br /> <h4>Product #3</h4> <input type="hidden" name="product_ids[]" value="25" ?> <input type="file" class="form-control" name="photos[25][]" id="photos" multiple><br /><br /> <h4>Product #4</h4> <input type="hidden" name="product_ids[]" value="26" ?> <input type="file" class="form-control" name="photos[26][]" id="photos" multiple><br /><br /> <h4>Product #5</h4> <input type="hidden" name="product_ids[]" value="27" ?> <input type="file" class="form-control" name="photos[27][]" id="photos" multiple><br /><br /> <h4>Product #6</h4> <input type="hidden" name="product_ids[]" value="28" ?> <input type="file" class="form-control" name="photos[28][]" id="photos" multiple><br /><br /> <button class="btn btn-large btn-primary" type="submit">Upload</button> </form> 这是PHP代码: foreach($_POST as $key => $value) { $$key = $value; } //Upload photos and link to records in DB $path = "uploads/"; // Upload directory $total_count = 0; $total_products = 0; foreach($product_ids as $key => $value) { $query = "SELECT * FROM `variants_queue` WHERE `id` = '$value'"; $rs = mysql_query($query,$con); $row = mysql_fetch_row($rs); $sku = $row[1]; $num_of_photos = count($_FILES["photos"]['name'][$value]); $count = 0; for($i=0;$i<=$num_of_photos-1;$i++) { $filename = $path.$sku."-".$i.".jpg"; $filesize = $_FILES['photos']['size'][$value][$i]; if($filesize > 0) { move_uploaded_file($_FILES['photos']["tmp_name"][$value][$i],$filename); $query2 = "INSERT INTO `variants_photos_queue` (variants_queue_id,filename,size) VALUES ('$value','$filename','$filesize')"; $rs2 = mysql_query($query2,$con); $count++; } } } 解决方法
这是完整的代码吗?
$product_ids没有任何引用. 请尝试这样. HTML: <form class="form-signin" action="products_csv.php" method="POST" enctype="multipart/form-data" style="max-width:600px !important"> <h2 class="form-signin-heading">Upload Photos</h2> <h4>Product #1</h4> <input type="hidden" name="product_ids[]" value="23" ?> <input type="file" class="form-control" name="photos[]" id="photos" multiple><br /><br /> <h4>Product #2</h4> <input type="hidden" name="product_ids[]" value="24" ?> <input type="file" class="form-control" name="photos[]" id="photos" multiple><br /><br /> <h4>Product #3</h4> <input type="hidden" name="product_ids[]" value="25" ?> <input type="file" class="form-control" name="photos[]" id="photos" multiple><br /><br /> <h4>Product #4</h4> <input type="hidden" name="product_ids[]" value="26" ?> <input type="file" class="form-control" name="photos[]" id="photos" multiple><br /><br /> <h4>Product #5</h4> <input type="hidden" name="product_ids[]" value="27" ?> <input type="file" class="form-control" name="photos[]" id="photos" multiple><br /><br /> <h4>Product #6</h4> <input type="hidden" name="product_ids[]" value="28" ?> <input type="file" class="form-control" name="photos[]" id="photos" multiple><br /><br /> <button class="btn btn-large btn-primary" type="submit">Upload</button> </form> PHP: <?php $path = "uploads/"; // Upload directory $total_count = 0; $total_products = 0; $product_ids = $_POST["product_ids"]; foreach($product_ids as $key => $value) { $query = "SELECT * FROM `variants_queue` WHERE `id` = '$value'"; $rs = mysql_query($query,$con); $row = mysql_fetch_row($rs); $sku = $row[1]; $num_of_photos = count($_FILES["photos"]); $count = 0; $filename = $path.$sku."-".$key.".jpg"; $filesize = $_FILES['photos']['size'][$key]; if($filesize > 0) { move_uploaded_file($_FILES['photos']["tmp_name"][$key],$con); $count++; } } ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |