我正在尝试将图像上传到输入页面上定义的目录,并传递给函数中的$ 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;
}
}
?>
答案 0 :(得分:0)
$success = move_uploaded_file($field['tmp_name'], $this->destination . $field['name']);
应该是
$success = move_uploaded_file($field['tmp_name'], $this->_destination . $field['name']);
请注意,您在$ this-&gt;目的地中输入了一个拼写错误,它应该是$ this-&gt; _destination
答案 1 :(得分:0)
在构造函数中,您有:$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']);