codeigniter在单独的字段中上传2个文件(图像和视频)

时间:2019-01-08 09:06:02

标签: codeigniter

嗨,我是Codeigniter的新手,我想以不同领域的相同形式上传图像和视频。我做到了,但它要么存储。最后一个像视频一样存储。

<div id="file-upload" class="form-fields">
                            <div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Image</span></div>
                            <input id="html_btn" type="file" id="fileToUpload" name="fileToUpload" />
                        </div>

                        <div id="file-upload" class="form-fields">
                            <div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Video</span></div>
                            <input id="html_btn" type="file" id="videoToUpload" name="videoToUpload" />
                        </div>

我的控制器是

public function videoupdate() {
            $data['user_data'] = $this->session->userdata('user_logged_in');
            if (($this->session->flashdata('success')))
                $data['success'] = $this->session->flashdata('success');
            else
                $data['error'] = $this->session->flashdata('error');
            if (!empty($data['user_data']['user_id'])) {
            $title = $_POST['title'];
            $description = htmlentities($_POST['description']);
            $target_dir = './cms/uploads/blog/video3/';  
            $temp = explode('.', $_FILES['fileToUpload']['name']);          
            $video = explode('.', $_FILES['videoToUpload']['name']);                            
            if (!empty($_FILES['fileToUpload']['name'])) {              
                $newfilename = round(microtime(true)) . '.' . end($temp);
            } else {
                $newfilename = "";
            }                       
             if (!empty($_FILES['videoToUpload']['name'])) {                

                $videofilename = round(microtime(true)) . '.' . end($video);
            } else {
                $videofilename = "";
            }       

            move_uploaded_file($_FILES['fileToUpload']['tmp_name'], './cms/uploads/blog/video3/' . $newfilename);
            move_uploaded_file($_FILES['videoToUpload']['tmp_name'], './cms/uploads/blog/video3/' . $videofilename);
            $createddate = date('Y-m-d H:i:s');
            $ipaddress = $_SERVER['REMOTE_ADDR'];

            $status = $this->microblog_model->insertBlogvideo($title, $description, $newfilename, $videofilename, $data['user_data']['user_id'], $createddate, $ipaddress);

1 个答案:

答案 0 :(得分:0)

我发现您上传文件的代码运行正常。当我在单独的php文件中检查它时:

<pre>
                        <?php 
                        if(isset($_POST)){
                            $temp = explode('.', $_FILES['fileToUpload']['name']);          
                            $video = explode('.', $_FILES['videoToUpload']['name']); 
                                try{
                                    //upload image
                                    if (!empty($_FILES['fileToUpload']['name'])) {              
                                        $newfilename = round(microtime(true)) . '.' . end($temp);
                                        if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], './uploads/' . $newfilename)){
                                            echo "Image Uploaded<br/>";
                                        }
                                    }
                                    //upload video
                                    if (!empty($_FILES['videoToUpload']['name'])) {                
                                        $videofilename = round(microtime(true)) . '.' . end($video);
                                        if(move_uploaded_file($_FILES['videoToUpload']['tmp_name'], './uploads/' . $videofilename)){
                                            echo "Video Uploaded<br/>"; 
                                        }
                                    }     

                                }catch(Exception $e){
                                    print_r($e);
                                }
                            }
                        ?>
                        </pre>
                        <form method="post" enctype="multipart/form-data">
                            <div id="file-upload" class="form-fields">
                            <div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Image</span></div>
                            <input id="html_btn" type="file" id="fileToUpload" name="fileToUpload" />
                            </div>

                            <div id="file-upload" class="form-fields">
                            <div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Video</span></div>
                            <input id="html_btn" type="file" id="videoToUpload" name="videoToUpload" />
                        </div>
                        <input type="submit" name="submit">
                        </form>

您应检查要上传的视频的大小以及允许的最大上传大小。我希望这将有所帮助。因为我多次检查了很多图像和视频,发现它工作正常。