我想在项目文件夹中的根目录中上传并保存带有变量名的文件,下面这段代码是我试过的,但当我去那个文件夹时,那里没有文件
<?php
if (isset($_POST['submit_student'])){
$student_id=mysqli_real_escape_string($cnn,$_POST['student_id']);
$exam_id=mysqli_real_escape_string($cnn,$_POST['exam_id']);
echo " <form action='manage_add_result.php' method='POST' enctype='multipart/form-data'>";
echo " <input type='file' name='resultpdf'><br>";
echo " <input type='hidden' name='exam_id' value=\"{$exam_id}\">";
echo "<input type='hidden' name='student_id' value=\"{$student_id}\">";
echo "<input type='submit' name='upload_btn' value='upload'>";
echo "</form>";
}
?>
<?php
if (isset($_POST['upload_btn'])){
if(isset($_FILES['resultpdf']['name'])){
$newname=mysqli_real_escape_string($cnn,$_POST['student_id']) . "_".mysqli_real_escape_string($_POST['exam_id']) . ".pdf";
$target='karname/'."{$newname}";
move_uploaded_file( $_FILES['resultpdf']['tmp_name'] , $target);
}
}
?>
我想知道我该怎么做
答案 0 :(得分:2)
首先,根据此条件语句submit_student
if (isset($_POST['submit_student']))
的表单元素,这就是部分代码不起作用的原因
尝试以下方法:
<?php
if (isset($_POST['upload_btn'])){
$student_id=mysqli_real_escape_string($cnn,$_POST['student_id']);
$exam_id=mysqli_real_escape_string($cnn,$_POST['exam_id']);
if(isset($_FILES['resultpdf']['name'])){
$newname=mysqli_real_escape_string($cnn,$_POST['student_id']) . "_".mysqli_real_escape_string($_POST['exam_id']) . ".pdf";
$target='karname/'."{$newname}";
move_uploaded_file( $_FILES['resultpdf']['tmp_name'] , $target);
}
echo " <form action='' method='POST' enctype='multipart/form-data'>";
echo " <input type='file' name='resultpdf'><br>";
echo " <input type='hidden' name='exam_id' value=\"{$exam_id}\">";
echo "<input type='hidden' name='student_id' value=\"{$student_id}\">";
echo "<input type='submit' name='upload_btn' value='upload'>";
echo "</form>";
}
?>
另外,由于您正在执行同一文件中的所有内容,因此您可以执行action=""
而不是action='manage_add_result.php'
将error reporting添加到文件的顶部,这将有助于在生产测试期间。
error_reporting(E_ALL);
ini_set('display_errors', 1);