CodeIgniter头像上传

2024-01-03

这是新的 HTML:

<input type="file" name="file_1" />
<input type="text" name="image_description_1" class="text-input"/>

这是新的 _submit 函数:

if($this->CI->input->post('file_1')){
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = 2000;
$config['upload_path'] = realpath(APPPATH . '../assets/uploads/avatars');

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

$image_data = $this->CI->upload->data();

$image['description'] = $this->CI->input->post('image_description_1');
$image['user_id'] = $id;
$image['image'] = $image_data['file_name'];

$this->CI->db->insert('report_images',$image);

}

描述和 user_id 已正确提交,但文件丢失。

我应该做一些不同的事情吗?不太确定出了什么问题。


我扩展了 Codeigniter 的上传类以完全满足您的需要。在这个类中我定义了两个方法validate_upload和do_upload。我没有在此文件中编写任何新代码,而是将 do_upload 代码分成两部分。 validate_upload 验证上传,如果文件未验证则返回 false,并且仅当 validate_upload 返回 true 时才应使用 do_upload。这是代码。

Class My_Upload extends CI_Upload
{   

    public function __construct(){
         parent::__construct();
    }   

    public function validate_upload($field = 'userfile')
    {

        // Is $_FILES[$field] set? If not, no reason to continue.
        if ( ! isset($_FILES[$field]))
        {
            $this->set_error('upload_no_file_selected');
            return FALSE;
        }

        // Is the upload path valid?
        if ( ! $this->validate_upload_path())
        {
            // errors will already be set by validate_upload_path() so just return FALSE
            return FALSE;
        }

        // Was the file able to be uploaded? If not, determine the reason why.
        if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
        {
            $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];

            switch($error)
            {
                case 1: // UPLOAD_ERR_INI_SIZE
                    $this->set_error('upload_file_exceeds_limit');
                    break;
                case 2: // UPLOAD_ERR_FORM_SIZE
                    $this->set_error('upload_file_exceeds_form_limit');
                    break;
                case 3: // UPLOAD_ERR_PARTIAL
                    $this->set_error('upload_file_partial');
                    break;
                case 4: // UPLOAD_ERR_NO_FILE
                    $this->set_error('upload_no_file_selected');
                    break;
                case 6: // UPLOAD_ERR_NO_TMP_DIR
                    $this->set_error('upload_no_temp_directory');
                    break;
                case 7: // UPLOAD_ERR_CANT_WRITE
                    $this->set_error('upload_unable_to_write_file');
                    break;
                case 8: // UPLOAD_ERR_EXTENSION
                    $this->set_error('upload_stopped_by_extension');
                    break;
                default :   $this->set_error('upload_no_file_selected');
                    break;
            }

            return FALSE;
        }


        // Set the uploaded data as class variables
        $this->file_temp        =   $_FILES[$field]['tmp_name'];
        $this->file_size        =   $_FILES[$field]['size'];
        $this->_file_mime_type($_FILES[$field]);
        $this->file_type        =   preg_replace("/^(.+?);.*$/", "\\1", $this->file_type);
        $this->file_type        =   strtolower(trim(stripslashes($this->file_type), '"'));
        $this->file_name        =   $this->_prep_filename($_FILES[$field]['name']);
        $this->file_ext     =   $this->get_extension($this->file_name);
        $this->client_name  =   $this->file_name;

        // Is the file type allowed to be uploaded?
        if ( ! $this->is_allowed_filetype())
        {
            $this->set_error('upload_invalid_filetype');
            return FALSE;
        }

        // if we're overriding, let's now make sure the new name and type is allowed
        if ($this->_file_name_override != '')
        {
            $this->file_name = $this->_prep_filename($this->_file_name_override);

            // If no extension was provided in the file_name config item, use the uploaded one
            if (strpos($this->_file_name_override, '.') === FALSE)
            {
                $this->file_name .= $this->file_ext;
            }

            // An extension was provided, lets have it!
            else
            {
                $this->file_ext  = $this->get_extension($this->_file_name_override);
            }

            if ( ! $this->is_allowed_filetype(TRUE))
            {
                $this->set_error('upload_invalid_filetype');
                return FALSE;
            }
        }

        // Convert the file size to kilobytes
        if ($this->file_size > 0)
        {
            $this->file_size = round($this->file_size/1024, 2);
        }

        // Is the file size within the allowed maximum?
        if ( ! $this->is_allowed_filesize())
        {
            $this->set_error('upload_invalid_filesize');
            return FALSE;
        }

        // Are the image dimensions within the allowed size?
        // Note: This can fail if the server has an open_basdir restriction.
        if ( ! $this->is_allowed_dimensions())
        {
            $this->set_error('upload_invalid_dimensions');
            return FALSE;
        }

        // Sanitize the file name for security
        $this->file_name = $this->clean_file_name($this->file_name);

        // Truncate the file name if it's too long
        if ($this->max_filename > 0)
        {
            $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
        }

        // Remove white spaces in the name
        if ($this->remove_spaces == TRUE)
        {
            $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
        }

        /*
         * Validate the file name
         * This function appends an number onto the end of
         * the file if one with the same name already exists.
         * If it returns false there was a problem.
         */
        $this->orig_name = $this->file_name;

        if ($this->overwrite == FALSE)
        {
            $this->file_name = $this->set_filename($this->upload_path, $this->file_name);

            if ($this->file_name === FALSE)
            {
                return FALSE;
            }
        }

        /*
         * Run the file through the XSS hacking filter
         * This helps prevent malicious code from being
         * embedded within a file.  Scripts can easily
         * be disguised as images or other file types.
         */
        if ($this->xss_clean)
        {
            if ($this->do_xss_clean() === FALSE)
            {
                $this->set_error('upload_unable_to_write_file');
                return FALSE;
            }
        }
        $this->set_image_properties($this->upload_path.$this->file_name);
        return TRUE;
    }

    public function do_upload($field = 'userfile')
    {
        /*
         * Move the file to the final destination
         * To deal with different server configurations
         * we'll attempt to use copy() first.  If that fails
         * we'll use move_uploaded_file().  One of the two should
         * reliably work in most environments
         */
        if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
        {
            if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
            {
                $this->set_error('upload_destination_error');
                return FALSE;
            }
        }

        /*
         * Set the finalized image dimensions
         * This sets the image width/height (assuming the
         * file was an image).  We use this information
         * in the "data" function.
         */
        return TRUE;    
    }
}   
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CodeIgniter头像上传 的相关文章

  • Codeigniter 分页:运行查询两次?

    我正在使用 codeigniter 和分页类 这是一个非常基本的问题 但我需要确保我没有遗漏任何东西 为了获得对从 MySQL 数据库获取结果进行分页所需的配置项 基本上需要运行查询两次 对吗 换句话说 您必须运行查询来确定记录总数 然后才
  • 使用控制器通过 codeigniter 处理返回的自定义 css 和 javascript 文件

    我正在开发一个 php codeigniter 项目 我正在考虑创建一个专门用于处理返回自定义 css 和 javascript 文件的控制器 在之前的项目中 我在视图文件的标头中包含了外部 CSS 和 JS 文件 但它们本质上必须是静态的
  • 如何将 .env 添加到 codeigniter?

    我尝试按照以下步骤使 php 连接到 Outlookhttps learn microsoft com en us outlook rest php tutorial https learn microsoft com en us outl
  • codeigniter,获取mysql表列中的最大值

    我正在使用 codeigniter 2 我有一个 mysql 表列 存储每个学生所用的时间 例如 1 2327 0 6547 1 9876 我想获得最大值 值该列 这是我的代码 this gt db gt select max time t
  • Codeigniter - 出现 404 Not Found 错误

    我们在 godaddy 有两个托管套餐 我们的实时网站使用以下 htaccess 文件运行良好 无需在 url 中使用 index php 即可访问网站 RewriteEngine On RewriteCond REQUEST FILENA
  • Codeigniter子域路由

    我正在尝试在 CodeIgniter 框架上运行的网站上设置博客脚本 我想在不对现有网站代码进行任何重大代码更改的情况下执行此操作 我认为创建一个指向另一个控制器的子域将是执行此操作的最干净的方法 我设置新设备所采取的步骤Blog涉及控制器
  • 定义根路径

    我正在寻找一种将配置变量定义为我的网站的根路径的方法 定义这个的最好方法是什么 我正在使用 Codeigniter config root path 这是我的 config 文件夹中的 site config 文件 dev dev appl
  • 在 Codeigniter 中从其他数据库切换动态数据库

    mi 文件 config php 是 active group default active record TRUE db master 是唯一的数据库 db master hostname localhost db master user
  • 控制器 HMVC 内的 CodeIgniter 负载控制器

    我在用着http github com philsturgeon codeigniter template http github com philsturgeon codeigniter template 对于模板 我尝试将其他控制器视图
  • Codeigniter $this->db->reconnect();用法

    I m not自动加载数据库 因为我的应用程序的大多数页面don t需要数据库处理 否则整个事情会变慢 我想要做的是 当数据库已经存在时 不要建立与数据库的新连接 而是使用它而不是打扰服务器数据库 那么我该如何实施 this gt db g
  • 在 Pyrocms 中管理登录重定向

    我需要以这样的方式管理登录 以便在成功登录后将控件重定向到调用pyrocms 中的登录方法的页面 默认情况下 它将控制权返回到主页 例如 我想要进入图库页面 但它要求用户登录 因此它将控件重定向到登录页面 现在我想在用户成功登录后将控件重定
  • 从数据库 MYSQL 和 Codeigniter 获取信息

    如果你们需要其他信息 上一个问题就在这里 从数据库中获取信息 https stackoverflow com questions 13336744 fetching information from the database 另一个更新 尽
  • 使用 CodeIgniter 加载视图文件夹外的视图

    我需要从以下范围之外加载视图 this gt load gt view 这似乎是从base application views目录 如何从外部访问视图 application 目录 我想我将不得不延长CI Loader class这是最好的
  • Codeigniter - CMS 的最佳路由配置?

    我想在 Codeigniter 中创建一个自定义 CMS 并且我需要一种将常规页面路由到默认控制器的机制 例如 mydomain com about mydomain com services maintenance 这些将通过我的页面处理
  • 如何让 CodeIgniter 接受“查询字符串”URL?

    根据 CI 的文档 CodeIgniter 使用基于分段的方法 例如 example com my group 如果我想找到一个特定的组 id 5 我可以访问 example com my group 5 并在控制器中定义 function
  • 无法使用 PHP mail() 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件

    我尝试使用 codeigniter 框架发送邮件 但它会引发错误 无法使用 PHP mail 发送电子邮件 您的服务器可能未配置为使用此方法发送邮件 From prakash t lt email protected cdn cgi l e
  • 从 MySQL 返回结果时的数字顺序

    我的数据库表中有以下类型的标题 Topic 1 blah blah Topic 2 blah blah Topic 3 blah blah Topic 10 blah blah Topic 11 blah blah etc 选择查询将始终返
  • Codeigniter查看和回显

    我有一个在 codeigniter 中处理网页侧栏的函数 如下 function process sidebar this gt load gt view first access 1 this gt load gt view second
  • Codeigniter:用户会话不断过期

    我正在使用 CodeIgniter 但在会话方面遇到了一个小问题 我已将 config php 中的 sess expiration 设置为 0 以便用户会话永远不会过期 但用户 甚至我自己 仍然偶尔会被踢出并要求再次登录 顺便说一句 我将
  • Drupal URL 重写冲突

    我已将 Drupal 7 安装在站点的根目录中 htaccess 文件自安装以来未曾修改过 不过 我还在子目录中设置了 CodeIgniter 我在 CI 目录中创建了一个 htaccess 文件 其中包含从 url 中删除 index p

随机推荐

  • 如何将 docker 与 drupal 和 drush 一起使用?

    我想用drush https github com drush ops drush 它需要在drupal容器中运行 还有一个drush docker 仓库 https hub docker com r drush drush 但我不知道如何
  • 防止电脑休眠

    我有一个正在播放某种媒体的应用程序 我不希望计算机在我的应用程序运行时进入睡眠状态 我查了一下 发现这个可以通过P Invoke来完成 显示器也不应该关闭 计算机也不应该进入睡眠状态 因此 我做了以下操作来测试这一点 b Click x y
  • Git 从现有远程分支添加工作树

    在我的远程存储库中有 3 个分支 主分支和 2 个长期运行的分支 master the common features are here like Core DAL north customized for A company long r
  • 将 PreviewKeyDown 中收到的密钥转换为字符串

    我在窗口上使用 PreviewKeyDown 事件来接收来自条形码扫描仪的所有键 KeyEventArgs 是一个枚举 没有给我实际的字符串 我不想使用 TextInput 因为某些键可能由控件本身处理 并且可能不会冒泡到 TextInpu
  • Spring Boot 应用程序中没有可用的合格 bean 类型

    运行我的 SpringBoot 应用程序时 出现以下错误 运行时出现异常 null IncationTargetException 创建名称为 bookController 的 bean 时出错 通过字段 bookRepository 表达
  • 提升::精神::气。如何将内联解析器表达式转换为独立语法,以及如何解压它们生成的元组?

    我正在使用 QI 和 Phoenix 我想编写一个返回 4 个布尔值的小语法 这些布尔值将用作语义操作内函数调用的参数 我有几个需要这些东西的函数 到目前为止我已经使用了这种方法 qi bool gt gt qi bool gt gt qi
  • 在 x86 上处理非常大的列表

    我需要处理大量浮点数 但在 x86 系统上遇到了内存限制 我不知道最终的长度 所以我需要使用可扩展类型 在 x64 系统上 我可以使用
  • 如何删除除每小时一条记录之外的所有记录

    我有一个包含数百万条传感器记录的 mysql 表 其结构如下 datanumber auto increment stationid int sensortype int measuredate datetime data medtext
  • TPL Dataflow,Post() 和 SendAsync() 之间的功能区别是什么?

    我对通过 Post 或 SendAsync 发送项目之间的区别感到困惑 我的理解是 在所有情况下 一旦一个项目到达数据块的输入缓冲区 控制权就会返回到调用上下文 对吗 那么为什么我需要 SendAsync 呢 如果我的假设不正确 那么我想知
  • 在 R 中使用 t.test() 时出错 - 没有足够的“x”观测值

    我尝试进行 t test 但它给了我这样的错误 在 R 中使用 t test 时出错 没有足够的 x 观察值 数据只有数值 没有 NA 组的比例是10比35 如何避免这种情况 先谢谢您的帮助 t test data Vrajdeb data
  • spring tx:advice和spring aop切入点的区别

    我是 Spring 新手 具有 Hibernate 的工作知识 我的工作是使用 Spring 声明式方法来实现事务 在 Google 的帮助下我成功完成了 感谢 Google 但无法清楚地理解我在 application context x
  • 如何正确配置 Julia 便携式或独立式

    如何正确配置Julia 便携式或独立式 https julialang s3 julialang org bin winnt x64 1 5 julia 1 5 0 win64 zip 对于外部存储 USB 驱动器发生的一切 添加 更新软件
  • libpcap 还是 PF_PACKET?

    我知道这个问题已经讨论过很多次了 我应该使用 libpcap 还是 PF PACKET 数据链路套接字 来捕获数据包 根据我的研究 几乎所有地方都建议使用 libpcap 而不是 PF PACKET 主要是因为它的可移植性 然而 对于我当前
  • 替换 Flutter 中的片段等小部件

    我是颤振新手 我有一个带有 2 个子小部件 Android 中的 2 个片段 的应用程序 当我单击 WidgetA 中的下一个按钮时 我想将该小部件替换 或推送 到 WidgetChildA 中 就像 Android 中的推送 或替换 片段
  • 什么开源消息队列软件可以提供严格排序的耐用性?

    我们需要的是实际上作为队列工作的 RabbitMQ并且不这样做 http www rabbitmq com faq html message ordering 消息应该保留在队列的头部 直到客户端明确地将它们出队 这似乎是一个非常简单的场景
  • Asp Net Core Web 推送通知

    主要目标是向站点添加发送 Web 通知的功能 以弹出系统通知 以使用 Html5 Push API 和服务工作人员提醒用户 不使用 SignalR 它只能在打开站点时运行客户端脚本 如前所述 还应该能够在网站关闭时发送通知here http
  • CMake - 在 Linux 中编译,在 Windows 中执行

    我有一个具有 Linux 依赖项的大型代码库 我想使用 CMake 将我的代码编译成可以在 Windows 上运行的可执行文件 即我希望 CMake 生成一个 exe 文件或类似性质的文件 我尝试过使用CMake网站上提供的解决方案 htt
  • Android Studio 本身不显示“数据库检查器”

    我使用的是4 2版本 这是Android Studio的最新版本 正如文档中所述 我在 视图 gt 工具窗口 中搜索了数据库检查器 但它没有出现在那里 我如何找到数据库检查器 任何帮助将不胜感激 Thanks in advance 我刚刚解
  • 为什么 doctests 在使用 Sphinx 的 `make doctest` 运行时会引发 NameError?

    我有一个简单的带有 doctest 的函数 http git io Tq2fTw 当与 Sphinx 一起运行时make doctest 给我以下错误 File scheemey rst line in default Failed exa
  • CodeIgniter头像上传

    这是新的 HTML