PHP:在localhost(apache)上映像

时间:2015-01-21 22:56:16

标签: php file-upload

我为我的上传系统制作了一个课程(文件: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;
    }
    }
?>

现在我正在上传algorihtm(file: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;
    }
?>

和表单(filname: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行的/var/www/html/project3/classes/Uploader.class.php中的/ views / img 并且:出错了(女巫来自if语句)在项目目录中我已经给了777权限可以有人解释我为什么我不能上传图像?感谢

1 个答案:

答案 0 :(得分:1)

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

应该是

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

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

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

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

缺少$