代码点击器文件上传 [英] Code Igniter File upload
问题描述
我要从两个独立的视图上传图片,因此在自动加载库下,我已经包括上传。
.php在配置。从这里我传递像大小,文件类型等参数。
if(??){
$ config [ 'upload_path'] = realpath('x');
$ config ['allowed_types'] ='gif';
$ config ['max_size'] ='1048';
$ config ['max_width'] ='1024';
$ config ['max_height'] ='768';
}
else {
$ config ['upload_path'] = realpath('x');
$ config ['allowed_types'] ='gif';
$ config ['max_size'] ='1048';
$ config ['max_width'] ='1024';
$ config ['max_height'] ='768';
}
现在有两个地方上传,我有不同的文件大小,类型参数,我的控制器如何知道要采取哪一个。如果{} else {]条件,我应该放什么。
让我知道,如果我不清楚。
您的代码必须执行以下操作:
(1)您需要设置一个标志是一个很好的方式)在每个上传表单,让你
知道哪个文件夹存储文件。让我们调用此标志 upload_target
在你的控制器中:
(2)从post变量中获取 upload_target
/ p>
(3)根据值 upload_target
,根据需要设置upload_config值
,然后加载/
upload_config
值在何处放置 如果您尝试在config / upload.php中定义一个二维数组,则库加载器可能会混淆。
而是定义一个名为 upload_settings.php
的配置文件并定义以下数组:
$ config ['target1'] ['upload_path'] ='./uploads_path_1/';
$ config ['target1'] ['allowed_types'] ='gif | jpg | png';
$ config ['target1'] ['max_size'] ='100';
$ config ['target1'] ['max_width'] ='1024';
$ config ['target1'] ['max_height'] ='768';
$ config ['target2'] ['upload_path'] ='./uploads_path_2/';
$ config ['target2'] ['allowed_types'] ='gif | jpg | png';
$ config ['target2'] ['max_size'] ='100';
$ config ['target2'] ['max_width'] ='1024';
$ config ['target2'] ['max_height'] ='768';
在上传控制器中,您将执行以下操作:
$ this-> config-> load('upload_settings',TRUE);
根据 upload_target
的值,从表单的post变量获取:
$ upload_config_values = $ this-> load-> config('target1' upload_settings');
或
$ upload_config_values = $ this-> load-> config('target2','upload_settings');
您现在可以载入或初始化上传类别:
$ this-> load-> library('upload',$ upload_config_values);
我把它写成元代码给你一个可行的方法。 >
设置隐藏值标志/参数
对于您用于上传文件的每个表单,请添加一个隐藏值
对于第一种形式:
从post变量获取 upload_target
并根据该值选择正确的配置文件。
I am uploading images from two separate views so in autoload under libraries, I have included upload.
I have a file called upload.php in config. From this I am passing parameters like size,file type etc.
if( ??){
$config['upload_path'] = realpath('x');
$config['allowed_types'] = 'gif';
$config['max_size'] = '1048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
}
else{
$config['upload_path'] = realpath('x');
$config['allowed_types'] = 'gif';
$config['max_size'] = '1048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
}
Now since there are two places where I upload, I have a different file size, type parameters, How do my controller know which one to take. What should I put in if{} else {] condition.
Let me know, If I am not not clear.
Your code has to do a few things:
(1) You need to set a flag (hidden variable is a good way) in each upload form so that you
know which folder to store the file in. Let's call this flag upload_target
In your controller:
(2) Get upload_target
from the post variables
(3) Depending on the value upload_target
, set the upload_config values as needed
and then load/initialize the upload class.
Where to put the upload_config
values
If you try to define a 2-dimensional array in config/upload.php, the library loader will probably get confused.
Instead, define a config file called upload_settings.php
and define the following arrays:
$config['target1']['upload_path'] = './uploads_path_1/';
$config['target1']['allowed_types'] = 'gif|jpg|png';
$config['target1']['max_size'] = '100';
$config['target1']['max_width'] = '1024';
$config['target1']['max_height'] = '768';
$config['target2']['upload_path'] = './uploads_path_2/';
$config['target2']['allowed_types'] = 'gif|jpg|png';
$config['target2']['max_size'] = '100';
$config['target2']['max_width'] = '1024';
$config['target2']['max_height'] = '768';
In your upload controller, you will do something like:
$this->config->load('upload_settings', TRUE);
Depending on the value of upload_target
that you got from the form's post variable:
$upload_config_values = $this->load->config('target1','upload_settings');
or
$upload_config_values = $this->load->config('target2','upload_settings');
You can now load or initialize your upload class:
$this->load->library('upload', $upload_config_values);
I wrote this up as meta-code to give you an outline of a feasible approach.
Setting Up the Hidden Value Flag/Parameter
For each form that you are using to upload files, add a hidden value as follows.
For the first form:
and for the second form:
Get the upload_target
value from post variables and pick the correct configuration file based on the value.
这篇关于代码点击器文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!