试试这个:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
move_uploaded_file($_FILES["Photo"]["tmp_name"],"images/".$_POST['Photo']);
$filters=array(
"FirstName",
"LastName",
"Photo",
"MobilePhone",
"MobilePhoneProvider",
"HomePhone",
"HomePhoneProvider",
"WorkPhone",
"WorkPhoneProvider"
);
$final=array();
foreach ($filters as $filter) {
$final[$filter]=$_POST[$filter]?$_POST[$filter]:"";
}
$final["PhoneNumber"]=array(
array(
"MobilePhone"=>$final["MobilePhone"],
"Provider"=>$final["MobilePhoneProvider"],
),
array(
"HomePhone"=>$final["HomePhone"],
"Provider"=>$final["HomePhoneProvider"],
),
array(
"WorkPhone"=>$final["WorkPhone"],
"Provider"=>$final["WorkPhoneProvider"],
)
);
$unsets=array(
"MobilePhone",
"MobilePhoneProvider",
"HomePhone",
"HomePhoneProvider",
"WorkPhone",
"WorkPhoneProvider"
);
foreach ($unsets as $unset) {
unset($final[$unset]);
}
echo json_encode($final);
exit;
}
?><!DOCTYPE html>
<html>
<head>
<title>Contact</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
First Name: <input type="text" name="FirstName"><br>
Last Name: <input type="text" name="LastName"><br>
Photo: <input type="file" name="Photo"><br>
Mobile Phone: <input type="tel" name="MobilePhone"><br>
Mobile Phone Provider: <input type="text" name="MobilePhoneProvider"><br>
Home Phone: <input type="tel" name="HomePhone"><br>
Home Phone Provider: <input type="text" name="HomePhoneProvider"><br>
Work Phone: <input type="tel" name="WorkPhone"><br>
Work Phone Provider: <input type="text" name="WorkPhoneProvider"><br>
<input type="submit">
</form>
</body>
</html>
这只是一个起点,您需要进行图像检查和其他各种检查以确保用户输入有效。(对于这个测试,您应该创建一个名为 的文件夹images
)