功能无法正确上传

Function not uploading correctly

我正在尝试将图像上传到在输入页面上定义并传递给函数内的 $path 变量的目录。现在它将图像上传到与上传脚本相同的目录,而不是定义的图像文件夹。我已经用几种方法重新工作了,结果总是一样的。 $path$this->destination 都是空的,图片上传到错误的地方。其他人能看到我做错了什么吗?

<?php
    //set the max upload size in bytes
    $max = 51200;
    if(isset($_POST['upload'])){
        //define path to the upload directory
        $destination = '../../../../image_folder/test/';
        require_once('upload.php');
        try {
            $upload = new Ps2_Upload("$destination");
            $upload->move();
            $result = $upload->getMessages();
        } catch (Exception $e) {
            echo $e->getMessage();
        } 
    }
?>
<!DOCTYPE html>
<html>
    <head>
    <body>
        <?php
            if (isset($result)) {
                echo '<ul>';
                foreach ($result as $message) {
                echo "<li>$message</li>";
            }
                echo '</ul>';
            }

            echo "the place the files will be put is $destination";
        ?> 
        <form action="" method="post" enctype="multipart/form-data" id="uploadImage">
            <label for="image">Upload New Image</label>
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max; ?>" />
            <input type="file" name="image" id="image" />
            <input type="submit" name="upload" id="upload" value="Upload" />
        </form>
    </body>
</html>

<?php
class Ps2_Upload {
    //protected variables
    protected $_uploaded = array();
    protected $_destination;
    protected $_max = 51200;
    protected $_messages = array();
    protected $_permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
    protected $_renamed = false;

    public function __construct($path){
        if(!is_dir($path) || !is_writable($path)){
            throw new Exception("$path must be a valid, writable directory.");
        }
        $this->_destination = $path;
        $this->_uploaded = $_FILES;
    }

    public function move(){
        $field = current($this->_uploaded);
        $success = move_uploaded_file($field['tmp_name'], $this->destination . $field['name']);
        if($success){
            $this->_messages[] = $field['name'] . " uploaded successfully to $this->destination";
        } else {
            $this->_messages[] = 'Could not upload ' . $field['name'];
        }
    }

    public function getMessages() {
        return $this->_messages;
    } 
}
?>
    $success = move_uploaded_file($field['tmp_name'], $this->destination . $field['name']);

应该是

    $success = move_uploaded_file($field['tmp_name'], $this->_destination . $field['name']);

请注意$this->destination 有错字,应该是$this->_destination

在你的构造函数中,你有这个:$this->_destination = $path;

在您的 move() 方法中,您有:$success = move_uploaded_file($field['tmp_name'], $this->destination . $field['name']);

您的受保护变量是 _destination,但您在 move() 方法中使用的是 destination。前面没有下划线。继续努力,可能会解决您的问题:

$success = move_uploaded_file($field['tmp_name'], $this->_destination . $field['name']);
  • 应该可以。