【发布时间】:2018-02-23 20:10:54
【问题描述】:
我有一个 php 表单,它在工作时会插入数据库、电子邮件通知,并允许可选地上传文档。 db & mail 部分有效,但文件上传无效。有一次,我确实在单独的页面上进行了文件上传。现在我有'error_reporting(-1);'在 php.ini 和 Apache 日志文件中没有显示任何内容来指示故障所在。我是否试图以相同的形式做太多事情?有没有办法获得更详细的错误报告以找出问题所在?
<?php
include('connect.php');
if((isset($_POST['username']) && !empty($_POST['username']))
&& (isset($_POST['email']) && !empty($_POST['email']))
&& (isset($_POST['city']) && !empty($_POST['city']))
&& (isset($_POST['state']) && !empty($_POST['state']))
&& (isset($_POST['file1']) && !empty($_POST['file1']))
&& (isset($_POST['file2']) && !empty($_POST['file2']))){
//if((isset($_POST['username']) && (!empty($_POST['username']))) && (isset($_POST['email']) && !empty($_POST['email'])) && (isset($_POST['subject']) && !empty($_POST['subject']))){
print_r($_POST);
$username = $_POST['username'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];
$name= $_FILES['file']['name'];
$tmp_name= $_FILES['file']['tmp_name'];
$submitbutton= $_POST['submit'];
$position= strpos($name, ".");
$fileextension= substr($name, $position + 1);
$fileextension= strtolower($fileextension);
$file1= $_POST['file1'];
$to = "someone@somewhere.com";
$headers = "From : " . $email;
if( mail($to, $username, $city, $headers)){
echo "<strong>E-Mail Sent successfully, we will get back to you soon.</strong>";
$query = "INSERT INTO `contact` (username, email, city, state, file1, file2) VALUES ('$username', '$email', '$city', '$state', '$zip', '$file1', '$file2')";
$result = mysqli_query($connection, $query);
if (isset($name)) {
$path= 'uploads/';
if (!empty($name)){
if (move_uploaded_file($tmp_name, $path.$name)) {
echo 'Uploaded!';
}
}
}
}
}
?>
<html><head></head><body>
<div class="container">
<form class="form-contact" method="POST" multipart/form-data>
<input type="name" name="username" id="inputusername" class="auto-style7" placeholder="Your Name" required><span class="auto-style6">
<input type="email" name="email" id="inputEmail" class="auto-style7" placeholder="Your E-Mail Address">
<input type="name" name="city" id="inputcity" class="auto-style7" placeholder="Your City">
<input type="name" name="state" id="inputstate" class="auto-style7" placeholder="Your State">
<p></p>
Select file1: <input type="file" name="file1">
Select file2: <input type="file" name="file2">
<br>
<button class="btn btn-lg btn-primary btn-block" type="submit">Send</button></form>
</div>
</body>
</html>
【问题讨论】:
-
顶部添加:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);,报错。 -
不应该说enctype='multipart/form-data'
-
旁注:检查字段时使用
isset和!empty是多余的。您只需要!empty检查,因为它会进行自己的 isset 检查。 -
输出$_FILES的内容,有什么吗?
-
如果您没有隐藏的输入,那么只需遍历 $_POST 并确保每个都不为空。有很多代码可以检查是否有空。此外,尝试简单地使用 HTML5 并将 required 添加到输入中,这样可以节省您的手指输入时间。