警告:move_uploaded_file(C:\ XAMPP \ htdocs中\ photo_gallery \ PUBLIC \影像\ powder.jpg):未能打开流:无效的参数在C:\ XAMPP \

问题描述:

<?php 
// If it's going to need the database, then it's 
// probably smart to require it before we start. 
require_once('database.php'); 

class Photograph extends DatabaseObject { 

    protected static $table_name="photographs"; 
    protected static $db_fields=array('id', 'filename', 'type', 'size', 'caption'); 
    public $id; 
    public $filename; 
    public $type; 
    public $size; 

public $caption; 

    private $temp_path; 
    protected $upload_dir="images"; 
    public $errors=array(); 

    protected $upload_errors = array(
     // http://www.php.net/manual/en/features.file-upload.errors.php 
     UPLOAD_ERR_OK    => "No errors.", 
     UPLOAD_ERR_INI_SIZE  => "Larger than upload_max_filesize.", 
     UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.", 
     UPLOAD_ERR_PARTIAL  => "Partial upload.", 
     UPLOAD_ERR_NO_FILE  => "No file.", 
     UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.", 
     UPLOAD_ERR_CANT_WRITE => "Can't write to disk.", 
     UPLOAD_ERR_EXTENSION => "File upload stopped by extension." 
    ); 

    // Pass in $_FILE(['uploaded_file']) as an argument 
    public function attach_file($file) { 
     // Perform error checking on the form parameters 
     if(!$file || empty($file) || !is_array($file)) { 
      // error: nothing uploaded or wrong argument usage 
      $this->errors[] = "No file was uploaded."; 
      return false; 
     } elseif($file['error'] != 0) { 
      // error: report what PHP says went wrong 
      $this->errors[] = $this->upload_errors[$file['error']]; 
      return false; 
     } else { 
      // Set object attributes to the form parameters. 
      $this->temp_path = $file['tmp_name']; 
      $this->filename = basename($file['name']); 
      $this->type  = $file['type']; 
      $this->size  = $file['size']; 
      // Don't worry about saving anything to the database yet. 
      return true; 

     } 
    } 

    public function save() { 
     // A new record won't have an id yet. 
     if(isset($this->id)) { 
      // Really just to update the caption 
      $this->update(); 
     } else { 
      // Make sure there are no errors 

      // Can't save if there are pre-existing errors 
      if(!empty($this->errors)) { return false; } 

      // Make sure the caption is not too long for the DB 
      if(strlen($this->caption) > 255) { 
       $this->errors[] = "The caption can only be 255 characters long."; 
       return false; 
      } 

      // Can't save without filename and temp location 
      if(empty($this->filename) || empty($this->temp_path)) { 
      $this->errors[] = "The file location was not available."; 
      return false; 
      } 

      // Determine the target_path 
      $target_path = SITE_ROOT .DS. 'public' .DS. $this->upload_dir .DS. $this->filename; 

      // Make sure a file doesn't already exist in the target location 
      if(file_exists($target_path)) { 
      $this->errors[] = "The file {$this->filename} already exists."; 
      return false; 
      } 

      // Attempt to move the file 
      if(move_uploaded_file($this->temp_path, $target_path)){ 
      // Success 
       // Save a corresponding entry to the database 
       if($this->create()) { 
        // We are done with temp_path, the file isn't there anymore 
        unset($this->temp_path); 
        return true; 
       } 
      } else { 
       // File was not moved. 
      $this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder."; 
      return false; 
      } 
     } 
    } 

    public function destroy() { 
     // First remove the database entry 
     if($this->delete()) { 
      // then remove the file 
      // Note that even though the database entry is gone, this object 
      // is still around (which lets us use $this->image_path()). 
      $target_path = SITE_ROOT.DS.'public'.DS.$this->image_path(); 
      return unlink($target_path) ? true : false; 
     } else { 
      // database delete failed 
      return false; 
     } 
    } 

    public function image_path() { 
     return $this->upload_dir.DS.$this->filename; 
    } 

    public function size_as_text() { 
     if($this->size < 1024) { 
      return "{$this->size} bytes"; 
     } elseif($this->size < 1048576) { 
      $size_kb = round($this->size/1024); 
      return "{$size_kb} KB"; 
     } else { 
      $size_mb = round($this->size/1048576, 1); 
      return "{$size_mb} MB"; 
     } 
    } 

    public function comments() { 
     return Comment::find_comments_on($this->id); 
    } 


    // Common Database Methods 
    public static function find_all() { 
     return self::find_by_sql("SELECT * FROM ".self::$table_name); 
    } 

    public static function find_by_id($id=0) { 
     global $database; 
    $result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE id=".$database->escape_value($id)." LIMIT 1"); 
     return !empty($result_array) ? array_shift($result_array) : false; 
    } 

    public static function find_by_sql($sql="") { 
    global $database; 
    $result_set = $database->query($sql); 
    $object_array = array(); 
    while ($row = $database->fetch_array($result_set)) { 
     $object_array[] = self::instantiate($row); 
    } 
    return $object_array; 
    } 

    public static function count_all() { 
     global $database; 
     $sql = "SELECT COUNT(*) FROM ".self::$table_name; 
    $result_set = $database->query($sql); 
     $row = $database->fetch_array($result_set); 
    return array_shift($row); 
    } 

    private static function instantiate($record) { 
     // Could check that $record exists and is an array 
    $object = new self; 
     // Simple, long-form approach: 
     // $object->id    = $record['id']; 
     // $object->username = $record['username']; 
     // $object->password = $record['password']; 
     // $object->first_name = $record['first_name']; 
     // $object->last_name = $record['last_name']; 

     // More dynamic, short-form approach: 
     foreach($record as $attribute=>$value){ 
      if($object->has_attribute($attribute)) { 
      $object->$attribute = $value; 
      } 
     } 
     return $object; 
    } 

    private function has_attribute($attribute) { 
     // We don't care about the value, we just want to know if the key exists 
     // Will return true or false 
     return array_key_exists($attribute, $this->attributes()); 
    } 

    protected function attributes() { 
     // return an array of attribute names and their values 
     $attributes = array(); 
     foreach(self::$db_fields as $field) { 
     if(property_exists($this, $field)) { 
      $attributes[$field] = $this->$field; 
     } 
     } 
     return $attributes; 
    } 

    protected function sanitized_attributes() { 
     global $database; 
     $clean_attributes = array(); 
     // sanitize the values before submitting 
     // Note: does not alter the actual value of each attribute 
     foreach($this->attributes() as $key => $value){ 
     $clean_attributes[$key] = $database->escape_value($value); 
     } 
     return $clean_attributes; 
    } 

    // replaced with a custom save() 
    // public function save() { 
    // // A new record won't have an id yet. 
    // return isset($this->id) ? $this->update() : $this->create(); 
    // } 

    public function create() { 
     global $database; 
     // Don't forget your SQL syntax and good habits: 
     // - INSERT INTO table (key, key) VALUES ('value', 'value') 
     // - single-quotes around all values 
     // - escape all values to prevent SQL injection 
     $attributes = $this->sanitized_attributes(); 
     $sql = "INSERT INTO ".self::$table_name." ("; 
     $sql .= join(", ", array_keys($attributes)); 
     $sql .= ") VALUES ('"; 
     $sql .= join("', '", array_values($attributes)); 
     $sql .= "')"; 
     if($database->query($sql)) { 
     $this->id = $database->insert_id(); 
     return true; 
     } else { 
     return false; 
     } 
    } 

    public function update() { 
     global $database; 
     // Don't forget your SQL syntax and good habits: 
     // - UPDATE table SET key='value', key='value' WHERE condition 
     // - single-quotes around all values 
     // - escape all values to prevent SQL injection 
     $attributes = $this->sanitized_attributes(); 
     $attribute_pairs = array(); 
     foreach($attributes as $key => $value) { 
      $attribute_pairs[] = "{$key}='{$value}'"; 
     } 
     $sql = "UPDATE ".self::$table_name." SET "; 
     $sql .= join(", ", $attribute_pairs); 
     $sql .= " WHERE id=". $database->escape_value($this->id); 
     $database->query($sql); 
     return ($database->affected_rows() == 1) ? true : false; 
    } 

    public function delete() { 
     global $database; 
     // Don't forget your SQL syntax and good habits: 
     // - DELETE FROM table WHERE condition LIMIT 1 
     // - escape all values to prevent SQL injection 
     // - use LIMIT 1 
     $sql = "DELETE FROM ".self::$table_name; 
     $sql .= " WHERE id=". $database->escape_value($this->id); 
     $sql .= " LIMIT 1"; 
     $database->query($sql); 
     return ($database->affected_rows() == 1) ? true : false; 

     // NB: After deleting, the instance of User still 
     // exists, even though the database entry does not. 
     // This can be useful, as in: 
     // echo $user->first_name . " was deleted"; 
     // but, for example, we can't call $user->update() 
     // after calling $user->delete(). 
    } 

} 

?> 

我得到的move_upload_file一些警告。我试图诊断问题,但我失败了。这是我得到的错误。警告:move_uploaded_file(C: XAMPP htdocs中 photo_gallery PUBLIC 影像 powder.jpg):未能打开流:无效的参数在C: XAMPP

警告:move_uploaded_file(C:\ XAMPP \ htdocs中\ photo_gallery \公共\图像\ powder.jpg):未能打开流:在 Ç 无效参数:\ XAMPP \ htdocs中\ photo_gallery \包括\ photograph.php上线88

警告:move_uploaded_file()以:无法移动 'C:\ XAMPP \ TMP \ php46A4.tmp' 到'C:\ XAMPP \ htdocs中\ photo_gallery \公共\图像\粉末。 JPG”在 C:\ XAMPP \ htdocs中\ photo_gallery \包括在线路\ photograph.php 88

+1

因为我使用PHP很长一段时间,但仍然,你的路径(我不能正确引用它的上帝)似乎对我来说太过分了。 –

+1

您的文件上传路径不正确。 –

问题似乎是在SITE_ROOT变量,它应该有它

没有空格,因为它似乎,SITE_ROOT有这个价值

C: \ xampp \ htdocs\photo_gallery 

,但它应该有这个

C:\xampp\htdocs\photo_gallery 

如果你告诉我们你设置SITE_ROOT的代码,我们可以帮你解决这个问题

编辑(按照关于这个问题的评论)

你的错误是在这一行,你把这个问题

define('SITE_ROOT', " c:\ xampp\htdocs\photo_gallery"); 

应该没有空格的注释,这样

define('SITE_ROOT', "c:\xampp\htdocs\photo_gallery"); 
+0

这里是我的网站根目录定义('DS')? null:define('DS',DIRECTORY_SEPARATOR); defined('SITE_ROOT')? null: \t define('SITE_ROOT',“c:\ xampp \ htdocs \ photo_gallery”); define(“LIBRARY_PATH”,realpath(dirname(__ FILE__)。'/ library')); 但现在我收到错误说没有找到文件位置。 – tina

+0

OK,添加编辑到响应 – jperelli

+0

现在我得到了同样的错误,当我纠正网站root.:Warning:move_uploaded_file(C:MPP \ htdocs中\ photo_gallery \ PUBLIC \影像\ powder.jpg):未能打开流:用C无效参数:\ XAMPP \ htdocs中\ photo_gallery \包括上线\ photograph.php 89 – tina