【问题标题】:$this->upload->do_upload('userfile') not working properly$this->upload->do_upload('userfile') 无法正常工作
【发布时间】:2017-01-04 10:48:01
【问题描述】:

我的 upload_form.php 脚本

    <html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <!--<form action="" method="post">-->

            <?php echo $error; ?>

            <?php echo form_open_multipart('upload/do_upload');?>
            <input type="file" name="userfile" size="20" />
            <br><br>

            <input type="submit" value="upload"/>

       <?php 
       form_close();
       ?>

</body>

在控制器内上传.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->helper(['form', 'url']);
    }

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

    public function do_upload()
    {
        $config = [
            'upload_path'   => './uploads/',
            'allowed_types' => 'gif|jpg|png',
            'max_size'      => 100,
            'max_width'     => 1024, //Mainly goes with images only
            'max_heigth'    => 768,
        ];

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

        if (!$this->upload->do_upload('userfile')) {
            $error = ['error' => $this->upload->display_errors()];
            $this->load->view('upload_form', $error);
        } else {
            $data = ['upload_data' => $this->upload->data()];
            $this->load->view('upload_success', $data);
        }
    }
}

当没有文件选择时,它会给出正确的错误。但是在选择其他文件(文本或图像)时没有给出错误。只显示空白页

移动上传的文件正在工作。

上传成功

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<h3>Your file was successfully uploaded!</h3>

<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>

<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>

</body>
</html>

【问题讨论】:

  • 查看代码是什么upload_success.php
  • 移动上传的文件适用于上传文件夹,所以我认为这不是 777 权限问题。
  • 尝试将 'max_size' => 100、'max_width' => 1024、'max_heigth' => 768 的值更改为更多
  • 是的,也要检查该文件夹的权限
  • @ – Hikmat Sijapati 编辑

标签: php codeigniter


【解决方案1】:

尝试如下..

 public function do_upload()
{
    $config = [
        'upload_path'   => './uploads/',
        'allowed_types' => 'gif|jpg|png',
        'max_size'      => 100,
        'max_width'     => 1024, //Mainly goes with images only
        'max_heigth'    => 768,
    ];

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

    if (!$this->upload->do_upload('userfile')) {
        $error = ['error' => $this->upload->display_errors()];
        $this->load->view('upload_form', $error);
    } else {
          $data = array('upload_data' => $this->upload->data());
          $this->load->view('upload_success', $data);
    }
}

}

【讨论】:

  • dreamveaver 文件中显示错误并带有红色符号
  • 好的尝试编辑过的代码,如果视图在子文件夹中,则制作正确的路径
【解决方案2】:

检查此代码

if ($_FILES['inputname']['name'] != "") { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '10000'; $this->load->library('upload', $config); }

【讨论】:

    【解决方案3】:

    我也遇到过同样的问题,但是当我更改代码时

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

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

    然后我的代码工作正常。尝试一次它可能对你有帮助。谢谢。

    【讨论】:

      猜你喜欢
      • 2016-05-15
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      相关资源
      最近更新 更多