Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在codeigniter中上载路径_Php_Codeigniter - Fatal编程技术网

Php 如何在codeigniter中上载路径

Php 如何在codeigniter中上载路径,php,codeigniter,Php,Codeigniter,我是新来的。目前我有以下几点: $config['upload_path'] = './uploads/'; 我只是想知道如何更新codeignitor中的路径。我已经尝试了下面的代码。我做错什么了吗 <?php defined('BASEPATH')`enter code here` OR exit('No direct script access allowed'); class Upload extends CI_Controller { func

我是新来的。目前我有以下几点:

$config['upload_path'] = './uploads/';
我只是想知道如何更新codeignitor中的路径。我已经尝试了下面的代码。我做错什么了吗

<?php
    defined('BASEPATH')`enter code here` OR exit('No direct script access allowed');
    class Upload extends CI_Controller {

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

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

        function do_upload()
        {
            $config['upload_path'] = './uploads/';

            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';

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

            if ( ! $this->upload->do_upload())
            {
                $error = array('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);
            }
        }
    }
    ?>

使用此库上载。。。它很容易使用

看法

组成部分

/**
* Description of uploader
*
* @author Rana
*/
class Uploader {
var $config;
public function __construct() {
$this->ci =& get_instance();
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/files/",
'upload_url' => base_url()."files/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
}
public function do_upload(){
$this->remove_dir($this->config["upload_path"], false);
$this->ci->load->library('upload', $this->config);
if($this->ci->upload->do_upload())
{
$this->ci->data['status']->message = "File Uploaded Successfully";
$this->ci->data['status']->success = TRUE;
$this->ci->data["uploaded_file"] = $this->ci->upload->data();
}
else
{
$this->ci->data['status']->message = $this->ci->upload->display_errors();
$this->ci->data['status']->success = FALSE;
}
}
function remove_dir($dir, $DeleteMe) {
if(!$dh = @opendir($dir)) return;
while (false !== ($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!@unlink($dir.'/'.$obj)) $this->remove_dir($dir.'/'.$obj, true);
}

closedir($dh);
if ($DeleteMe){
@rmdir($dir);
}
}
}

从您的代码中,您的文件将上载到根目录中的上载文件夹中<代码>$config['upload_path']='./uploads/'这是存储上传文件的地方。 您可以在应用程序文件夹所在的位置创建一个目录上传。 如果您需要当前代码

您需要为上载的图像设置一个目标文件夹。在CodeIgniter安装的根目录下创建一个名为uploads的文件夹,并将其文件权限设置为777。 默认情况下,上载例程希望文件来自名为userfile的表单字段。 从


问题不是celar..图像名称在哪里?$this->upload->do_upload('userfile')并上传以更改上传这里没有问题
/**
* the demo for file upload tutorial on codesamplez.com
* @return view
*/
public function file_upload_demo()
{
try
{
if($this->input->post("submit")){
$this->load->library("app/uploader");
$this->uploader->do_upload();
}
return $this->view();
}
catch(Exception $err)
{
log_message("error",$err->getMessage());
return show_error($err->getMessage());
}
}
/**
* Description of uploader
*
* @author Rana
*/
class Uploader {
var $config;
public function __construct() {
$this->ci =& get_instance();
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/files/",
'upload_url' => base_url()."files/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
}
public function do_upload(){
$this->remove_dir($this->config["upload_path"], false);
$this->ci->load->library('upload', $this->config);
if($this->ci->upload->do_upload())
{
$this->ci->data['status']->message = "File Uploaded Successfully";
$this->ci->data['status']->success = TRUE;
$this->ci->data["uploaded_file"] = $this->ci->upload->data();
}
else
{
$this->ci->data['status']->message = $this->ci->upload->display_errors();
$this->ci->data['status']->success = FALSE;
}
}
function remove_dir($dir, $DeleteMe) {
if(!$dh = @opendir($dir)) return;
while (false !== ($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!@unlink($dir.'/'.$obj)) $this->remove_dir($dir.'/'.$obj, true);
}

closedir($dh);
if ($DeleteMe){
@rmdir($dir);
}
}
}
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

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

    if ( ! $this->upload->do_upload('image name'))
    {
        $error = array('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);
    }