如何使用move_uploaded_file函数连接正确的路径?



我对move_uploaded_file函数有问题。我不知道为什么它不起作用,也许是init.php路径有问题.我有几个相关的文件,所以我决定向您展示我的存储库: https://github.com/pchudzin/Learning-PHP-OOP/tree/master/PhotoGallerySystem 在这里你可以下载我的代码,还有数据库文件。

我的代码中有 2 条警告和错误消息("文件目录可能没有权限"(:

警告: move_uploaded_file(SITE_ROOT\admin\images\13-uklad-1.png(: 无法打开流:中没有此类文件或目录 C:\xampp\htdocs\kurs_OOP_PHP\Learning-PHP-OOP\PhotoGallerySystem\admin\include\photo.php 在第 62 行

警告:move_uploaded_file((: 无法移动 'C:\xampp\tmp\php1C1.tmp' to 'SITE_ROOT\admin\images\13-uklad-1.png' 在 C:\xampp\htdocs\kurs_OOP_PHP\Learning-PHP-OOP\PhotoGallerySystem\admin\include\photo.php 在第 62 行

图像文件夹的绝对路径:

C:\xampp\htdocs\kurs_OOP_PHP\Learning-PHP-OOP\PhotoGallerySystem\admin\images

图像文件夹具有读/写权限。

我将感谢您的帮助! 初始化.php

<?php
defined('DS') ? null : define( 'DS', DIRECTORY_SEPARATOR );
defined('DS') ? null : define( 'SITE_ROOT', 'C:' . DS . 'xampp' . DS . 'htdocs' . DS . 'kurs_OOP_PHP' . DS . 'Learning-PHP-OOP' . DS . 'PhotoGallerySystem' );
defined('INCLUDES_PATH') ? null : define('INCLUDES_PATH', 'SITE_ROOT' . DS . 'admin'. DS . 'includes');
require_once("functions.php");
require_once("new_config.php");
require_once("database.php");
require_once("db_object.php");
require_once("user.php");
require_once("photo.php");
require_once("session.php");
?>

照片.php

<?php
class Photo extends Db_object {
protected static $db_table = "photos";
protected static $db_table_fields = array('photo_id', 'title', 'description', 'filename', 'type', 'size');
public $photo_id;
public $title;
public $description;
public $filename;
public $type;
public $size;
public $tmp_path;
public $upload_directory = "images";
public $errors = array();
public $upload_errors_array = array(
UPLOAD_ERR_OK => "There is no error, the file uploaded with success.",
UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini.",
UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.",
UPLOAD_ERR_PARTIAL => "The uploaded file was only partially uploaded.",
UPLOAD_ERR_NO_FILE => "No file was uploaded.",
UPLOAD_ERR_NO_TMP_DIR => "Missing a temporary folder.",
UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk.",
UPLOAD_ERR_EXTENSION => "A PHP extension stopped the file upload."
);
// This is passing $_FILES['uploaded_file'] as an argument
public function set_file( $file ) {
if( empty( $file ) || !$file || !is_array( $file ) ) {
$this->errors[] = "There was no file uploaded here";
return false;
} else if( $file['error'] != 0 ) {
$this->errors[] = $this->upload_errors_array[ $file['error'] ];
return false;
} else {
$this->filename = basename( $file['name'] );
$this->tmp_path = $file['tmp_name'];
$this->type = $file['type'];
$this->size = $file['size'];
}
}
public function save() {
if( $this->photo_id ) {
$this->update();
} else {
if( !empty( $this->errors ) ) {
return false;
}
if( empty( $this->filename ) || empty( $this->tmp_path ) ) {
$this->erros[] = "the file was not available";
return false;
}
$target_path = 'SITE_ROOT' . DS . 'admin'. DS . $this->upload_directory . DS . $this->filename;
if ( file_exists( $target_path ) ) {
$this->errors = "The file {$this->filename} already exists";
return false;
}
if ( move_uploaded_file( $this->tmp_path, $target_path ) ) {
if( $this->create() ) {
unset( $this->tmp_path);
return true;
}
} else {
$this->errors[] = "the file directory probably does not have permission";
return false;
}
}
}
}
?>

上传.php

<?php include("includes/header.php"); ?>
<?php
if( !$session->is_signed_in() ) {
redirect( "login.php" );
}
?>
<?php
$message="";
if( isset( $_POST['submit'] ) ) {
$photo = new Photo();
$photo->title = $_POST['title'];
$photo->set_file( $_FILES['file_upload'] );
if( $photo->save() ) {
$message = "Photo uploaded succesfully";
} else {
$message = join("<br>", $photo->errors);
}
}
?>

<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<?php include("includes/top_nav.php"); ?>
<!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
<?php include("includes/side_nav.php"); ?>
<!-- /.navbar-collapse -->
</nav>

<div id="page-wrapper">
<div class="container-fluid">
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Upload
<small>Subheading</small>
</h1>
<div class="col-md-6">
<?php echo $message ?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="text" name="title" class="form-control">
</div>
<div class="form-group">
<input type="file" name="file_upload">
</div>
<input type="submit" name="submit">
</form>
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /#page-wrapper -->
<?php include("includes/footer.php"); ?>

db_object.php

<?php
class Db_object {
protected static $db_table = "users";
public static function find_all() {
return static::find_by_query("SELECT * FROM " . static::$db_table . " ");
}
public static function find_by_id( $user_id ) {
global $database;
$the_result_array=static::find_by_query("SELECT * FROM " . static::$db_table . " WHERE id=$user_id LIMIT 1");
return !empty( $the_result_array ) ? array_shift( $the_result_array ) : false;
}
public static function find_by_query( $sql ) {
global $database;
$result_set = $database->query( $sql );
$the_object_array = array();
while( $row = mysqli_fetch_array( $result_set ) ) {
$the_object_array[] = static::instantation($row);
}
return $the_object_array;
}
public static function instantation( $the_record ) {
$calling_class = get_called_class();
$the_object = new $calling_class;
foreach ($the_record as $the_attribute => $value) {
if( $the_object->has_the_attribute( $the_attribute ) ) {
$the_object->$the_attribute = $value;
}
}
return $the_object;
}
private function has_the_attribute( $the_attribute ) {
$object_properties = get_object_vars($this);
return array_key_exists( $the_attribute, $object_properties );
}
protected function properties() {
$properties = array();
foreach (static::$db_table_fields as $db_field) {
if( property_exists($this, $db_field) ) {
$properties[$db_field] = $this->$db_field;
}
}
return $properties;
}
protected function clean_properties() {
global $database;
$clean_properties = array();
foreach ($this->properties() as $key => $value) {
$clean_properties[$key] = $database->escape_string($value);
}
return $clean_properties;
}
public function save() {
return isset( $this->id ) ? $this->update() : $this->create();
}
public function create() {
global $database;
$properties = $this->clean_properties();
$sql = "INSERT INTO " . static::$db_table . "(" . implode(",", array_keys($properties)) . ")";
$sql .= "VALUES ('" . implode("','", array_values($properties)) . "')";
if( $database->query( $sql ) ) {
$this->id = $database->the_insert_id();
return true;
} else {
return false;
}
}
public function update() {
global $database;
$properties = $this->properties();
$properties_pairs = array();
foreach ($properties as $key => $value) {
$properties_pairs[] ="{$key}='{$value}'";
}
$sql = "UPDATE " . static::$db_table . " SET ";
$sql .= implode(", ", $properties_pairs);
$sql .= " WHERE id= " . $database->escape_string( $this->id );
$database->query( $sql );
return ( mysqli_affected_rows( $database->connection ) == 1 ) ? true : false;
}
public function delete() {
global $database;
$sql = "DELETE FROM " . static::$db_table . " ";
$sql .= "WHERE id=" . $database->escape_string( $this->id );
$sql .= " LIMIT 1";
$database->query( $sql );
return ( mysqli_affected_rows( $database->connection ) == 1 ) ? true : false;
}
}
?>

它看起来像这样一行:

$target_path = 'SITE_ROOT' . DS . 'admin'. DS . $this->upload_directory . DS . $this->filename;

当您打算使用常量SITE_ROOT时,正在使用文字字符串"SITE_ROOT"。所以我认为你的台词应该读:

$target_path = SITE_ROOT . DS . 'admin'. DS . $this->upload_directory . DS . $this->filename;

相关内容

  • 没有找到相关文章

最新更新