Codeigniter 文件上传问题
Codeigniter file uploading issue
我正在尝试使用 codeigniter 3 上传图像文件,但它不起作用。无论我做什么,它总是显示 "You didn't select a file to upload"。当我单击“网络”选项卡中的错误时,它会在控制台上记录错误 500,然后将我重定向到显示该错误的新选项卡。
这是我的 HTML 代码:
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<form method="post" enctype="multipart/form-data" action="<?php echo base_url('index.php/welcome/upload'); ?>">
<input type="text" name="username" value="Zahid Saeed">
<input type="file" name="profile_img">
<button type="submit">Submit Form</button>
</form>
</div>
这是我的控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper("url");
}
public function index()
{
$this->load->view('welcome_message');
}
public function upload() {
$config = array(
"upload_path" => "./uploads/",
"allowed_types" => "gif|jpg|png"
);
echo "<pre>";
print_r($this->input->post());
print_r($_FILES);
echo "</pre>";
$this->load->library("upload", $config);
$this->upload->initialize($config);
if(!$this->upload->do_upload("profile_img")) {
echo $this->upload->display_errors();
echo "IN IF";
}
else {
echo "img uploaded successfully";
}
}
}
还有一件事,确切的代码在 linux 机器上运行,实际上在服务器上运行。但它不适用于我的笔记本电脑。我正在使用 Windows 8.1
提前致谢
上传路径使用绝对路径可能是个好主意
$this->upload_config['upload_path'] = FCPATH . 'uploads/';
当我 运行 你的代码时,它在 Windows 10 Home 上的 WAMP64 下工作,即文件已上传,我可以看到
Array
(
[username] => Zahid Saeed
)
Array
(
[profile_img] => Array
(
[name] => header-background.png
[type] => image/png
[tmp_name] => C:\wamp64\tmp\php34DA.tmp
[error] => 0
[size] => 563
)
)
img uploaded successfully
打开您的 php.ini 文件
搜索扩展名=php_fileinfo.dll
如果您使用的是xampp,它可能会被默认注释
change
;extension=php_fileinfo.dll
to
extension=php_fileinfo.dll
并重新启动您的 xampp...
这解决了我的问题
//检查文件是否为空
如果(!empty($_FILES['attach_file']['name'])){
$config['upload_path'] = APPPATH.'../assets/images/'; $配置['allowed_types'] = 'jpg|jpeg|png|mp4'; $config['file_name'] = 'attach_file_'.time(); $config['max_size'] = "1024";$config['max_height']= "800";$config['max_width'] = "1280";$config['overwrite' ] = false;$this->upload->initialize($config);if(!($this->upload->do_upload("attach_file"))){$data['error']['attach_file'] = $this->upload->display_errors();else{ $coverPhotoData = $this->upload->data(); $coverPhoto =$coverPhotoData['file_name'];}}elseif($this->input->get('cup')){$coverPhoto = $previous['attach_file'];}
此代码不支持文件上传的 obj 和 fbx 格式我为此所做的更改
class 欢迎扩展 CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper("url");
}
public function index()
{
$this->load->view('welcome_message');
}
public function upload() {
$config = array(
"upload_path" => "./uploads/",
"allowed_types" => "gif|jpg|png"
);
echo "<pre>";
print_r($this->input->post());
print_r($_FILES);
echo "</pre>";
$this->load->library("upload", $config);
$this->upload->initialize($config);
if(!$this->upload->do_upload("profile_img")) {
echo $this->upload->display_errors();
echo "IN IF";
}
else {
echo "img uploaded successfully";
}
}
}
我正在尝试使用 codeigniter 3 上传图像文件,但它不起作用。无论我做什么,它总是显示 "You didn't select a file to upload"。当我单击“网络”选项卡中的错误时,它会在控制台上记录错误 500,然后将我重定向到显示该错误的新选项卡。 这是我的 HTML 代码:
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<form method="post" enctype="multipart/form-data" action="<?php echo base_url('index.php/welcome/upload'); ?>">
<input type="text" name="username" value="Zahid Saeed">
<input type="file" name="profile_img">
<button type="submit">Submit Form</button>
</form>
</div>
这是我的控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper("url");
}
public function index()
{
$this->load->view('welcome_message');
}
public function upload() {
$config = array(
"upload_path" => "./uploads/",
"allowed_types" => "gif|jpg|png"
);
echo "<pre>";
print_r($this->input->post());
print_r($_FILES);
echo "</pre>";
$this->load->library("upload", $config);
$this->upload->initialize($config);
if(!$this->upload->do_upload("profile_img")) {
echo $this->upload->display_errors();
echo "IN IF";
}
else {
echo "img uploaded successfully";
}
}
}
还有一件事,确切的代码在 linux 机器上运行,实际上在服务器上运行。但它不适用于我的笔记本电脑。我正在使用 Windows 8.1
提前致谢
上传路径使用绝对路径可能是个好主意
$this->upload_config['upload_path'] = FCPATH . 'uploads/';
当我 运行 你的代码时,它在 Windows 10 Home 上的 WAMP64 下工作,即文件已上传,我可以看到
Array
(
[username] => Zahid Saeed
)
Array
(
[profile_img] => Array
(
[name] => header-background.png
[type] => image/png
[tmp_name] => C:\wamp64\tmp\php34DA.tmp
[error] => 0
[size] => 563
)
)
img uploaded successfully
打开您的 php.ini 文件
搜索扩展名=php_fileinfo.dll
如果您使用的是xampp,它可能会被默认注释
change
;extension=php_fileinfo.dll
to
extension=php_fileinfo.dll
并重新启动您的 xampp...
这解决了我的问题
//检查文件是否为空 如果(!empty($_FILES['attach_file']['name'])){ $config['upload_path'] = APPPATH.'../assets/images/'; $配置['allowed_types'] = 'jpg|jpeg|png|mp4'; $config['file_name'] = 'attach_file_'.time(); $config['max_size'] = "1024";$config['max_height']= "800";$config['max_width'] = "1280";$config['overwrite' ] = false;$this->upload->initialize($config);if(!($this->upload->do_upload("attach_file"))){$data['error']['attach_file'] = $this->upload->display_errors();else{ $coverPhotoData = $this->upload->data(); $coverPhoto =$coverPhotoData['file_name'];}}elseif($this->input->get('cup')){$coverPhoto = $previous['attach_file'];}
此代码不支持文件上传的 obj 和 fbx 格式我为此所做的更改
class 欢迎扩展 CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper("url");
}
public function index()
{
$this->load->view('welcome_message');
}
public function upload() {
$config = array(
"upload_path" => "./uploads/",
"allowed_types" => "gif|jpg|png"
);
echo "<pre>";
print_r($this->input->post());
print_r($_FILES);
echo "</pre>";
$this->load->library("upload", $config);
$this->upload->initialize($config);
if(!$this->upload->do_upload("profile_img")) {
echo $this->upload->display_errors();
echo "IN IF";
}
else {
echo "img uploaded successfully";
}
}
}