数据库连接保持良好。首先让我们看看代码:
if (isset($_POST['submit'])) {
# code...
$tenant_name = $_POST['name'];
$tenant_address = $_POST['address'];
$tenant_contact = $_POST['contact'];
$tenant_family = $_POST['family_member'];
$tenant_date = $_POST['month'].$_POST['day'].$_POST['year'];
// image
$file_name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
$target_path = "/var/www/html/records/admin/images/";
$target_file_path = $target_path.basename($_FILES['file']['name']);
if(move_uploaded_file($temp_name, $target_file_path)){
$sql = "INSERT INTO tenants (name, address, contact, no_of_family_member, started_date, image) VALUES ('$tenant_name', '$tenant_address', '$tenant_contact', '$tenant_family', '$tenant_date', '$file_name')";
$query = mysqli_query($con, $sql);
if ($query) {
# code...
echo "New Tenant Added";
}
else
echo "Data insertion error!";
}
else
echo "error uploading image";
}
见表格:
<form enctype="multipart/form-data" action="new_tenant_process.php" method="POST">
<table border="0">
<tr><td>Name</td><td><input type="text" name="name"></td></tr>
<tr><td>Permanent Address:</td><td><input type="text" name="address"></td></tr>
<tr><td>Contact No.:</td><td><input type="text" name="contact"></td></tr>
<tr><td>No of Famiy Member:</td><td><select name="family_member">
<option value="1">1</option>
</select></td></tr>
<tr><td>Started at:</td><td colspan="3"><select name="month">
<option value="jan">Jan</option>
<option value="feb">Feb</option>
</select>
<select name="day">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select name="year">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select></td>
</tr>
<tr><td>Upload a picture</td><td><input type="file" name="file"></td>
<tr><td colspan="2"><input type="submit" value="Add" name="submit"></td></tr>
</table>
</form>
当我运行这段代码时:
if(move_uploaded_file($ temp_name,$ target_file_path))
此条件部分失败,error uploading image
在浏览器中回显。我可能已经失去了轨道..帮助!!
答案 0 :(得分:1)
您需要在move_uploaded_file
条件中获得if (isset($_POST['submit']))
。
您希望在发送表单后移动文件,而不是更早。
if (isset($_POST['submit'])) {
...
if(move_uploaded_file($temp_name, $target_file_path)){
...
}
}
答案 1 :(得分:1)
确保您的文件名仅包含英文字符。
检查两次文件名和目标文件夹 - $temp_name
,$target_file_path
<?php
if (isset($_POST['submit'])) {
# code...
$tenant_name = $_POST['name'];
$tenant_address = $_POST['address'];
$tenant_contact = $_POST['contact'];
$tenant_family = $_POST['family_member'];
$tenant_date = $_POST['month'].$_POST['day'].$_POST['year'];
// image
$file_name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
// make sure you have something in $file_name
$target_path = "/var/www/html/records/admin/images/";
$target_file_path = $target_path.basename($_FILES['file']['name']);
}
if(move_uploaded_file($temp_name, $target_file_path)){
$sql = "INSERT INTO tenants (name, address, contact, no_of_family_member, started_date, image) VALUES ('$tenant_name', '$tenant_address', '$tenant_contact', '$tenant_family', '$tenant_date', '$file_name')";
$query = mysqli_query($con, $sql);
if ($query) {
# code...
echo "New Tenant Added";
} else {
echo "Data insertion error!";
}
} else {
echo "error uploading image";
}
答案 2 :(得分:0)
尝试更改图片文件夹权限:
chmod 755 /var/www/html/records/admin/images
答案 3 :(得分:0)
首先要确保您已将enctype="multipart/form-data"
添加到表单中。其次,您已设置$target_path wrong
。
尝试:
$target_path = "./images/";
if you are uploading from your admin folder.