代码点火器上传图片

发布于 2024-10-17 08:01:41 字数 2245 浏览 3 评论 0原文

大家好,我正在使用一个管理系统,该系统可以使用图像创建新闻,但我无法找到如何将图像名称从我的模型文件发送到我的控制器,

这是我的模型文件:

function uploadImg()
{
    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' => 2000,
        'encrypt_name' => true
    );

    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    $config = array(
        'source_image'    => $image_data['full_path'],
        'new_image'       => $this->gallery_path . '/thumbs',
        'maintain_ration' => true,
        'width'           => 200,
        'height'          => 200,
        'encrypt_name'    => true,
        'max_size'        => 2000
    );

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


    # Ret profil billed navn #

    $file_array = $this->upload->data('file_name');
    return $billed_sti['billed_sti'] = $file_array['file_name'];

    //$this->db->where('username', $this->input->post('username'));
    //$this->db->update('users', $profilBilledNavn);
}

这是我的控制器:

function opret() {

    $this->form_validation->set_rules('overskrift', 'overskrift', 'required');
    $this->form_validation->set_rules('description', 'description', 'required');
    $this->form_validation->set_rules('indhold', 'indhold', 'required');

    if($this->form_validation->run() == true)
    {

        $this->load->model('admin/nyheder_model');
        $billed_sti = $this->nyheder_model->uploadImg();

        $data = array(
            'overskrift'  => $this->input->post('overskrift'),
            'description' => $this->input->post('description'),
            'indhold'     => $this->input->post('indhold'),
            'billed_sti'  => $billed_sti,
            'brugernavn'  => $this->session->userdata('username'),
            'godkendt'    => 'ja'
        );

        $this->db->insert('nyheder', $data); 

        redirect('admin/nyheder/index');
    } else {
        $this->index();
    }


}

Hello all im working on a admin system that can create news with a image but i cant find out how to send the image name from my model file to my controller,

this is my model file:

function uploadImg()
{
    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' => 2000,
        'encrypt_name' => true
    );

    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    $config = array(
        'source_image'    => $image_data['full_path'],
        'new_image'       => $this->gallery_path . '/thumbs',
        'maintain_ration' => true,
        'width'           => 200,
        'height'          => 200,
        'encrypt_name'    => true,
        'max_size'        => 2000
    );

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


    # Ret profil billed navn #

    $file_array = $this->upload->data('file_name');
    return $billed_sti['billed_sti'] = $file_array['file_name'];

    //$this->db->where('username', $this->input->post('username'));
    //$this->db->update('users', $profilBilledNavn);
}

This is my controller:

function opret() {

    $this->form_validation->set_rules('overskrift', 'overskrift', 'required');
    $this->form_validation->set_rules('description', 'description', 'required');
    $this->form_validation->set_rules('indhold', 'indhold', 'required');

    if($this->form_validation->run() == true)
    {

        $this->load->model('admin/nyheder_model');
        $billed_sti = $this->nyheder_model->uploadImg();

        $data = array(
            'overskrift'  => $this->input->post('overskrift'),
            'description' => $this->input->post('description'),
            'indhold'     => $this->input->post('indhold'),
            'billed_sti'  => $billed_sti,
            'brugernavn'  => $this->session->userdata('username'),
            'godkendt'    => 'ja'
        );

        $this->db->insert('nyheder', $data); 

        redirect('admin/nyheder/index');
    } else {
        $this->index();
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮世清欢 2024-10-24 08:01:41

我在控制器而不是模型中进行图像处理。

“模型是 PHP 类,旨在处理数据库中的信息。”

来自:http://codeigniter.com/user_guide/general/models.html

I do the image processing in the controller rather than the model.

"Models are PHP classes that are designed to work with information in your database."

from: http://codeigniter.com/user_guide/general/models.html

っ左 2024-10-24 08:01:41

您需要做的是将上传图像的代码移动到控制器。

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);
        }
    }

完成此操作后,

您可以从这一行中创建的 $data 变量中插入文件名:

$data = array('upload_data' => $this->upload->data());

您可以获得如下值:

$data['file_name']

该文件将上传您配置的文件夹,并且您将从控制器将文件名插入到数据库中。

我希望它有帮助。

What you need to do is move the code for uploading the image to the controler.

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);
        }
    }

Once you did that,

You can insert the name of the file from the $data variable created in this line:

$data = array('upload_data' => $this->upload->data());

and you can get the value like this:

$data['file_name']

The file will upload the the folder you configured, and you will insert the filename to the DB From the controller.

I hope it helps.

旧城烟雨 2024-10-24 08:01:41

请使用控制器中的上传功能,因为模型类用于处理数据库信息。请检查下面的代码

//Controller Class

function upload_image()
{
//Check for the submit
// Submit Name refers to the name attribute on the submit input tag.
// $filename refers to the name attribute of the file input tag.

if($_SERVER['REQUEST_METHOD'] == "POST")
{
$submit = $this->input->post('submit');
if($submit == "Submit Name")
{
//Load the relevant classes and libraries
$this->load->library('upload');
$this->load->model('admin/nyheder_model','nmodel');
$filename = "image_file";
//Define the config array
$config = array();
$config['upload_path'] = $this->gallery_path;
$config['allowed_types'] = "jpg|gif|png";
$config['max_size'] = 0; //0 is for no limit

$this->upload->initalize($config);

if(!$this->upload->do_upload("$filename"))
{
echo $this->upload->display_errors();
}
else
{
$file_data = $this->upload->data();
$filename_1 = $file_data['file_name'];
$insert_array = array('filename'=>"$filename_1");
$this->nmodel->insert_data($insert_array);
} // end of the else statement
} // end of the isset statement
} // end of the outer conditional statement

现在您已经在 $filename_1 变量中获得了文件名的值,您可以将其传递给模型类并将该值存储在数据库中。

谢谢
J

Please use the upload function in your controller as the model classes are used to handle the database information. Please check the code below

//Controller Class

function upload_image()
{
//Check for the submit
// Submit Name refers to the name attribute on the submit input tag.
// $filename refers to the name attribute of the file input tag.

if($_SERVER['REQUEST_METHOD'] == "POST")
{
$submit = $this->input->post('submit');
if($submit == "Submit Name")
{
//Load the relevant classes and libraries
$this->load->library('upload');
$this->load->model('admin/nyheder_model','nmodel');
$filename = "image_file";
//Define the config array
$config = array();
$config['upload_path'] = $this->gallery_path;
$config['allowed_types'] = "jpg|gif|png";
$config['max_size'] = 0; //0 is for no limit

$this->upload->initalize($config);

if(!$this->upload->do_upload("$filename"))
{
echo $this->upload->display_errors();
}
else
{
$file_data = $this->upload->data();
$filename_1 = $file_data['file_name'];
$insert_array = array('filename'=>"$filename_1");
$this->nmodel->insert_data($insert_array);
} // end of the else statement
} // end of the isset statement
} // end of the outer conditional statement

Now you have the value of the filename in the $filename_1 variable which you can pass to the model class and can store the value in the database.

Thanks
J

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文