Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-OOP中的多文件上传方法_Php_Mysql_File Upload_Foreach - Fatal编程技术网

PHP-OOP中的多文件上传方法

PHP-OOP中的多文件上传方法,php,mysql,file-upload,foreach,Php,Mysql,File Upload,Foreach,我试图在PHP中构建一个方法,将多个文件上传到MySQL数据库中,并将文件名插入其中 我正试图基于PHP OOP来构建它 我面临着多个问题,我已经被这些问题困扰了两个多星期。 首先:该方法仅向数据库中插入一行,即使我在表单中选择了多个文件 Second:该方法没有获取文件名,因此即使只插入一行,也会插入一个空行 这是images类: 而var_dump显示了以下内容: 更多详细信息: 以下是images类扩展的主要类: <?php class Crud{ protected stati

我试图在PHP中构建一个方法,将多个文件上传到MySQL数据库中,并将文件名插入其中

我正试图基于PHP OOP来构建它

我面临着多个问题,我已经被这些问题困扰了两个多星期。

首先:该方法仅向数据库中插入一行,即使我在表单中选择了多个文件

Second:该方法没有获取文件名,因此即使只插入一行,也会插入一个空行

这是images类:

var_dump
显示了以下内容:

更多详细信息:

以下是images类扩展的主要类:

<?php
class Crud{
  protected static $db_table;
  public static function find(){
    return static::find_query("SELECT * FROM " . static::$db_table . "");
  }
  public static function find_limit($limit){
    return static::find_query("SELECT * FROM " . static::$db_table . " LIMIT " . $limit. "");
  }
  public static function find_id($id){
    global $database;
    $the_result_array = static::find_query("SELECT * FROM " . static::$db_table . " WHERE id='$id'");
    return !empty($the_result_array) ? array_shift($the_result_array) : false;
  }
  public static function find_query($sql){
    global $database;
    $set_result = $database->query($sql);
    $object_array = array();
    while($rows = mysqli_fetch_array($set_result)){
      $object_array[] = static::instantiation($rows);
    }
    return $object_array;
  }
  public static function instantiation($records){
    $calling_class = get_called_class();
    $object = new $calling_class;
    foreach ($records as $attribute => $value) {
      if($object->has_attribute($attribute)){
        $object->$attribute = $value;
      }
    }
    return $object;
  }
  private function has_attribute($attribute){
    $object_properties = get_object_vars($this);
    return array_key_exists($attribute, $object_properties);
  }
  protected function properties(){
    $properties = array();
    foreach (static::$table_fields as $field){
      if(property_exists($this, $field)){
        $properties[$field] = $this->$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 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->last_id();
      print_r($sql);
      var_dump($sql);
      return true;
    }else{
      return false;
    }
  }
}
 ?>
var\u dump
显示以下内容:

此外,这里是HTML页面:

<?php include_once "admin/head.php"; ?>
<?php if(!$session->is_signed_in()) {redirect("../index");}  ?>
<?php
 if(isset($_POST['submit'])){
   $images = new Images();
   $images->new_images();
 }
 ?>
<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-6">
      <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
        <div class="form-group">
          <label for="images" class="control-label">نص الصورة البديل</label>
          <input type="file" class="form-control" name="images[]" id="images" multiple="">
        </div>
        <input type="submit" name="submit" value="Submit" class="btn btn-primary">
      </form>
    </div>
  </div>
</div>
<?php include "admin/footer.php"; ?>

نص الصورة البديل

您通过
$\u文件['images']
的循环是错误的。其中的每个元素不是单个文件的所有属性。每个元素都是一个不同的属性,它包含所有上传的属性的数组。例如,
$\u文件['images']['name']
是所有名称的数组。因此,您需要遍历其中一个属性,然后获得其他属性的相应元素

foreach ($_FILES['images']['name'] as $index => $name) {
    $file = array('name' => $name, 'tmp_name' => $_FILES['images']['tmp_name'][$index]);
    $this->set_files($file);
    ...
}

这就是学习驾驶的人与有经验的人之间的区别,他们用在垃圾场找到的零件制造自己的汽车,而没有经验的人的任何输入,还有人在教练的指导下驾驶基本的汽车。一旦你学会了基本知识,你将有机会了解更多关于汽车机械工作原理的知识,并尝试更强大的汽车。代码也是如此:从适当的东西开始,探索它如何在内部工作,不要害怕尝试更高级的解决方案。你必须在技术堆栈中选择一个点,然后开始。从CPU中的单个原子到晶体管、电路、指令集,再到低级API、操作系统、脚本语言、框架,再到用它构建的应用程序,都是如此。这是一个巨大的传播。从一个框架开始,学习更多关于基础的知识,通过理解软件和软件工程原理,一路向下,一路向上。你的学习没有底部,也没有顶部。如果你是那种想要学习的人,那么无论你从哪里开始,都会有无限的机会学习更多。从一个框架开始的好处很简单:你可以从好的例子中学习,你可以得到框架社区的支持,你可以在更短的时间内完成更多的工作,因为你不必重新发明轮子来让任何东西工作。@tadman谢谢你,你真的引导我走上了一条更好的道路,你自己学习很难,在线教程和课程让你迷路了。它工作得很好,但只上传了一个文件,我不得不删除以下内容才能上传多个文件
$this->id=$database->last_id()
并返回true来自
Crud
类和
图像
类非常感谢您,如果您能向我解释为什么我必须删除上述语句才能上载多个文件,我将不胜感激。
<?php
class Crud{
  protected static $db_table;
  public static function find(){
    return static::find_query("SELECT * FROM " . static::$db_table . "");
  }
  public static function find_limit($limit){
    return static::find_query("SELECT * FROM " . static::$db_table . " LIMIT " . $limit. "");
  }
  public static function find_id($id){
    global $database;
    $the_result_array = static::find_query("SELECT * FROM " . static::$db_table . " WHERE id='$id'");
    return !empty($the_result_array) ? array_shift($the_result_array) : false;
  }
  public static function find_query($sql){
    global $database;
    $set_result = $database->query($sql);
    $object_array = array();
    while($rows = mysqli_fetch_array($set_result)){
      $object_array[] = static::instantiation($rows);
    }
    return $object_array;
  }
  public static function instantiation($records){
    $calling_class = get_called_class();
    $object = new $calling_class;
    foreach ($records as $attribute => $value) {
      if($object->has_attribute($attribute)){
        $object->$attribute = $value;
      }
    }
    return $object;
  }
  private function has_attribute($attribute){
    $object_properties = get_object_vars($this);
    return array_key_exists($attribute, $object_properties);
  }
  protected function properties(){
    $properties = array();
    foreach (static::$table_fields as $field){
      if(property_exists($this, $field)){
        $properties[$field] = $this->$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 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->last_id();
      print_r($sql);
      var_dump($sql);
      return true;
    }else{
      return false;
    }
  }
}
 ?>
INSERT INTO images(id,image_url,property_id,date)VALUES ('','','1','2017-10-20 20:24:05')
string(89) "INSERT INTO images(id,image_url,property_id,date)VALUES ('','','1','2017-10-20 20:24:05')"
<?php include_once "admin/head.php"; ?>
<?php if(!$session->is_signed_in()) {redirect("../index");}  ?>
<?php
 if(isset($_POST['submit'])){
   $images = new Images();
   $images->new_images();
 }
 ?>
<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-6">
      <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
        <div class="form-group">
          <label for="images" class="control-label">نص الصورة البديل</label>
          <input type="file" class="form-control" name="images[]" id="images" multiple="">
        </div>
        <input type="submit" name="submit" value="Submit" class="btn btn-primary">
      </form>
    </div>
  </div>
</div>
<?php include "admin/footer.php"; ?>
foreach ($_FILES['images']['name'] as $index => $name) {
    $file = array('name' => $name, 'tmp_name' => $_FILES['images']['tmp_name'][$index]);
    $this->set_files($file);
    ...
}