目前我正在使用php上传文件到数据库。所以我成功上传了这个文件。在数据库中显示了文件的路径,但是我检查数据库中的文件是不存在的。
以下是数据库数据,您可以在圆圈部分看到路径正确。
在我的数据库下面没有任何文件
以下是我的代码,希望有人能帮我弄清楚我的错误部分在哪里。
submit.php
$ideaTopic = $_POST['ideaTopic'];
$ideaCategories = $_POST['ideaCategories'];
$ideaContent = $_POST['ideaContent'];
$ideaChooseFile = $_POST['ideaChooseFile'];
$anonymous = $_POST['anonymous'];
$checkbox_term = $_POST['checkbox_term'];
$getfile = $_FILES["uploadfile"]['name'];
$complete = true;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($checkbox_term != "accpected") {
$termIsNotCheck = true;
$complete = false;
}
else{
$termIsNotCheck = false;
}
if (empty($ideaTopic)) {
$isEmpty_topic = true;
$complete = false;
}
else{
$isEmpty_topic = false;
}
if (empty($ideaCategories)) {
$isEmpty_categories = true;
$complete = false;
}
else{
$isEmpty_categories = false;
}
if (empty($ideaContent)) {
$isEmpty_content = true;
$complete = false;
}
else{
$isEmpty_content = false;
}
//start here
if ($_FILES["uploadfile"]["type"] == "application/pdf" ||$_FILES["uploadfile"]["type"] == "application/zip" || $_FILES["photo"]["type"] == "application/msword" ||
$_FILES["photo"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" )
{
// echo"true";
if ($_FILES["uploadfile"]["error"] > 0) {
$complete = false;
$error_upload_file = true;
// echo "Return Code: " . $_FILES["image"]["error"] . "<br/><br/>";
} else {
if (file_exists("upload/" . $_FILES["uploadfile"]["name"])) {
$file_exist = true;
// echo $_FILES["image"]["name"] . " <span id='invalid'><b>already exists.</b></span> ";
}
else {
if (!is_dir("upload/" . $u_id . "/file/")) {
mkdir("upload/" . $u_id . "/file/");
}
else {
}
$sourcePath = $_FILES['uploadfile']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "upload/" . $u_id . "/file/" . $_FILES['uploadfile']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath, $targetPath); // Moving Uploaded file
// echo $targetPath;
if ($complete) {
if ($stmt = $conn->prepare("INSERT INTO idea (idea_topic,idea_categories,idea_content,idea_choose_file,idea_anonymous) VALUES (?, ?, ?, ?, ?)")) {
$stmt->bind_param("ssssi", $ideaTopic, $ideaCategories, $ideaContent, $targetPath, $anonymous);
$stmt->execute();
$stmt->close();
$submit_successful = true;
} else {
$submit_successful = false;
}
}
}
}
}
else{
// echo"not imgae";
}
$idea_status = array(
'submit-successful' => $submit_successful,
'isEmpty-topic' => $isEmpty_topic,
'isEmpty-categories' => $isEmpty_categories,
'isEmpty-content' => $isEmpty_content,
'isEmpty-choosefile' => $isEmpty_choosefile,
// 'isEmpty-closuredate' => $isEmpty_closuredate,
// 'isEmpty-finalclosure' => $isEmpty_finalclosure,
'termIsNotCheck' => $termIsNotCheck,
'error_upload_file' =>$error_upload_file,
'file_exist' => $file_exist
);
header('Content-Type: application/json');
echo json_encode($idea_status);
}
答案 0 :(得分:0)
通过移动文件,您必须使用绝对值而不是新文件的相对路径。
move_uploaded_file($sourcePath, __DIR__."/".$targetPath);