我正在为我的博客开发后端,但上传图片的代码并没有按预期工作。 当我提交它时上传图像并将其移动到指定的文件夹但在数据库中没有插入记录。这是用于上传的PHP
<?php
session_start();
include('Connections/conn.php');
if (!isset($_SESSION['userid'])) {
header("location:index.php");
}
$suc=" ";
$writer=$_SESSION['my_username'];
if(isset($_POST['submit']))
{
error_reporting(E_ALL ^ E_NOTICE);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
$title=$_POST['title'];
$intro=$_POST['intro'];
$body=$_POST['body'];
$keywords=$_POST['keywords'];
$date=$_POST['date'];
$fileToUpload=$_POST['fileToUpload'];
$sql2="Insert into articles(title,intro,body,keywords,date,writer,fileToUpload)VALUES('$title','$intro','$body','$keywords','$date','$writer','$target_file')"or die(mysqli_error());
$result2 = mysqli_query($db_conn, $sql2);
$suc=" <div class='alert alert-success'>
<span><b>Success</b>: New article posted successfully!</span>
</div>";
}
?>
这是表格
<form method="POST" action"<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<div class="form-group">
<label>Title</label>
<input type="text" id="title" name="title" class="form-control" required/>
</div>
<div class="form-group">
<label>Intro</label>
<textarea id="intro" name="intro" class="form-control" required></textarea>
</div>
<div class="form-group">
<label>Enter keyword tags</label><br/>
<div>
<input type="text" id="keywords" name="keywords" class="tagsinput" required/>
</div>
</div>
<div class="form-group">
<label>Date</label><br/>
<div>
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-calendar"></span></span>
<input type="text" id="date" name="date" class="form-control datepicker" placeholder="Select Date" required>
</div>
<span class="help-block">Click on input field to select date</span>
</div>
<div class="form-group">
<div>
<label>Picture</label>
<input type="file" multiple class="file" data-preview-file-type="any" name="fileToUpload" id="fileToUpload"/>
</div>
</div>
<div>
<div class="block">
<p>Type the content below, note that you can extend the height of the editor by dragging the bottom border.</p>
<textarea class="summernote" id="body" name="body" placeholder="Enter the body text" required>
</textarea>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<button class="btn btn-danger btn-block" type="reset">Reset</button>
</div>
<div class="col-md-6">
<button class="btn btn-info btn-block" type="submit" name="submit" value="submit">Publish Article</button>
</div>
</div>
</form>
答案 0 :(得分:0)
替换
$fileToUpload=$_POST['fileToUpload'];
与
$fileToUpload=$_FILES['fileToUpload']['name'];
答案 1 :(得分:0)
试试这个
$sql2="INSERT INTO articles(title,intro,body,keywords,date,writer,fileToUpload)VALUES('$title','$intro','$body','$keywords','$date','$writer','$target_file')");
if (!mysqli_query($db_conn, sql2))
{
echo("Error description: " . mysqli_error($db_conn));
}
else{
echo "Inserted";
}
和我的意见是在
move_uploaded_file
获得成功后添加插入代码