简体   繁体   中英

Upload file Warning: move_uploaded_file(../view/pictureswhy.PNG): failed to open stream: Permission denied

Have been trying to upload an image and it is not working as expected. The error in the title is being displayed on the form. The image is being inserted via a file in the 'View' and checked in the 'Controller', however, when it comes to the final upload it fails, due to permissions I assume...

I have given both files in the view and controller the correct permissions for uploading files - in FileZilla. 在此处输入图片说明

The code is: FORM - View

 <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> 

The controller -

//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)";
}

}

Please excuse the indenting

仅授予上传文件权限还不够-您需要允许整个文件夹上传图像。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM