在Codeigniter中上载图像显示错误上载路径似乎无效

如何解决在Codeigniter中上载图像显示错误上载路径似乎无效

||
$config[\'upload_path\'] = site_path().\'photos/\';
$config[\'allowed_types\'] = \'gif|jpg|png|jpeg\';
$config[\'max_size\'] = \'2048\';
$this->load->library(\'upload\',$config);    
if ( ! $this->upload->do_upload())
{ 
    $this->data[\'alert\'] = $this->upload->display_errors();                     
    $this->load->view(\'profile/photo\',$this->data);
}   
else
{
    $upload_data = $this->upload->data();

    $filename = $upload_data[\'file_name\'];
    $width = $upload_data[\'image_width\'];
    $height = $upload_data[\'image_height\'];
    $config1 = array();
    $this->load->library(\'image_lib\');
    $config1[\'source_image\'] = site_path().\'photos/\'.$filename;


    $this->remove_existing_file($this->session->userdata(\'user_id\'));
    $this->Profile_model->savephoto(\'Edit\',$filename );
    redirect(\'/profile/photo\');
}
我收到此错误:   上载路径似乎无效。     

解决方法

        发生此错误的原因只有几个: 目录
site_path().\'photos/\'
不存在,请尝试运行
is_dir()
以确保它存在。 该目录存在,但不可写。确保在目录上设置了适当的权限。尝试运行
is_writable()
以确保。 您要使用的目录已存在,但尚未正确将其表示在Upload库中。尝试使用带有斜杠的绝对路径,类似于《用户指南》中的示例。 除此之外,我没有想到的任何解释。这是用于验证路径的CI代码(属于Upload类的一部分):
public function validate_upload_path()
{
    if ($this->upload_path == \'\')
    {
        $this->set_error(\'upload_no_filepath\');
        return FALSE;
    }

    if (function_exists(\'realpath\') AND @realpath($this->upload_path) !== FALSE)
    {
        $this->upload_path = str_replace(\"\\\\\",\"/\",realpath($this->upload_path));
    }

    // This is most likely the trigger for your error
    if ( ! @is_dir($this->upload_path))
    {
        $this->set_error(\'upload_no_filepath\');
        return FALSE;
    }

    if ( ! is_really_writable($this->upload_path))
    {
        $this->set_error(\'upload_not_writable\');
        return FALSE;
    }

    $this->upload_path = preg_replace(\"/(.+?)\\/*$/\",\"\\\\1/\",$this->upload_path);
    return TRUE;
}
更新: 根据您的评论,尝试使用此方法,让我们看看在进行下一步调试之前会发生什么:
$config[\'upload_path\'] = \'./community/photos/\';
    ,        
$config[\'upload_path\'] = \'./photos/\';
$config[\'allowed_types\'] = \'gif|jpg|jpeg|png\';
$config[\'max_size\'] = \'1000\';
$config[\'max_width\'] = \'1920\';
$config[\'max_height\'] = \'1280\';                     

$this->upload->initialize($config);
将此代码写在您的控制器块中 这个问题很常见,我知道您在__construct()块中编写了此代码,但是正确的方法是在调用上载代码的特定Controller中使用此代码。     ,        这应该有帮助 如果您需要在上传文件时自动创建以下目录:
./assets/2016-07-27/
,则必须扩展Upload库以自动创建目录(如果目录不存在)。 代码:
<?php
defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');

/**
 * File Uploading Class Extension
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Uploads
 * @author      Harrison Emmanuel (Eharry.me)
 * @link        https://www.eharry.me/blog/post/my-codeigniter-upload-extension/
 */
class MY_Upload extends CI_Upload {

    /**
     * Validate Upload Path
     *
     * Verifies that it is a valid upload path with proper permissions.
     *
     * @return  bool
     */
    public function validate_upload_path()
    {
        if ($this->upload_path === \'\')
        {
            $this->set_error(\'upload_no_filepath\',\'error\');
            return FALSE;
        }

        if (realpath($this->upload_path) !== FALSE)
        {
            $this->upload_path = str_replace(\'\\\\\',\'/\',realpath($this->upload_path));
        }

        if ( ! is_dir($this->upload_path))
        {
            // EDIT: make directory and try again
            if ( ! mkdir ($this->upload_path,0777,TRUE))
            {
                $this->set_error(\'upload_no_filepath\',\'error\');
                return FALSE;
            }
        }

        if ( ! is_really_writable($this->upload_path))
        {
            // EDIT: change directory mode
            if ( ! chmod($this->upload_path,0777))
            {
                $this->set_error(\'upload_not_writable\',\'error\');
                return FALSE;
            }
        }

        $this->upload_path = preg_replace(\'/(.+?)\\/*$/\',\'\\\\1/\',$this->upload_path);
        return TRUE;
    }
}
如何使用: 只需创建
application/libraries/MY_Upload.php
并将上面的代码粘贴到其中即可。 就这样! 更多信息: GitHub Gist。 我在Eharry.me上的博客文章 注意: 此扩展与CodeIgniter 3.x和2.x版本兼容。     ,        如果您在Windows上,请尝试写入\“ c:\\\”,然后查看是否可行。 还有site_path()的输出是什么,例如echo site_path();。 ? 在xammp上,我发现我需要写c:\\ xammp \\ htdocs \\ myproject \\ photos \\而不是仅使用\'\\ photos \\\';     ,        您好在服务器上,您可以连接filezilla并右键单击文件夹并更改文件属性     ,        检查您的.htaccess文件不阻止应用程序文件夹 检查上传的是CHMOD 777 使用var_dump(is_dir(\'/ photos / \'));看看您的目录是否存在! 最后尝试一下:
    $config[\'upload_path\'] = \'photos/\';
    $config[\'allowed_types\'] = \'gif|jpg|jpeg|png\';
    $config[\'max_size\'] = \'1000\';
    $config[\'max_width\'] = \'1920\';
    $config[\'max_height\'] = \'1280\';                     

    $this->upload->initialize($config);
    ,        我认为问题可以通过使用解决
$config[\'upload_path\'] =\'./photos/\';
代替
$config[\'upload_path\'] = site_path().\'photos/\';
    ,        我知道这已经得到回答。即使是我也很难尝试上传文件。但就我而言,问题是我在/ var / www以外的文件夹中有CodeIgniter应用程序,而www文件夹中有索引页(主要是出于安全措施)。 uploads文件夹应位于包含index.php文件的/ var / www文件夹中。这就是我要做的。接下来是将777权限授予上载文件夹。     ,        我在Windows上使用湿巾
$config[\'upload_path\'] = base_url().\'./resources/uploads/\';
根本不工作。
var_dump(is_dir($config[\'upload_path\'])); //return false
var_dump(is_writable($config[\'upload_path\']));  //return false
但是当我将其更改为
$config[\'upload_path\'] = \'./resources/uploads/\';   //works fine
它工作正常。所以我想在Windows上这不是权限问题,这是由base_url()方法引起的,这有多奇怪。     ,        在您的控制器中试用此代码
$config[\'upload_path\'] = \'images\'; //name of the uploading folder
$config[\'allowed_types\'] = \'jpeg|png|jpg\'; 
$config[\'file_name\'] = \'name_for_file\'; 

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

if (!$this->upload->do_upload())
 {
  echo $this->upload->display_errors();
  exit;
 }
else
 {
  $upload_data = $this->upload->data();
 } 
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-