普通网友 2016-07-11 16:27
浏览 31

move_uploaded_file不适用于会话中保存的文件而不显示任何错误消息

I am developing a site which allows users to upload images to their account. And the users are allowed to fill a form and upload images as a guest, and then be asked to login if they have not logged in yet, and then the files will be saved and update the database afterwards.

I am using opencart by the way,

The controller function to handle the form is

            if (!$this->customer->isLogged()) {
                //let's start the session
                if (!isset($_SESSION)) {
                    session_start();
                }

                $this->session->data['partner_front'] = $this->request->post;
                $this->session->data['files'] = $this->request->files;
                $this->session->data['redirect'] = $this->url->link('charity/partner&path=' . $project_id, '', 'SSL');
                $this->response->redirect($this->url->link('account/login', '', 'SSL'));
            } else {
                if(!isset($this->request->get['product_id'])){
                    $this->model_account_customerpartner->addProduct($this->request->post);
                    $this->session->data['success'] = $this->language->get('text_success');
                } else {
                    $this->model_account_customerpartner->editProduct($this->request->post);
                    $this->session->data['success'] = $this->language->get('text_success_update');
                }

The controller function in login part is

        if (isset($this->session->data['partner_front']) && $this->session->data['partner_front']) {
            $this->load->model('account/customerpartner');
            $this->model_account_customerpartner->addProduct($this->session->data['partner_front']);
            $this->session->data['success'] = $this->language->get('text_success');

            unset($this->session->data['partner_front']);
            unset($this->session->data['files']);
            $this->response->redirect($this->url->link('account/customerpartner/productlist', '', 'SSL'));
        }

And my model function which used to upload images and create temp name for the SQL is

public function addProduct($data){
$renamedImage = '';
$renamedOImage = array();
if(isset($this->session->data['files']) && $this->session->data['files']) {
    $files = $this->session->data['files'];
} else {
    $files = $this->request->files;
} print_r($files);

if (isset($files['image']['name']) AND $files['image']['name']) {
    if(count($files['image']['name']) == 1) {
        $renamedImage = rand(100000,999999) . basename(preg_replace('~[^\w\./\\\\]+~', '', $files['image']["name"][0]));
        move_uploaded_file($files["image"]["tmp_name"][0], DIR_IMAGE . MPIMAGEFOLDER .$renamedImage);
    } else {
        /**
         * upload product base image
         */
        $renamedImage = rand(100000,999999) . basename(preg_replace('~[^\w\./\\\\]+~', '', $files['image']["name"][0]));
        move_uploaded_file($files["image"]["tmp_name"][0], DIR_IMAGE . MPIMAGEFOLDER .$renamedImage);
        $renamedOImage[0] = $renamedImage;

        foreach (array_slice($files['image']['name'], 1) as $index => $product_image) {             

            $renamedImg = rand(100000,999999) . basename(preg_replace('~[^\w\./\\\\]+~', '', $product_image));
            //upload product images
            move_uploaded_file(array_slice($files['image']["tmp_name"], 1)[$index], DIR_IMAGE . MPIMAGEFOLDER .$renamedImg);

            $renamedOImage[$index] = $renamedImg;
        }

     }
}}

Right now, the image can be uploaded without going through the login controller function, but cannot be uploaded if the users are asked to login after submit the form. I used print_r($files) to check the structure and content of the files before move_uploaded_file function, and get the same results of both of them.

Could anyone tells me why? Thanks a lot for any helps.

  • 写回答

1条回答 默认 最新

  • dsb0003795 2016-07-11 16:37
    关注

    You can't call move_uploaded_file in a different script, because the uploaded file is deleted when the first script exits.

    What you need to do is have the first script move the file to a permanent file, and put the filename in the session variable. Then the second script can link that file into the user's entry in the database after they login.

    You might want to have a cron job that periodically deletes old files that haven't been linked to any accounts.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集