我有一個動態創建的字段,其中一個是文件輸入字段。驗證多個動態創建的字段
對於非動態創建的輸入字段,我使用它來驗證服務器端:
if(isset ($_POST['submitform']))
{
$error = array();
foreach($_POST as $key => $value)
{
$data[$key] = filter($value);
}
if(empty($data['randominputfield']))
{
$error[] = "<p>Input something here</p>";
}
$target = "path/to/folder/";
$fileinput = ($_FILES['fileupload']['name']);
$filebasename = substr($fileinput, 0, strripos($fileinput, '.'));
$fileextension = substr($fileinput, strripos($fileinput, '.'));
$filesize = $_FILES["fileupload"]["size"];
$fileformats = array('.png','.jpeg','.jpg','.gif');
if (in_array($fileextension, $fileformats) && ($filesize <= 350000))
{
// Rename file
$fileinputnewname = md5($filebasename) . $fileextension;
if (file_exists("path/to/folder/" . $fileinputnewname))
{
$error[] = "<p>A file with this name already exists</p>";
}
else
{
$filemove = move_uploaded_file($_FILES["fileupload"]["tmp_name"], "path/to/folder" . $fileinputnewname);
}
}
elseif (empty($filebasename))
{
$error[] = "<p>Please select a file to upload</p>";
}
elseif ($filesize > 350000)
{
$error[] = "<p>Files should not exceed 350kB</p>";
}
else
{
$error[] = "<p>Only files with the extensions ". implode(', ',$fileformats)." are allowed</p>";
unlink($_FILES["fileupload"]["tmp_name"]);
}
if(empty($error))
{
//perform database insertion
}
}
和精美的作品。
但現在我想給用戶添加字段無限多的機會(我做到了這一點使用jQuery),這樣他們可以在上傳一組以上的數據(即隨機輸入和文件)同時,我想驗證,然後繼續將每組數據存儲在數據庫中。有任何想法嗎?
如此循環,雖然提交的文件\名 –
@Dagon一個例子,也許? – user3754923