PHP:在本地主机(apache)上上传图片

PHP: image upload on localhost(apache)

我已经为我的上传系统制作了一个class(文件:Uploader.class.php):

    <?php
class Uploader{
    private $filename;
    private $fileData;
    private $destination;

    public function __construct($key){
        $this->filename = $_FILES[$key]['name'];
        $this->fileData = $_FILES[$key]['tmp_name'];
        }
    public function saveIn($folder){
        $this->destination = $folder;
        }
    public function save(){
        //μεταβλητη για να τσεκαρω αν ο φακελος που θα γινει η αποθηκευση ειναι μπορει να γραφτει
        $folderIsWriteAble = is_writable($this->destination);
        if($folderIsWriteAble){
            $name = "this->destination/$this->filename";
            $succes = move_uploaded_file($this->fileData,$name);
            }else{
                trigger_error("cannot write to $this->destination");
                $succes = false;
                }
    return $succes;
    }
    }
?>

现在正在制作上传算法(文件:upload.php):

<?php

$newImageSubmitted = isset($_POST['new-image']);
if($newImageSubmitted){

    $output = upload();
    }else{

        $output = include_once "views/upload-form.php";
        }
return $output;

function upload(){

    include_once "classes/Uploader.class.php";


    $uploader = new Uploader("image-data");
    $uploader->saveIn("/views/img");
    $fileUploaded = $uploader->save();
    if($fileUploaded){
        $out = "new file uploaded";
        }else{
            $out = "something went wrong";
            }
     return $out;
    }
?>

和表格(文件名:upload-form.php):

    <?php

return"
<h1>Upload new jpg images</h1>
<form method='post' action='phppage.php?page=upload' enctype='multipart/form-data' >
<label>Find a jpg image to upload</label>
<input type='file' name='image-data' accept='image/jpeg' />
<input type='submit' value='upload' name='new-image' />
</form> ";

?>

我想将图像存储在文件中 在路径中:views/img 表格和算法保存在 views 目录中,所以当我点击上传时,我在第 22 行出现 output:Notice: cannot write to /views/img in /var/www/html/project3/classes/Uploader.class.php 并且:在项目目录中出现错误( 来自 if 语句)我已经授予 777 权限有人可以解释我为什么我不能上传图像吗?谢谢

$uploader->saveIn("/views/img");

应该是

$uploader->saveIn("views/img");

这是假设您实际上并没有尝试将图像存储在 /views/img

然后改变 $name = "this->destination/$this->filename";

$name = "$this->destination/$this->filename";

缺少 $