简体   繁体   English

代码点火器文件上传和文件写入

[英]Code igniter file upload and file write

I try to write code on page using with file uploader. 我尝试使用文件上传器在页面上编写代码。 The page generated must have the css code with the path name of image uploaded by user as background. 生成的页面必须具有css代码,并且用户上传的图像的路径名将作为背景。 I' using codeigniter file_write. 我正在使用codeigniter file_write。 I have inputs to get bg color and title too. 我也有输入以获得bg颜色和标题。 I have a layout.php page on view (with 777 permission) But, my code not work, after click on upload button, appers the error: "The upload path does not appear to be valid" 我在视图上有一个layout.php页面(具有777权限),但是,我的代码不起作用,单击“上载”按钮后出现错误:“上载路径似乎无效”

 class Upload extends CI_Controller {

    public function __construct()
        {
            parent::__construct();
            $this->load->helper(array('form', 'url', 'file', 'directory'));
        }

    public function index()
        {
            $this->load->view('upload_form', array('error' => ''));
        }

    public function do_upload()
        {

            $path = $this->input->post('new-folder');
            $title = $this->input->post("title");
            $bgcolor = $this->input->post("bgcolor");

            $upload_path_url = site_url().'uploads/'.$path.'/';

            $config['upload_path'] = './uploads/'.$path.'/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '2000';
            $config['max_width']  = '1920';

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload()) {
                $error = array('error' => $this->upload->display_errors('<p class="error">','<p>'));
                $this->load->view('upload_form', $error);

            } else {
                $data = $this->upload->data();

                //set the data for the json array   
                $info->name = $data['file_name'];
                $info->size = $data['file_size'];
                $info->type = $data['file_type'];
                $info->url = $upload_path_url .$data['file_name'];

                $info->thumbnail_url = $upload_path_url .$data['file_name'];
                $info->delete_url = base_url().'upload/deleteImage/'.$data['file_name'];
                $info->delete_type = 'DELETE';

                //this is why we put this in the constants to pass only json data
                if (IS_AJAX) {
                    echo json_encode(array($info));

                // so that this will still work if javascript is not enabled
                } else {
                     echo $upload_path_url;

                    if(!is_dir($path))
                    {
                        mkdir($upload_path_url,0777,TRUE);

                    } 

                    $page_layout = $this->load->view('layout');
                    $write_data = '<div style="width:100%; height:100%; background: url(../uploads/'.$data['file_name'].')"></div></body></html>' ;

                    if ( ! write_file( $page_layout . $write_data, 'a+') )
                    {
                        $msg_file = '<label>Unable to generate the page</label>';
                    }
                    else
                    {
                        $msg_file = '<label>Layout Page was generated!</label>';
                    }


                    $file_data['msg_file'] = $msg_file;
                    $file_data['title'] = $title;
                    $file_data['bgcolor'] = $bgcolor;
                    $file_data['upload_data'] = $this->upload->data();
                    $this->load->view('upload_success', $file_data); 
                }
            }
        }
}

Part of form_upload.php: form_upload.php的一部分:

<!-- Site title -->
<label>Title:</label>
<input type="text" size="40" name="title" />
<br /><br />
<label>Background color:</label>
<input type='text' value='#fff' name='bgcolor' id='bg' size="10" />
<br /><br />

Part of upload-success.php: upload-success.php的一部分:

    if ( isset($msg_file) ) { echo $msg_file; }
    echo '<br /><br />';
    echo '<label>Site title: '. $title.'</label>';
    echo '<br />'; 
    //echo $write_data;
    echo '<br />'; 
    echo '<label>background-color: '. $bgcolor.'</label>';
    echo '<br />';

Message "The upload path does not appear to be valid" references to your $config['upload_path'] = './uploads/'.$path.'/' line. 消息“上载路径似乎无效”引用了$config['upload_path'] = './uploads/'.$path.'/' . $config['upload_path'] = './uploads/'.$path.'/'行。 It means the directory doesn't exsist and/or has bad permissions. 这意味着该目录不存在和/或具有错误的权限。 Looking at your code, $path comes via POST data from the form, and you're not creating that folder anywhere BEFORE you do upload. 查看您的代码,$ path来自表单的POST数据,在上载之前,您没有在任何位置创建该文件夹。 I don't think CI's Upload class creates an upload folder if the one doesn't exist. 我不认为CI的Upload类不会创建一个上传文件夹。 I've not tested that, but try someting like following: 我没有测试过,但是尝试如下所示的方法:

$config['upload_path'] = './uploads/'.$path.'/';
[...]

$this->load->library('upload', $config);

if (!empty($path) && !file_exists($config['upload_path'])) {
   if (!mkdir($config['upload_path'], 0777, true)) {
    die('Failed to create folders...');
   }
}

if ( ! $this->upload->do_upload()) {
[...]

I'm not talking about POST data check as this is another matter. 我不是在谈论POST数据检查,因为这是另一回事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM