一直在尝试上传图片,但无法正常工作。标题中的错误将显示在表单上。图像是通过“视图”中的文件插入并在“控制器”中检查的,但是,当涉及到最终上传时,由于权限的原因,它失败了。
在FileZilla中,我已经为视图中的文件和控制器赋予了正确的上传文件权限。
代码是: 表格-查看
<form class="article" id="article-form" name="article" method="post" enctype="multipart/form-data">
<ol>
<li>
<label for="heading">Heading</label> <span id="headingMessage"></span>
<input name="heading" id="heading" class="form-control" type="text" required>
</li>
<li>
<label for="topic">Topic</label> <span id="topicMessage" required></span>
<input name="topic" id="topic" class="form-control" type="text" list="football">
<datalist id="football">
<option value="Scotland"></option>
<option value="England"></option>
<option value="Spain"></option>
</datalist>
</li>
<li>
<label for="summary">Summary</label> <span id="summaryMessage"></span>
<input name="summary" id="summary" class="form-control" type="text">
</li>
<li>
<label for="thumbnail">Thumbnail Link</label> <span id="thumbnailMessage"></span>
<!-- <input name="thumbnail" id="thumbnail" class="form-control" type="text" required> -->
<input type="file" name="file" id="file">
</li>
<li>
<label for="video">Video</label> <span id="videoMessage"></span>
<input name="video" id="video" class="form-control" type="text">
</li>
<li>
<label for="articleText">Text</label>
<textarea name="articleText" id="articleText" class="md-textarea form-control" required></textarea>
</li>
<!-- <li>
<div class="g-recaptcha" data-sitekey="6LcUAnQUAAAAAPeF1u6Hcnf0Y5TfS4-0xitZ7ZeZ"></div>
</li> -->
</ol>
<input class="btn btn-success" id="formButton" type="submit" name="submit"value="Submit" name="submit">
<input class="btn btn-danger" id="formButton" type="reset" value="Reset">
</form>
控制器-
//check image
$target_dir = "../view/pictures";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 0;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 2;
//$thumbnail = '<img class="img-responsive" id="articleImage" src="'.$_POST["file"].'">';
echo $target_file;
} else {
echo "File is not an image. <hr>";
$uploadOk = 0;
}
}
$thumbnail=$target_file;
include("../model/api-article.php") ;
if($uploadOk>1)
{
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
$articleTxt = insertArticle($headline, $topic, $summary, $text, $thumbnail,$video,$date,$userId);
} else {
echo "Sorry, there was an error uploading your file.";
}
}else{
echo "Article not inserted - only upload images (PNG, JPEG)";
}
}
请原谅缩进
答案 0 :(得分:2)
仅授予上传文件权限可能不够-您需要允许整个文件夹上传图像。