Jump to content

upload multiple files in PHP OOP and insert file names in MySql database


Tahhan

Recommended Posts


 

I am trying to upload multiple images to the server and at the same time insert the images info into a MySql database.

I am trying to achieve this using PHP OOP, I tried many ways using the foreach loop, but did not get any luck with it, any ideas on how can I solve this issue.

PHP images class code:

<?php
class Images extends Crud{
  protected static $db_table = "images";
  protected static $table_fields = array("id", "image_url", "property_id", "date");
  public $id;
  public $image_url;
  public $property_id;
  public $date;
  public $filename;
  public $tmp_path;
  public $upload_dir = "images";
  public $errors = array();
  public $upload_errors_array = array(
    UPLOAD_ERR_OK           => "There is no error.",
    UPLOAD_ERR_INI_SIZE     => "The file size exceeds the upload_max_filesize",
    UPLOAD_ERR_FORM_SIZE    => "The file upload exceeds the MAX_FILE_SIZE",
    UPLOAD_ERR_PARTIAL      => "The uploaded file was only partially uploaded",
    UPLOAD_ERR_NO_TMP_DIR   => "Missing a temporary folder",
    UPLOAD_ERR_CANT_WRITE   => "Failed to write file on desk",
    UPLOAD_ERR_EXTENSION    => "A PHP extension stopped the file upload"
  );

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

  public function set_files($file){
    if(empty($file) || !$file || !is_array($file)){
      $this->errors[] = "There was no file uploaded here";
      return false;
    }else{
        $this->image_url = $file['name'];
        $this->tmp_path = $file['tmp_name'];
      }
  }
  public function new_images(){
        $this->set_files($_FILES['images']);
        $this->property_id    = "1";
        $this->date           = date('Y-m-d H:i:s');
        $target_path = $this->upload_dir . DS . $this->image_url;
        move_uploaded_file($this->tmp_path, $target_path);
        if($this->create()){
          return true;
        }
  }
}

 ?>

HTML form:

<?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">
          <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"; ?>

I want to be able to select more than 1 image at the same time, upload the images to the server, and insert each image info in a MySql database row.

any help or advice would be appreciated.

Link to comment
Share on other sites

You receive an array in set_files but you doesn't loop the array when setting the image_url and the tmp_path

public function set_files($file){
    // $file should be an array
    if(empty($file) || !$file || !is_array($file)){
      $this->errors[] = "There was no file uploaded here";
      return false;
    }else{

        // $file is an array of files. You should loop the array before getting the file's name and tmp_name
        // $this->image_url = $file['name'];
        // $this->tmp_path = $file['tmp_name'];

        // you should do something like this
        foreach($file as $f) {
            $newFile = new stdClass();
            $newFile->image_url = $f['name'];
            $newFile->tmp_path = $f['tmp_name'];
            $this->files[] = $newFile; // put the file in an array and do stuff with it
        }
    }
}

public function new_images() {
    // $_FILES['images'] is an array of multiple files
    $this->set_files($_FILES['images']);
}
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.