Codeigniter上传工作离线但不在线

时间:2012-11-19 06:17:11

标签: php codeigniter

您好我想在我的网站上为每位用户上传一张100 x 100像素的图片作为徽标。它适用于我的localhost,但无法在线工作(图像未上传)。好吧,这个概念很简单我只需上传它,然后根据它们的ID重命名图像,然后将它显示在页面上供他们查看。另一件事,localhost版本更改了指定路径上的图像,但在上传某些内容后不会显示它,但有时它可以正常工作。那么这不是一个大案例,我只是认为这可能是解决这个问题的线索。到目前为止,这是我的代码:

这是在顶部声明:

$data['base'] = $this->config->base_url();
$data['check_error'] = false;
$data['error_message'] = array();
$data['id'] = $this->session->userdata('id');

这是代码:

if (isset($_FILES['userfile']) && is_uploaded_file($_FILES['userfile']['tmp_name'])){
                    $new_name = $data['id'];

                $config['upload_path'] = './Logos/';
                $config['allowed_types'] = 'jpg|png|gif';
                $config['encrypt_name'] = true;
                $config['max_size'] = '100';
                $config['max_width']  = '100';
                $config['max_height']  = '100';

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

                    if ( ! $this->upload->do_upload())
                    {
                        array_push($data['error_message'], "You have the following errors in your entry:\n");
                        array_push($data['error_message'], "- Logo must be 100x100 pixels in size not exceeding 100KB with JPG,PNG or GIF format");
                        array_push($data['error_message'], "\nLogo upload failed.");
                        $data['check_error'] = true;
                    }
                    else
                    {
                        $data = array('upload_data' => $this->upload->data());

                        //DO UPDATE PART HERE
                        $file = $data['upload_data']['file_name'];
                        rename($config['upload_path'] . $file, $config['upload_path'] .$new_name.'.jpg');
                        //GO TO SETTINGS
                        $this->load->helper('url');  
                        redirect($data['base'].'settings');

                    }
                }

HTML:

 <tr>
    <td><p class="titles">Logo</p></td>
    <td>
    <div>
        <input type="text" id="fileName" class="file_input_textbox" readonly="readonly">

        <div class="file_input_div">
          <input type="button" value="Browse" class="file_input_button" />
          <input type="file" class="file_input_hidden" onchange="javascript: document.getElementById('fileName').value = this.value" id="upload" name="userfile" />
        </div>
    </div>
    </td>
</tr>

<tr>
<td>
</td>
<td>
<p class="titles">100px x 100px jpg, png or gif only.</p>
</td>
</tr>

<tr>
<td><p class="titles">Current Logo</p></td>
<td>
    <img src="<?php if(is_array(@getimagesize($base."Logos/".$id.".jpg"))){echo $base."Logos/".$id.".jpg";}else{echo $base."Logos/default.jpg";} ?>"  style="margin:0px 0px 0px 90px;"/>
</td>
</tr>

我还在本地使用CodeIgniter_2.1.3和5.4.3以及在线使用5.2.17。坚果哈哈!

1 个答案:

答案 0 :(得分:1)

您应该查看文件名部分。首先,您已将条件encrypt_name设置为true。无论如何,当您将文件重命名为id时,这不是必需的。 您将新文件名设置为等于

$data['id'].".jpg" 

如果上传的文件是GIF或PNG图像,这将永远不会有效......