Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joomla 3.0 MVC file upload in custom component backend

SOLUTION: How to Save Uploaded File's Name on Database

this ended up helping me.

i am trying to add a file upload to a custom component using XML and database.

I know how to get file upload done in a static PHP environment but my knowledge about the PHP MVC structure in joomla makes it so I am unable to add it.

What I have done so far:

• Added the field in the XML file (of the type file)
• Added the form fields in admin view project
• Added an extra field My_project table(same as the image upload column)

Until this point it works.(fields are shown in admin backend component)

Now when you save the document with a file uploaded in the admin back end it does not save it to the database.

if i put media as field type then it works, but when i change it to file it breaks down.

XML file

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset>

  <field name="project_file" type="file"

            label="Upload file"
            description="Upload file"  
            directory="mysites" /> 


       <field name="main_image" type="media"

            label="COM_MYSITES_FORM_LBL_PROJECT_MAIN_IMAGE"
            description="COM_MYSITES_FORM_DESC_PROJECT_MAIN_IMAGE" 
            filter="raw"
            directory="mysites" /> 

 </fieldset>

PHP file upload script i normaly use

<?php
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

but what part goes in the model and what part goes in the controller? and how to call it.

entire view is called in the controller

class MysitesControllerProject extends JControllerForm {

    function __construct() {
        $this->view_list = 'projects';
        $jinput = JFactory::getApplication()->input;
        $files = $jinput->files->get('jform');
        $file = $files['project_file']; 
        $this->upload($file);
        parent::__construct();
    }

    public function upload($files)
    {
        $file_name = $files['name'];
        $src = $files['tmp_name'];
        $size = $files['size'];
        $upload_error = $files['error'];
        $type = $files['type'];
        $dest = "/home/vol3/byethost33.com/b33_13467508/bim-portfolio.cu.cc/htdocs/tmp";

        if (isset( $file_name)) {
            // Move the uploaded file.
            JFile::upload( $src, $filepath );
        }
    }

}
like image 269
user2617187 Avatar asked Jul 25 '13 11:07

user2617187


1 Answers

Placing new field in database and XML form is only half of work. You also have to write file save/upload functionality. There are two places you can do it. In controller (for example save task procedure) or model (there are 2-3 functions where you can do it). Look into this file /administrator/components/com_media/controllers/upload.php (upload procedure). I would just extend your save function so before data will be saved into database file will be stored on file system. You can find original save function declaration in /libraries/legacy/controller/legacy.php (for Joomla 3.0.1, for other versions it shouldn't be hard to find)

Here is sample save function:

public function save($key = null, $urlVar = null){
    // youre file upload code

    return parent::save($key = null, $urlVar = null)
}
like image 104
Artur Stępień Avatar answered Sep 17 '22 06:09

Artur Stępień