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

时间:2016-07-11 16:27:00

标签: php opencart

我正在开发一个网站,允许用户将图像上传到他们的帐户。并且允许用户填写表单并以访客身份上传图像,然后在他们尚未登录时被要求登录,然后保存文件并在之后更新数据库。

我顺便使用opencart,

处理表单的控制器函数是

            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');
                }

登录部分的控制器功能是

        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'));
        }

我用于上传图像并为SQL创建临时名称的模型函数是

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;
        }

     }
}}

目前,无需通过登录控制器功能即可上传图像,但如果在提交表单后要求用户登录,则无法上传。 我使用print_r($files)在move_uploaded_file函数之前检查文件的结构和内容,并获得两者相同的结果。

有人可以告诉我为什么吗?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您无法在其他脚本中调用move_uploaded_file,因为在第一个脚本退出时会删除上传的文件。

您需要做的是让第一个脚本将文件移动到永久文件,并将文件名放在会话变量中。然后,第二个脚本可以在登录后将该文件链接到数据库中的用户条目。

您可能希望有一个cron作业,定期删除尚未链接到任何帐户的旧文件。