Jump to content

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


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.

Edited by Tahhan

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']);
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.