Coder Social home page Coder Social logo

problem with uploading files about admin HOT 4 OPEN

maldicore avatar maldicore commented on August 15, 2024
problem with uploading files

from admin.

Comments (4)

lucienjarrett avatar lucienjarrett commented on August 15, 2024

I did this but its still not working for me

from admin.

afftee avatar afftee commented on August 15, 2024

BootstrapFormHelper.php - when creates a form don't sets enctype to "multipart/form-data", because of bug in create method.
Changing $Model->displayFieldTypes[$modelKey] to $Model->displayFieldTypes should help.
Here are the complete create method of BootstrapFormHelper class.

    public function create($model = null, $options = array())
    {
        if (empty($options['class'])) {
            $options['class'] = 'well';
        }

        $modelKey = $this->model();
        $Model = ClassRegistry::init($modelKey);

        if(!empty($Model->displayFieldTypes)){
            if(in_array('image', $Model->displayFieldTypes) ||   in_array('file',$Model->displayFieldTypes)){
                      $type_val = array(
                          'type' => 'file'
                      );
                      $options = array_merge($type_val, $options);
                }
        }
        return parent::create($model, $options);
    }

Don't forget to set models field type to image (or file) in Models class file.
Like that :

var $displayFieldTypes = array(
    'src_big' => 'image'
);

var $upLoads = array(
    'imgDir' => 'photos'
);

from admin.

Neiklot avatar Neiklot commented on August 15, 2024

hello,

This is not copying the image inside the correct folder, after a creation of new line, you can see (image not found)

from admin.

Neiklot avatar Neiklot commented on August 15, 2024

NOW is working for my,

I changed the AdminAppController.php with this:

/**********************************************************************************************************************

array( 'className' => 'Admin.BootstrapForm' ), 'Html', 'Session' => array( 'className' => 'Admin.BootstrapSession' ), 'Paginator' => array( 'className' => 'Admin.BootstrapPaginator' ) ); ``` // Check if they are logged in function authenticate() { // Check if the session variable User exists, redirect to loginform if not if(!$this->Session->check('User')) { $this->redirect(array('controller' => 'users', 'action' => 'login_form')); exit(); } } // Authenticate on every action, except the login form function afterFilter() { if( $this->action != 'login_form' ) { $this->authenticate(); } } /** * Scaffold * * @var string */ public $scaffold; /** * Pagination * * @var string */ public function beforeRender() { } /** * Before Filter * * @return void */ public function beforeFilter() { $modelClass = $this->modelClass; // $fieldKey = $this->field(); $Model = ClassRegistry::init($modelClass); $ignoreModelList = array(); if(isset($Model->ignoreModelList)){ $ignoreModelList = $Model->ignoreModelList; } $displayFieldTypes[$modelClass] = array(); if(isset($Model->displayFieldTypes)){ $displayFieldTypes[$modelClass] = $Model->displayFieldTypes; } // Get association model displayfield types foreach ($Model->hasMany as $key => $value) { $subModel = ClassRegistry::init($value['className']); $displayFieldTypes[$key] = $subModel->displayFieldTypes; } $ignoreFieldList = array(); if(isset($Model->ignoreFieldList)){ $ignoreFieldList = $Model->ignoreFieldList; } $adminSettings = array(); if(isset($Model->adminSettings)){ $adminSettings = $Model->adminSettings; } parent::beforeFilter(); $iconPath = APP.'Plugin'.DS.'Admin'.DS.'webroot'.DS.'img'.DS.'admin_icons'; $iconFolder = new Folder($iconPath); $iconsInFolder = $iconFolder->find('.*png', true); foreach ($iconsInFolder as $key => $value) { $iconsInFolder[$key] = str_replace('.png', '', $value); } $Folder = new Folder(APP . 'Model'); $files = $Folder->find('.*\.php', true); $navbar = array(); foreach ($files as $file) { if ($file !== 'AppModel.php') { $model = str_replace('.php', '', $file); $Model = ClassRegistry::init($model); $schema = $Model->schema(); if(!in_array($model, $ignoreModelList) && isset($schema)){ if(isset($Model->adminSettings['icon'])){ $icon_file = $iconPath.DS.$Model->adminSettings['icon'].'.png'; if(file_exists($icon_file)){ $icon = $Model->adminSettings['icon']; } else { $icon = $iconsInFolder[0]; array_shift($iconsInFolder); } } else { $icon = $iconsInFolder[0]; array_shift($iconsInFolder); } $controller = Inflector::tableize($model); $title = Inflector::pluralize($model); $navbar[] = array( 'title' => $title, 'url' => array( 'plugin' => 'admin', 'controller' => $controller, 'action' => 'index', ), 'icon' => $icon, ); } } } $request = $this->request; $data=$request['data']; if($data){ $Model = ClassRegistry::init($modelClass); $imgDir = $Model->upLoads['imgDir']; foreach ($request['data'][$modelClass] as $key => $value) { if(isset($displayFieldTypes[$modelClass][$key]) && $displayFieldTypes[$modelClass][$key] == 'file' || $displayFieldTypes[$modelClass][$key] == 'image'){ if(empty($value)){ unset($this->request->$request['data'][$modelClass][$displayFieldTypes[$modelClass]]); } else { $itemDir = ''; if(isset($Model->upLoads['itemDir']) && !empty($Model->upLoads['itemDir'])){ // set as a field value or as a string in model $upLoads variable $itemDir = $Model->upLoads['itemDir']; if(is_array($itemDir) && isset($itemDir['field'])) { $itemField = $itemDir['field']; if(isset($data[$modelClass][$itemField])){ $itemDir = $data[$modelClass][$itemField]; } } else { $itemDir = $Model->upLoads['itemDir']; } } $fileOK = $this->uploadFiles($imgDir, $value, $itemDir); if(array_key_exists('urls', $fileOK)) { // save the url in the form data $this->request->data[$modelClass][$key] = $fileOK['urls'][0]; } } } } } if(empty($ignoreFieldList)) $ignoreFieldList = array(); if(empty($displayFieldTypes)) $displayFieldTypes = array(); if(empty($ignoreModelList)) $ignoreModelList = array(); if(empty($adminSettings)) $adminSettings = array(); $this->set(compact('navbar','displayFieldTypes','ignoreFieldList', 'adminSettings')); } /** * uploads files * @return: * will return an array with the success of each file upload */ public function uploadFiles($folder=null, $formdata, $itemId = null) { // setup dir names absolute and relative if(isset($folder)){ $folder_url = WWW_ROOT.'img'.DS.$folder; $rel_url = 'img'.DS.$folder; } else { $folder_url = WWW_ROOT.'img'; $rel_url = 'img'; } // create the folder if it does not exist if(!is_dir($folder_url)) { mkdir($folder_url); } // if itemId is set create an item/sub folder if($itemId) { // set new absolute folder $folder_url = $folder_url.DS.$itemId; // set new relative folder $rel_url = $rel_url.DS.$itemId; // create the folder if it does not exist if(!is_dir($folder_url)) { mkdir($folder_url); } } // list of permitted file types $permitted = array('image/gif','image/jpeg','image/pjpeg','image/png'); // replace spaces with underscores // $filename = $formdata; // $filename = substr($filename,strpos($filename,'.')+1,strlen($filename)); $filename = $formdata['name']; $filename = str_replace(' ', '_', $filename); // assume filetype is false $typeOK = FALSE; // check filetype is ok foreach($permitted as $type) { if($type == $formdata['type']) { $typeOK = TRUE; break; } } if($typeOK) { if(!file_exists($folder_url.DS.$filename)) { // create full filename $full_url = $folder_url.DS.$filename; $url = $rel_url.DS.$filename; // upload the file $success = move_uploaded_file($formdata['tmp_name'], $full_url); } else { // create unique filename and upload file // ini_set('date.timezone', 'India/Maldives'); $now = date('Y-m-d-His'); $full_url = $folder_url.DS.$now.$filename; $url = $rel_url.DS.$now.$filename; $success = move_uploaded_file($formdata['tmp_name'], $url); } // if upload was successful if($success) { // save the url of the file $result['urls'][] = substr($url, 4); } else { $result['errors'][] = "Error uploaded $filename. Please try again."; } } elseif($formdata['error'] == 4) { // no file was selected for upload $result['nofiles'][] = "No file Selected"; } else { // unacceptable file type $result['errors'][] = "$filename cannot be uploaded. Acceptable file types: gif, jpg, png."; } return $result; } } ``` *////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// and the form.ctp in the line 40 BSForm->create($modelClass, array('type' => 'file')); ?>

I Hope this help you too,
Neiklot

from admin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.