您没有选择要上传的文件。PHP 代码点火器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20115515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
You did not select a file to upload. PHP Code Igniter
提问by SSMA
I am trying to implement a post functionality and want to pick message and image from a php view. My message functionality is working good. But on image upload i receive an error You did not select a file to upload. This is my controller function
我正在尝试实现发布功能,并希望从 php 视图中选择消息和图像。我的消息功能运行良好。但是在图像上传时我收到一个错误You did not select a file to upload。这是我的控制器功能
function post_func()
{
session_start();
echo $post_message=$_POST['post'];
echo $share_with=$_POST['share_with'];
echo $image=$_POST['image'];
if($image==null){
echo "<br/>no image<br/>";
}
else{
////////////////////////////////////////////////////////////////////////////////////////
$config['upload_path'] = './application/css';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo "<br/>";
echo $this->upload->display_errors();
echo "<br/> image error<br/>";
}
else
{
echo "<br/> reached <br/>";
session_start();
$this->membership_model->insert_images($this->upload->data(),$email);
$data = array('upload_data' => $this->upload->data());
echo "<br/ problem<br/>";
}
////////////////////////////////////////////////////////////////////////////////////////
}
$public;
if($share_with=="public"){
echo "1";
$public=true;
}else{
echo "0";
$public=false;
}echo "-----------------------------<br/>";
echo $user=$this->session->userdata('user_identification');
$data = array
(
'userid'=> $user,
'public' => $public,
'message' => $post_message,
'picname' => "None"
);
$this->load->model('membership_model');
$this->membership_model->add_message($data);
echo "</br>";
echo $user=$this->session->userdata('user_identification');
}
This is my view.
这是我的看法。
<?php echo form_open('search/post_func');?>
<!--<form id="loginForm" action="../search/post_func" method="post" enctype="multipart/form-data" >-->
<div id="your_post">
<div id="post_image">
<img id ="post_img" src="<?php echo $this->config->item('base_url'); ?><?php echo '/application/css/'. $img ?>"/>
</div>
<textarea name="post" rows="5" cols="30" placeholder="Share an update..." id="post_text" rows="2" value=""></textarea>
//other view items
<?php
echo form_close();
?>
Please help me
请帮我
回答by mzalazar
Change:
改变:
$this->upload->do_upload()
To this:
对此:
$this->upload->do_upload('my_file_input_name')
and it will work!! :)
它会起作用!!:)
回答by Nil'z
Change:
改变:
<?php echo form_open_multipart('search/post_func');?>
回答by user2936213
Your form type should be "multipart"
Change your form tag to:
您的表单类型应该是“multipart”
将您的表单标签更改为:
<?php echo form_open_multipart('search/post_func');?>
回答by Ahmed Mahmoud
enctype='multipart/form-data'
you should put previous attribute in form tag
你应该把以前的属性放在表单标签中
$this->upload->do_upload('file_name')
pass input tag file name to pervious line in your controller
将输入标记文件名传递到控制器中的上一行
<input type="file" name ="file_name" >
回答by user6151303
Do this:
做这个:
if(isset($_FILES['profile_pic']['name']) && !empty($_FILES['profile_pic']['name'])){
$config['upload_path']= './assets/img/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$config['max_width'] = 10000;
$config['max_height'] = 10000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('profile_pic'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
exit;
}
else
{
$data = array('upload_data' => $this->upload->data());
if(!empty($data)){
$img1 = $data['upload_data']['file_name'];
}
}
}
$insert_data['profile_pic'] = $img1;
回答by ShaziaNasim
example code is
示例代码是
$config['upload_path'] = FCPATH.'upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
if ( ! $this->upload-> do_upload('userfile'))
{
$errors = $this->upload->display_errors();
$this->session->set_flashdata('error', $errors);
redirect('ur_link');
}
else
{
$data = array('upload_data' => $this->upload->data());
$fullpath= $data['upload_data']['full_path'];
$file_name = $data['upload_data']['file_name'];
}
}
回答by Indrasinh Bihola
Another reason I find this problem to be occurred. if you forget to initialize after load your library:
我发现这个问题发生的另一个原因。如果您在加载库后忘记初始化:
$this->load->library('upload', $config);
$this->upload->initialize($config); //Make this line must be here.
I hope this might help you.
我希望这可以帮助你。
回答by Robot70
So note I was doing a mysql upload import and I got the "You did not select a file to upload" message. It turns out my import sql file had extra columns which caused the fail and returned this error message.
所以请注意,我正在执行 mysql 上传导入,并且收到“您没有选择要上传的文件”消息。事实证明我的导入 sql 文件有额外的列导致失败并返回此错误消息。

