Jump to content

How To Upload File For My Module , My Module Extends Moduleadmincontroller


Recommended Posts

i google about my question , here is almost related my question as below

 

--------------------------------------------------------------------------------------------------------------------------------------------------------

http://stackoverflow.com/questions/27205564/prestashop-helper-form-file-type

 

I assume this AdminController for your model. Now a model obviously can't hold a file in table column. What you can do is hold path to the uploaded file. That's what you can save.
 
You should look in AdminController class (which you extended). When you submit a form, one of two method are executed:
 
processAdd()
processUpdate()
Now investigate the flow logic in these methods. Other methods are called from within this methods, such as:
 
$this->beforeAdd($this->object); -> calls $this->_childValidation();
$this->validateRules();
$this->afterUpdate($object);
As you can see, there these are the methods where you can do you custom stuff. If you look up these functions in AdminController class, the're empty. They are purposely added so people can override them and put their custom logic there.
 
So, using these functions, you can validate your uploaded file fields (even though it isnt in the model itself), if it validates you can then assign path to the object; and then in beforeAdd method you can actually move the uploaded file to the desired location (because both child validation and default validation has passed).
 
The way I've done it:
 
protected function _childValidation()
{
   // Check upload errors, file type, writing permissions
   // Use $this->errors['file'] if there is an error;
 
protected function beforeAdd($object)
{
   // create filename and filepath
   // assign these fields to object;
 
protected function afterAdd($object)
{
   // move the file
If you allow the file field to be updated, you'll need to to these steps for Update methods as well.
----------------------------------------------------------------------------------------------------------------------------------------------
i think this got point, but the answer not written specific code yet, could anyone describe this??
many thanks
Link to comment
Share on other sites

i figured it out, the code as below 

in ../classes/controller/AdminController.php

Add->

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    protected function afterAdd($object)
    {
        //var_dump($this->object);
 
        if(is_uploaded_file($_FILES["imagenews"]["tmp_name"]))
        {
 
            $file_path1 = $_FILES["imagenews"]["name"];
            $file_path2 = $_FILES["videonews"]["name"];
 
            move_uploaded_file($_FILES["imagenews"]["tmp_name"], $file_path1);
            move_uploaded_file($_FILES["videonews"]["tmp_name"], $file_path2);
            //Db::getInstance()->Execute("INSERT INTO `"._DB_PREFIX_."newsmodule` (videonews, imagenews) VALUES ( '$file_path', '$file_path')");
            $tmp_id = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS("SELECT id_item FROM `"._DB_PREFIX_."newsmodule` ORDER BY id_item DESC LIMIT 1;");
            //var_dump($tmp_id[0]);
            //var_dump($tmp_id[0]['id_item']);
            Db::getInstance()->Execute("UPDATE `"._DB_PREFIX_."newsmodule` SET videonews = '".$file_path2."', imagenews  = '".$file_path1."' WHERE  id_item = ".$tmp_id[0]['id_item']);
 
        } else {
            echo("Upload failed! ");
        }
        
        //return true;
    }
--------------------------------------------------------------------------------------------------------------------------------------
Add->
 
    protected function beforeAdd($object)
    {
        //d(Tools::getValue($_FILES["imagenews"]));
        //var_dump($_FILES['imagenews']);
 
        if($_FILES["imagenews"]["size"] > 1024*3*1024)
        {
            echo ("File too large");
            exit;
        }
 
    }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ps. imagenews and videonews are variables, both are my <input .. name=imagenews.......> <input .. name=videonews.......>

 

if anyone could share better method , that will be very appreciated.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...