[英]File Uploading in Codeigniter
尝试通过Codeigniter上载图片,但出现错误:
您没有选择要上传的文件。
控制器:
function register()
{
$this->load->view('register');
if(isset($_POST['btnRegister']))
{
if($_POST['txtupass']===$_POST['txtucpass'])
{
//$data = array('uname'=>$_POST['txtuname'], 'uemail'=>$_POST['txtuemail'], 'upass'=>$_POST['txtupass'], 'utype'=>'user', 'uphoto'=>$_FILES['txtPhoto']['name']);
//$this->user->register_user($data);
//----Upload----//
$fname = $_FILES['txtPhoto']['name'];
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
//$FT = $_FILES['txtPhoto']['type'];
//$Ex =substr($FT, 6, strlen($FT));
//$FNAME = $_FILES['txtPhoto']['name'];
//$TempName = $_FILES['txtPhoto']['tmp_name'];
//move_uploaded_file($FNAME, './uploads/'.$FNAME.$Ex);
//-------------//
if ( ! $this->upload->do_upload()) { echo $this->upload->display_errors(); }
else
{
$this->upload->initialize($config);
$this->upload->do_upload($fname);
//$this->upload->data();
echo("<p>'".$_POST['txtuname']."' Registered Successfully!</b>");
}
echo("<p>'".$_POST['txtuname']."' Registered Successfully!</b>");
}
else
{
echo("<p style='color:red;'>Error: Password not match</p>");
}
}
}
视图:
<body>
<h1 align="center">Registration User</h1>
<?php echo form_open_multipart('users/register');?>
<table border="1" align="center">
<tr>
<th align="left" style="color:white;">Username</th>
<td><input type="text" name="txtuname" style="width:270px" /></td>
</tr>
<tr>
<th align="left" style="color:white;">Email</th>
<td><input type="text" name="txtuemail" style="width:270px" /></td>
</tr>
<tr>
<th align="left" style="color:white;">Password</th>
<td><input type="password" name="txtupass" style="width:270px" /></td>
</tr>
<tr>
<th align="left" style="color:white;">Confirm Password</th>
<td><input type="password" name="txtucpass" style="width:270px" /></td>
</tr>
<tr>
<th align="left" style="color:white;">Select Photo</th>
<td><input type="file" name="txtPhoto" style="color:white;" /></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="btnRegister" value="Register" /></td>
</tr>
</table>
</form>
</body>
我已经阅读了许多关于Stackoverflow的文章,并应用了所有文章,但都收到了相同的错误。 我混淆了问题所在。
加载上传库后,您必须进行初始化:
$this->load->library('upload', $config);
$this->upload->initialize($config);
在代码中进行以下更改:
1删除此行
$fname = $_FILES['txtPhoto']['name'];
2在您的以下代码中进行更改。
if (!$this->upload->do_upload('txtPhoto')) {
echo $this->upload->display_errors();
} else {
print_r($this->upload->data());
}
它可以在我的PC上运行。
您的控制器功能应为-
function register(){
$this->load->view('register');
if(isset($_POST['btnRegister']))
{
if($_POST['txtupass']===$_POST['txtucpass'])
{
$fname = $_FILES['txtPhoto']['name'];
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('txtPhoto')) {
print_r($this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($data);
echo("<p>'".$_POST['txtuname']."' Registered Successfully!</b>");
}
echo("<p>'".$_POST['txtuname']."' Registered Successfully!</b>");
}
else
{
echo("<p style='color:red;'>Error: Password not match</p>");
}
}
}
另外,请检查您的上传路径,因为我看到的错误仅在于上传路径。 您的应用程序中存在什么上载目录?
function insert()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$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('uploadv', $error);
} else {
$data = $this->upload->data();
$iname = $data['file_name'];
$name = $this->input->post('name');
$age = $this->input->post('age');
$this->Exam_mod->insert($name, $age, $img);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.