cakePHP 3.0 uploading images

前端 未结 6 1284
北荒
北荒 2020-12-16 20:07

I want to upload images in my cakephp 3.0 app. But I get the error message:

Notice (8): Undefined index: Images [APP/Controller/ImagesController.php, line 55         


        
相关标签:
6条回答
  • 2020-12-16 20:37
    /*Path to Images folder*/
    $dir = WWW_ROOT . 'img' .DS. 'thumbnail';
    /*Explode the name and ext*/
                    $f = explode('.',$data['image']['name']);
                     $ext = '.'.end($f);
        /*Generate a Name in my case i use ID  & slug*/
                    $filename = strtolower($id."-".$slug);
    
         /*Associate the name to the extension  */
                    $image = $filename.$ext;
    
    
    /*Initialize you object and update you table in my case videos*/
                    $Videos->image = $image;     
                if ($this->Videos->save($Videos)) {
    /*Save image in the thumbnail folders and replace if exist */
                move_uploaded_file($data['image']['tmp_name'],$dir.DS.$filename.'_o'.$ext);
    
                unlink($dir.DS.$filename.'_o'.$ext);
                    } 
    
    0 讨论(0)
  • 2020-12-16 20:39

    In your view file, add like this, in my case Users/dashboard.ctp

    <div class="ChImg">
    <?php 
    echo $this->Form->create($particularRecord, ['enctype' => 'multipart/form-data']);
    echo $this->Form->input('upload', ['type' => 'file']);
    echo $this->Form->button('Update Details', ['class' => 'btn btn-lg btn-success1 btn-block padding-t-b-15']);
    echo $this->Form->end();       
    ?>
    </div>
    

    In your controller add like this, In my case UsersController

    if (!empty($this->request->data)) {
    if (!empty($this->request->data['upload']['name'])) {
    $file = $this->request->data['upload']; //put the data into a var for easy use
    
    $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
    $arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
    $setNewFileName = time() . "_" . rand(000000, 999999);
    
    //only process if the extension is valid
    if (in_array($ext, $arr_ext)) {
        //do the actual uploading of the file. First arg is the tmp name, second arg is 
        //where we are putting it
        move_uploaded_file($file['tmp_name'], WWW_ROOT . '/upload/avatar/' . $setNewFileName . '.' . $ext);
    
        //prepare the filename for database entry 
        $imageFileName = $setNewFileName . '.' . $ext;
        }
    }
    
    $getFormvalue = $this->Users->patchEntity($particularRecord, $this->request->data);
    
    if (!empty($this->request->data['upload']['name'])) {
                $getFormvalue->avatar = $imageFileName;
    }
    
    
    if ($this->Users->save($getFormvalue)) {
       $this->Flash->success('Your profile has been sucessfully updated.');
       return $this->redirect(['controller' => 'Users', 'action' => 'dashboard']);
       } else {
       $this->Flash->error('Records not be saved. Please, try again.');
       }
    }
    

    Before using this, create a folder in webroot named upload/avatar.

    Note: The input('Name Here'), is used in

    $this->request->data['upload']['name']
    

    you can print it if you want to see the array result.

    Its works like a charm in CakePHP 3.x

    0 讨论(0)
  • 2020-12-16 20:44

    Now that everyone makes advertisement for his plugins here, let me do this as well. I've checked the Uploadable behavior linked in the other question, it's pretty simple and half done it seems.

    If you want a complete solution that is made to scale on enterprise level check FileStorage out. It has some features I haven't seen in any other implementations yet like taking care of ensuring your won't run into file system limitations in the case you get really many files. It works together with Imagine to process the images. You can use each alone or in combination, this follows SoC.

    It is completely event based, you can change everything by implementing your own event listeners. It will require some intermediate level of experience with CakePHP.

    There is a quick start guide to see how easy it is to implement it. The following code is taken from it, it's a complete example, please see the quick start guide, it's more detailed.

    class Products extends Table {
        public function initialize() {
            parent::initialize();
            $this->hasMany('Images', [
                'className' => 'ProductImages',
                'foreignKey' => 'foreign_key',
                'conditions' => [
                    'Documents.model' => 'ProductImage'
                ]
            ]);
            $this->hasMany('Documents', [
                'className' => 'FileStorage.FileStorage',
                'foreignKey' => 'foreign_key',
                'conditions' => [
                    'Documents.model' => 'ProductDocument'
                ]
            ]);
        }
    }
    
    class ProductsController extends ApController {
        // Upload an image
        public function upload($productId = null) {
            if (!$this->request->is('get')) {
                if ($this->Products->Images->upload($productId, $this->request->data)) {
                    $this->Session->set(__('Upload successful!');
                }
            }
        }
    }
    
    class ProductImagesTable extends ImageStorageTable {
        public function uploadImage($productId, $data) {
            $data['adapter'] = 'Local';
            $data['model'] = 'ProductImage',
            $data['foreign_key'] = $productId;
            $entity = $this->newEntity($data);
            return $this->save($data);
        }
        public function uploadDocument($productId, $data) {
            $data['adapter'] = 'Local';
            $data['model'] = 'ProductDocument',
            $data['foreign_key'] = $productId;
            $entity = $this->newEntity($data);
            return $this->save($data);
        }
    }
    
    0 讨论(0)
  • 2020-12-16 20:44
    <?php
    namespace App\Controller\Component;
    
    use Cake\Controller\Component;
    use Cake\Controller\ComponentRegistry;
    use Cake\Network\Exception\InternalErrorException;
    use Cake\Utility\Text;
    
    /**
     * Upload component
     */
    class UploadRegCompanyComponent extends Component
    {
    
        public $max_files = 1;
    
    
        public function send( $data )
        {
            if ( !empty( $data ) ) 
            {
                if ( count( $data ) > $this->max_files ) 
                {
                    throw new InternalErrorException("Error Processing Request. Max number files accepted is {$this->max_files}", 1);
                }
    
                foreach ($data as $file) 
                {
                    $filename = $file['name'];
                    $file_tmp_name = $file['tmp_name'];
                    $dir = WWW_ROOT.'img'.DS.'uploads/reg_companies';
                    $allowed = array('png', 'jpg', 'jpeg');
                    if ( !in_array( substr( strrchr( $filename , '.') , 1 ) , $allowed) ) 
                    {
                        throw new InternalErrorException("Error Processing Request.", 1);       
                    }
                    elseif( is_uploaded_file( $file_tmp_name ) )
                    {
                        move_uploaded_file($file_tmp_name, $dir.DS.Text::uuid().'-'.$filename);
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 20:55

    We're using https://github.com/josegonzalez/cakephp-upload with great success in our production app, and has done so for quite some time.

    Has awesome support for using "Flysystem" (https://flysystem.thephpleague.com/) as well - which is abstractions from specific file system(s) - so moving from normal local file system to S3 is a no-brainer, or Dropbox or whatever place you want :-)

    You can find related (high quality) plugins on file uploading right here: https://github.com/FriendsOfCake/awesome-cakephp#files - I've used "Proffer" with success as well, and it's by no means "almost done" or anything alike - both has all my recommendations and is in my eyes production ready!

    0 讨论(0)
  • 2020-12-16 20:56

    Maybe the following would help. It's a behavior who helps you to upload files very easy!

    http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/

    Let me know if you struggle.

    Greetz

    0 讨论(0)
提交回复
热议问题