出于某种原因,我的脚本在上传文件时总是返回一个echo。 我的表格:
<form enctype="multipart/form-data" class='makePost' action="post.php" method="post">
<label for="title">Kies een titel voor uw post: </label>
<input type="text" name="title" id="title" placeholder="Titel" />
<label for="tags">Voeg tags toe aan uw post: </label>
<input type="text" name="tags" id="tags" placeholder="Tags - Gebruik een '-' om tags te onderscheiden." />
<label for="post">Maak hier uw post: </label>
<p><em>Gebruik **(text)** om tekst vetgedrukt te maken. Gebruik --(tekst)-- om tekst schuingedrukt te maken en gebruik ^^(titel)^^ voor titels en headers.</em></p>
<textarea name="post" rows="50" cols="90" placeholder="Maak hier uw post."></textarea>
<label for="editors">Kies anderen die uw post mogen bewerken: </label>
<select class="editors" name="editors" id="editors">
<?php
echo "<option value='1'>editName</option>";
echo "<option value='2'>editName</option>";
echo "<option value='3'>editName</option>";
echo "<option value='4'>editName</option>";
?>
</select>
<label for="thumbnail">Upload een thumbnail voor uw post: </label>
<input type="file" name="fileToUpload" id="fileToUpload" />
<label for="post">Post klaar? Gecheckt op fouten? Post hem nu! </label>
<input type="submit" value="Post" name="post" id="post" />
</form>
我的PHP(在同一个文件中):
<?PHP
if(isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] == true) {
if(isset($_SESSION['userType'])){
$userType = $_SESSION['userType'];
if($userType === 'editor' || $userType === 'admin') {
$error = 1;
if(isset($_POST['post'])) {
$title = htmlentities($_POST['title']);
$tags = htmlentities($_POST['tags']);
$post = htmlentities($_POST['post']);
$editors = htmlentities($_POST['editors']);
$author = htmlentities($_SESSION['userID']);
// $thumbnail = htmlentities($_POST['thumbnail']);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST['post'])){
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$log .= 'File is not an image. Please try again.<br/>';
$uploadOk = 0;
}
}
if(file_exists($target_file)) {
$log .= "Sorry, a file with this name already exists. Please try renaming the file.<br/>";
$uploadOk = 0;
}
if($_FILES["fileToUpload"]["size"] > 500000) {
$log .= "Sorry, your file is too large. We only support images up to 500kb.<br/>";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG"){
$log .= "Sorry we only support files of types .jpg, .jpeg and .png files<br/>";
$uploadOk = 0;
}
if($uploadOk == 0) {
$log .= 'Sorry, for some reason your file was not uploaded<br/>';
} else {
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$log .= "Photo uploaded!";
$thumbnail = $_FILES["fileToUpload"]["name"];
$error = 0;
$log .= $thumbnail."<br/>";
} else {
$log .= "Sorry there was an error while uploading<br/>";
}
}
if($error == 0) {
$postArr = $arrayName = array(
'title' => $title,
'tags' => $tags,
'post' => $post,
'author' => $author,
'editors' => $editors,
'thumbnail' => $thumbnail
);
$post = new Post(false, $postArr);
$post->sendPost();
}
}
$views = "views/post.php";
}
} else {
$log .= 'Unfortunately your account is not allowed to post, if you think you should be: contact an administrator.<br />';
}
} else {
$log .= 'Unfortunately your account is not allowed to post, if you think you should be: contact an administrator.<br />';
}
/*End logic code*/
include_once $template;
?>
所以我想要的是将输入的图像保存到我的目录中。返回的错误消息:
发布 文件不是图像。请再试一次。 抱歉,由于某种原因,您的文件未上传
提前致谢!
答案 0 :(得分:2)
根据getimagesize
的文件:
失败时,会返回 FALSE 。
在代码的这一部分中,您需要检查getimagesize
不返回false 。我认为您的意思是检查等于false ,而是因为您正在检查失败。
if(isset($_POST['post'])){
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) { // Here. Should be ===, not !==. Alternatively, `!$check`
$log .= 'File is not an image. Please try again.<br/>';
$uploadOk = 0;
}
}
答案 1 :(得分:0)
似乎你的检查与你想要的相反
你有
if($check !== false) {
但是,它应该是
if(!$check) {