在這里,我正在使用 ajax 創建一個 FormData,因此我可以將一些輸入檔案發送到 php 腳本,然后我可以將其保存為我的資料庫中的 blob。問題是如何在將 FormData 發送到 php 腳本后動態回圈它,以便我可以動態獲取每個名稱并保存用戶上傳的內容并保留空輸入
更多解釋:
<input id = "Pay_cert1" type="file" name="" />
<input id = "Pay_cert2" type="file" name="" />
<input id = "Pay_cert3" type="file" name="" />
<input id = "Pay_cert4" type="file" name="" />
然后我得到輸入的值“如果有一個值”并將其附加到我的資料表單中
var Pay_cert1= $('#Pay_cert1').prop('files')[0];
var Pay_cert2= $('#Pay_cert2').prop('files')[0];
var Pay_cert3= $('#Pay_cert3').prop('files')[0];
var Pay_cert4= $('#Pay_cert4').prop('files')[0];
var form_data = new FormData();
if(document.getElementById("Pay_cert1").files.length != 0)
{
form_data.append('Pay_cert1', Pay_cert1);
}
if(document.getElementById("Pay_cert2").files.length != 0)
{
form_data.append('Pay_cert2', Pay_cert2);
}
if(document.getElementById("Pay_cert3").files.length != 0)
{
form_data.append('Pay_cert3', Pay_cert3);
}
if(document.getElementById("Pay_cert4").files.length != 0)
{
form_data.append('Pay_cert4', Pay_cert4);
}
$.ajax({
url: 'Upload.php',
cache: false,
contentType: false,
processData: false,
data:form_data ,
type: 'post',
success : function(data) {
}
});
在 PHP 中,我想在我的 DataForm 上動態回圈并獲取每個僅附加的檔案的 tmpname,所以我怎么說
foreach(name in $_FILES['']) // how can I loop here on DataForm and get each tmpname??
{
console.log( $_FILES['name']['tmp_name']);
}
uj5u.com熱心網友回復:
Foreach 在 php 中看起來像這樣
PHP:foreach
這就是 $_FILES 的樣子
PHP: $_FILES
因此,在 Upload.php 中,您可以更改您的代碼
foreach(name in $_FILES['']) // how can I loop here on DataForm and get each tmpname??
{
console.log( $_FILES['name']['tmp_name']);
}
到
foreach($_FILES as $key => $file ) { // $key is index and $file is item
echo "this is ".$file['tmp_name']; // like console.log in js
}
現在您可以對 $_FILES 變數進行互動,這意味著將訪問每個 temp_name
uj5u.com熱心網友回復:
如果您要使用querySelectorAll查找所有檔案輸入元素,您可以遍歷它們并以比上面顯示的更簡潔的方式將名稱和值分配給 FormData 物件。
每個檔案都可以是appended需要使用array命名語法的變數- 即:files[]
使用該變數設定 Ajax 請求(fetch為簡單起見,此處使用),然后處理上傳的 PHP 代碼可以$_FILES使用files名稱迭代集合- 即:$_FILES['files']或您之前分配的任何變數fd.append('NAME[]',value)。
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES['files'] ) ){
$output=array();
foreach( $_FILES['files']['name'] as $i => $name ) {
if( !empty( $_FILES['files']['tmp_name'][$i] ) ) {
$name = $_FILES['files']['name'][$i];
$size = $_FILES['files']['size'][$i];
$type = $_FILES['files']['type'][$i];
$tmp = $_FILES['files']['tmp_name'][$i];
$error= $_FILES['files']['error'][$i];
$ext = pathinfo( $name, PATHINFO_EXTENSION );
# do what you need to save the files and generate some output to send back to the AJAX callback
$output[]=array(
'name'=>$name,
'size'=>$size,
'type'=>$type,
'ext'=>$ext,
'date'=>date(DATE_ATOM)
);
}
}
exit( json_encode($output) );
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title></title>
<style>input{clear:both;display:block;margin:0.5rem 0;padding:0.5rem;}</style>
</head>
<body>
<input type="file" name="Pay_cert1" />
<input type="file" name="Pay_cert2" />
<input type="file" name="Pay_cert3" />
<input type="file" name="Pay_cert4" />
<input type='button' value='Upload' />
<script>
document.querySelector('input[type="button"]').addEventListener('click',(e)=>{
let url=location.href; //'Upload.php';
let fd = new FormData();
document.querySelectorAll('input[type="file"][name^="Pay_cert"]').forEach(n => {
if ( n.files.length > 0 ) {
fd.append( 'files[]', n.files[0], n.name );
}
});
fetch( url, { method:'post', body:fd } )
.then( r=>r.json() )
.then( json=>{
console.log(json)
})
});
</script>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406633.html
標籤:
