我正在开发一个基于简单项目的软件,用户可以通过上传一些文件和详细信息来添加项目。因此,我使用了具有4个不同命名文件输入的表单。用户可以上传任何一个。然后,我用4个不同的if语句检查下一页中的所有4个输入。但是问题是它只能与最后的if语句一起使用。没有错误,没有日志显示,并且第4个文件正在上传,并且数据已插入到数据库中,如第4条if语句中所述。
我检查了https://stackoverflow.com/questions/linked/2704314?sort=hot&page=1的相关问题,发现大部分问题都提到了Multiple file upload in php,但这对我来说没有帮助。我尝试了foreach循环进行多次上传,以解决问题。但是它在我的情况下不起作用,因为我需要重命名每个上传文件的Mysql特定表中的最后一个ID,然后再将该名称输入Mysql数据库。
这是我在表单中使用的代码:
echo'<form method="post" action="?page=add3&id='.$last_id.'" enctype="multipart/form-data"><div class="form-group">
<label for="audio">Upload Audio Documents:</label>
<input type="file" class="form-control" id="audio" name="audio">
<label for="video">Upload Video Documents:</label>
<input type="file" class="form-control" id="video" name="video">
<label for="promote">Upload Promotion Documents:</label>
<input type="file" class="form-control" id="promote" name="promote">
<label for="sponser">Upload Sponser Documents:</label>
<input type="file" class="form-control" id="sponser" name="sponser">
</div><button type="submit" class="btn btn-primary">Submit</button> <br/><br/><br/>
</form>';
这是我在add3页面中使用的代码:
//////audio/////
if (($_FILES["audio"]["size"] > 0) && ($_FILES["audio"]["size"] < 10*MB))
{if ($_FILES["audio"]["error"] > 0) {echo "Erro: " . $_FILES["audio"]["error"] . "<br>";} else {
if (!file_exists("storage/document/" . $_FILES["audio"]["name"])) {
$sql = "SELECT id FROM document ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) { while($row = $result->fetch_assoc())
{
$nid= $row["id"];
$nid2 = $nid+1;
$temp = explode(".", $_FILES["audio"]["name"]);
$name = $_FILES["audio"]["name"];
$newfilename = $nid2 . '.' . end($temp);
move_uploaded_file($_FILES["audio"]["tmp_name"], "storage/document/" . $newfilename);
$sql = "INSERT INTO document (name, link, pid, cid) VALUES ('$name', '$newfilename', '$id', 1)";
if ($conn->query($sql) === TRUE) { echo "<center><h2>Audio Documents successfully Uploaded.</h2>"; }
else { echo "Error: " . $sql . "<br>" . $conn->error; }
}}}}}
//////audio end/////
//////video/////
if (($_FILES["video"]["size"] > 0) && ($_FILES["video"]["size"] < 10*MB))
{if ($_FILES["video"]["error"] > 0) {echo "Erro: " . $_FILES["video"]["error"] . "<br>";} else {
if (!file_exists("storage/document/" . $_FILES["video"]["name"])) {
$lid = $conn->insert_id;
$nid = $lid+1;
$temp = explode(".", $_FILES["video"]["name"]);
$name = $_FILES["video"]["name"];
$newfilename = $nid . '.' . end($temp);
move_uploaded_file($_FILES["video"]["tmp_name"], "storage/document/" . $newfilename);
$sql = "INSERT INTO document (name, link, pid, cid) VALUES ('$name', '$newfilename', '$id', 2)";
if ($conn->query($sql) === TRUE) { echo "<center><h2>video Documents successfully Uploaded.</h2>"; }
else { echo "Error: " . $sql . "<br>" . $conn->error; }
}}}
//////video end/////
/////promote///////
if (($_FILES["promote"]["size"] > 0) && ($_FILES["promote"]["size"] < 10*MB))
{if ($_FILES["promote"]["error"] > 0) {echo "Erro: " . $_FILES["promote"]["error"] . "<br>";} else {
if (!file_exists("storage/document/" . $_FILES["promote"]["name"])) {
$lid = $conn->insert_id;
$nid = $lid+1;
$temp = explode(".", $_FILES["promote"]["name"]);
$name = $_FILES["promote"]["name"];
$newfilename = $nid . '.' . end($temp);
move_uploaded_file($_FILES["promote"]["tmp_name"], "storage/document/" . $newfilename);
$sql = "INSERT INTO document (name, link, pid, cid) VALUES ('$name', '$newfilename', '$id', 3)";
if ($conn->query($sql) === TRUE) { echo "<center><h2>Promotion Documents successfully Uploaded.</h2>"; }
else { echo "Error: " . $sql . "<br>" . $conn->error; }
}}}
//////promote end/////
/////sponser///////
if (($_FILES["sponser"]["size"] > 0) && ($_FILES["sponser"]["size"] < 10*MB))
{if ($_FILES["sponser"]["error"] > 0) {echo "Erro: " . $_FILES["sponser"]["error"] . "<br>";} else {
if (!file_exists("storage/document/" . $_FILES["sponser"]["name"])) {
$lid = $conn->insert_id;
$nid = $lid+1;
$temp = explode(".", $_FILES["sponser"]["name"]);
$name = $_FILES["sponser"]["name"];
$newfilename = $nid . '.' . end($temp);
move_uploaded_file($_FILES["sponser"]["tmp_name"], "storage/document/" . $newfilename);
$sql = "INSERT INTO document (name, link, pid, cid) VALUES ('$name', '$newfilename', '$id', 4)";
if ($conn->query($sql) === TRUE) { echo "<center><h2>Sponser Documents successfully Uploaded.</h2>"; }
else { echo "Error: " . $sql . "<br>" . $conn->error; }
}}}
//////sponser end/////
我希望4个有效输入文件的输出是4个成功消息,但实际输出只是最后一个。