在我的CodeIgniter项目中,我在项目创建过程中上传文件。这是上传功能:
public function addProcess()
{
echo 'test';
if($this->input->server('REQUEST_METHOD') === 'POST')
{
$this->form_validation->set_rules('album', 'Album', 'trim|required|callback_check_select', 'xss_clean');
if ($this->form_validation->run() === FALSE)
{
$data['albums'] = $this->album_model->albumList();
$this->adminTheme('admin/addSong',$data);
}
else
{
$error = 0;
$this->load->library('upload');
$total_count_of_files = count($_FILES['userfile']['name']);
$album = $this->input->post('album');
$songs = $this->input->post('song');
$artist = $this->input->post('artist');
for($i=0; $i< $total_count_of_files; $i++)
{
if(!$songs[$i])
$songs[$i] = "Track-".($i+1);
if(!$artist[$i])
$artist[$i] = " ";
$_FILES['file']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['file']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['file']['size'] = $_FILES['userfile']['size'][$i];
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|jpeg|gif|png|mp3';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$this->upload->initialize($config);
if($this->upload->do_upload('file'))
{
$msg = $this->upload->data();
$name = $msg['file_name'];
$error += 0;
$this->song_model->add($songs[$i], $artist[$i], $name, $album);
}else{
$errorStr[] = "$songs[$i] Upload Faild";
$error += 1;
}
}
//$this->album_model->add();
if($error == 0)
{
//$this->msg('admin/song','Successfully added..Redirecting now...');
}
else
{
$this->msg('admin/song','Successfully added..Redirecting now...');
}
}
}
else
{
redirect('admin/album');
}
}
doupload的另一个函数
public function do_upload()
{
$upload_path_url = base_url().'uploads/';
$config['upload_path'] = FCPATH.'uploads/';
$config['allowed_types'] = 'jpg|jpeg|gif|png|mp3';
$config['max_size'] = '30000';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
} else {
$data = $this->upload->data();
$info->name = $data['file_name'];
$info->size = $data['file_size'];
$info->type = $data['file_type'];
$info->url = $upload_path_url .$data['file_name'];
$info->thumbnail_url = $upload_path_url .$data['file_name'];
$info->delete_url = base_url().'upload/deleteImage/'.$data['file_name'];
$info->delete_type = 'DELETE';
if (IS_AJAX) {
echo json_encode(array($info));
} else {
$file_data['upload_data'] = $this->upload->data();
$this->load->view('admin/upload_success', $file_data);
}
}
}
我的表单代码
<div id="container">
<h2>Song Name - Add New</h2>
<div class="error">
<?php echo validation_errors(); ?>
</div>
<?php $attributes = array('id' => 'myform'); ?>
<?php echo form_open_multipart('admin/song/addProcess',$attributes); ?>
<div class="line">
<label style="width: 160px;">Album:</label>
<select class="category" name="album" style="width: 280px;">
<option value="select">Select Album</option>
<?php foreach($albums as $album): ?>
<option value="<?php echo $album['id']; ?>">
<?php echo $album['root_name']." --> ".$album['category_name']." --> ".$album['album_name']; ?></option>
<?php endforeach ?>
</select>
<input class="form-submit" style="margin-top: 6px;" type="button" id="more_fields" onclick="add_fields();" value="Add More" />
</div>
<!--end line-->
<div id="nameFields">
<div class="line">
<input type="text" name="song[]" class="input1" style="width: 180px;" placeholder="Song Name" />
<input type="text" name="artist[]" class="input1" style="width: 180px;" placeholder="Artist Name" />
<input type="file" name="userfile[]" class="category" style="width:80px;" />
</div>
<!--end line-->
</div>
<input type="submit" value="Save" class="form-submit" style="margin-right: 180px;" />
</form>
</div>
<!--end container-->
<script type="text/javascript">
function add_fields() {
document.getElementById('nameFields').innerHTML += "<div class='line'><input type='text' name='song[]' class='input1' style='width: 180px;' placeholder='Song Name' /><input type='text' name='artist[]' class='input1' style='width: 180px;' placeholder='Artist Name' /><input type='file' name='userfile[]' class='category' style='width:80px;' /></div>";
}
</script>
然而,这没有任何作用。文件未上传。甚至没有创建空目录。有人能帮我找到错误吗?
感谢。
答案 0 :(得分:0)
在你的addProcess代码中,删除下面的代码
$_FILES['file']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['file']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['file']['size'] = $_FILES['userfile']['size'][$i];
并尝试
foreach ($_FILES as $fieldname => $fileObject)
{
if (!empty($fileObject['name']))
{
$this->upload->initialize($config);
if (!$this->upload->do_upload($fieldname))
{
$errors[] = $this->upload->display_errors();
}
else
{
//success code
}
}
}