Displaying the image uploaded with cakephp2

Asked 2 years ago, Updated 2 years ago, 57 views

I'm trying to implement the image upload function on a blog like the one I created in the tutorial.
I have created an image upload form on the input form.
I'd like to see it on the next confirmation screen, but it doesn't show up for some reason (it's chrome, but it only shows an emoji mark).

The way I'm thinking about it now is
■ Controller of article posting screen
If you do 1.post, fill in the session
2. Get tmp_name for $_file 3. Create temporary files in the img folder below app/webroot in tempnam() 4. Move the image uploaded with move_uploaded_file() to the temporary file you created. 5. Enter the directory of the temporary file you created in the session
■Confirmation screen controller
6. Get the name of the $_file name and directory of temporary files from session
7.$this->setto make it available on the view screen
■Verification Screen View
8.Get it from html->image

I thought I could do it like this, but
For some reason, I cannot create a directory under img for some reason./tmp It comes out underneath.
The permission is 777, but what other causes are there?

Please let me know if there is a better way.
(I am also studying, so I will not use plugin as much as possible)

Add: The controller source looks like this

>public function add(){
    if($this->request->is('post'))){
        $this->Post->set($this->request->data);
          if($this->Post->validates()){
            $this->request->data['Post']['user_id'] = $this->Auth->user('id');
            $this->Session->write('Session', $this->request->data);
            if(isset($this->request->data['Post']['image'])){
                $file_name = $this->request->data('Post.image.tmp_name');
                $tmpfile=tempnam("/var/www/html/share/cake/cakephp-2.6.7/app/webroot/img/tmp", "xxx");
                $this->Session->write('Session.xxx', $tmpfile);
                if(move_uploaded_file($file_name,$tmpfile)){
                    return$this->redirect(array('action'=>'confirm'));
                } else {$this->Session->setFlash(__(' file could not be saved to tmp.'));}
            }
          }
    }
}

public function confirm(){
    if($this->Session->check('Session')}
        $title=$this->Session->read('Session.Post.title');
        $body = $this->Session->read('Session.Post.body');
        // Image processing
        $file_name = $this->Session->read('Session.Post.image.name');
        if(isset($file_name)){
            // move_uploaded_file($file_name, $tmpfile);
            $image=$this->Session->read('Session.xxx');
            // mkdir($image,0777);
            $this->set('image',$image);
            }
        $this->set('title',$title);
        $this->set('body',$body);

php cakephp

2022-09-30 19:13

1 Answers

You can only write speculation because the source is not presented at all, but when you are trying to create a temporary file under app/webroot/img in tempnam(), did you specify a directory that does not already exist?Or did you specify a relative path?

tempnam() will go to the temporary directory on the system to create the file instead if the directory does not exist or if the directory does not exist but you do not have write permission.

PHP:tempnam-Manual

description
Creates a unique temporary filename in the directory that you specify with the permission set to 0600. If the specified directory does not exist or cannot be written to the directory, tempnam() generates a filename in the temporary directory of the system and returns the full path (including the name) to the file.

img If you want to place temporary files in a deeper hierarchy, you must have created a directory for that hierarchy beforehand.For Cake, Folder::create() is useful.


2022-09-30 19:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.