【发布时间】:2017-05-17 04:30:40
【问题描述】:
我在使用框架之前创建了一个自定义函数,现在我想再次使用它。当我尝试使用自定义函数上传图像时出现问题。它说错误undefined index : [the field_name]。
大多数人使用 CI 库和 CI 上传函数do_upload(),但我想使用我自己的函数,因为它还会创建较小的图像用作拇指。
我 3 天前开始使用 CI,但仍然不知道如何更改任何可以使 $_FILES[] 正常工作的东西。
观点:
<form action="path/to/controller/method" method="post" enctype="multipart/form-data">
<input type="file" name="fupload">
</form>
控制器:
public function __construct(){
parent::__construct();
$this->load->helper('fungsi_thumb'); // this is the custom function
$config['allowed_types'] = '*';
$this->load->library('upload',$config);
}
public function input_file(){
$data = array(
'location_file' => $_FILES['fupload']['tmp_name'],
'type_file' => $_FILES['fupload']['type'],
'name_file' => $_FILES['fupload']['name']
);
$this->load->model('input_model');
$this->input_model->put_file($data);
}
我已经将我的自定义函数文件放入application\helpers\。我应该同时显示自定义函数文件和模型文件吗?
我也已经改了public $allowed_types = '*';
更新
模型
public function put_file($data){
//BUAT FILE
$lokasi_file = $data['location_file'];
$tipe_file = $data['type_file'];
$nama_file = $data['name_file'];
$acak = rand(1,99);
$nama_file_unik = $acak.$nama_file;
UploadImage($nama_file_unik); // this is my custom function
$sql="INSERT INTO produk(gambar)VALUES (?)";
$query=$this->db->query($sql,array($nama_file_unik));
if($query)
{
echo "BERHASIL";
}
else
{
echo "GAGAL";
}
}
更新新内容
我终于可以使用$_FILES,我需要在控制器构造函数内部加载库。
现在的新问题是即使在我自己的自定义函数中使用do_upload() 函数也没有上传文件
这是我的自定义功能
function UploadImage($fupload_name){
// SET DATA FILE NYA
$config['file_name'] = $fupload_name;
$config['upload_path'] = 'http://localhost/mobileapp/assets/gambar/';
var_dump($config['upload_path']);
//load the upload library
$CI =& get_instance();
$CI->load->library('upload',$config);
//Upload the file
if( !($CI->upload->do_upload('fupload'))){
$error = $CI->upload->display_errors();
}else{
$file_data = $CI->upload->data();
}
}
现在正如您在上面看到的,我正在尝试使用$config['file_name'] = $fupload_name; 将file_name 更改为一个新的随机生成的名称(在模型中完成)并创建一个新对象,因为显然我需要这样做加载库并使用do_upload() 方法。
但我仍然无法使用它。现在我又卡住了
【问题讨论】:
-
在
input_model中向我们展示您的put_file函数以了解实际问题。 -
CI 中的
Upload.php文件也使用$_FILES[],但是当我使用它时,它显示undefined index....为什么? -
现在编辑@RohanKumar
标签: php codeigniter file-upload