【发布时间】:2014-02-17 22:04:19
【问题描述】:
以下是使用 codeigniter 构建的文件上传功能。这段代码在 locahost 上运行良好,但是当我上传到服务器时,没有任何效果。
文件夹未创建,数据未插入数据库。
error_reporting(E_ERROR); // remove when error checking
class Upload extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model("upload_model");
$this->config->load('uploads');
}
function index()
{
$upload_config = $this->upload_model->get_config();
$data['upload_config']=$upload_config;
$data['errors']='';
$data['fb_user']=$fb_user;
$this->form_validation->set_rules('userfile', 'UserFile', 'required');
if ($this->input->post('submit'))
{
//Access bf_upload_settings table through Model function
// Check and Create upload path
if (!is_dir($upload_config['upload_path']))
{
mkdir($upload_config['upload_path'], 0777, TRUE);
}
// Check and Create tmp directory
if (!is_dir($upload_config['upload_path'].'/'.$this->config->item('tmp_dir')))
{
if(!mkdir($upload_config['upload_path'].'/'.$this->config->item('tmp_dir')) )
die('Failed to create folder: '.$this->config->item('tmp_dir') );
chmod($upload_config['upload_path'].'/'.$this->config->item('tmp_dir'),0777);
}
// Check and Create User directory (username used)
// set the configurations fro the CI Upload class, using values from database in bf_upload_settings table
$config['upload_path'] = $upload_config['upload_path'].'/'.$this->config->item('tmp_dir');
$config['allowed_types'] = implode('|', unserialize( $upload_config['allowed_types'] ) );
$config['max_size'] = $upload_config['max_size_kb']*1024; //Value stored in Database (KB) converted to Bytes
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$data['errors']=$this->upload->display_errors();
}
else
{
$upload = $this->upload->data();
$data = array(
'name' => $upload['file_name'],
'file_size' => $upload['file_size'],
'file_path' => $upload['full_path'],
'file_type' => $upload['file_type'],
'user_id' => $user_profile['id']
);
$is_image=$upload['is_image']; // Checks if the uploaded file is an image file
$file_type=$upload['file_type'];
$name= $upload['file_name'];
//Check if file is a Video, if so, send it to the video folder in User Uploads Folder
if ($upload['file_type']=='video/mp4')
{
if (!is_dir($upload_config['upload_path'].'/'.$this->config->item('videos_dir')))
{
if(!mkdir($upload_config['upload_path'].'/'.$this->config->item('videos_dir')) )
die('Failed to create folder: '.$this->config->item('videos_dir') );
chmod($upload_config['upload_path'].'/'.$this->config->item('videos_dir'),0777);
}
copy($upload_config['upload_path'].'/'.$this->config->item('tmp_dir').'/'.$name, $upload_config['upload_path'].'/'.$this->config->item('videos_dir').'/'.$name);
// rename($upload_config['upload_path'].'/'.$this->config->item('videos_dir').'/'.$name, $upload_config['upload_path'].'/'.$this->config->item('videos_dir').$name);
unlink($upload_config['upload_path'].'/'.$this->config->item('tmp_dir').'/'.$name);
}
if ($upload['file_type']=='audio/mpeg' || 'audio/mp3') //Check if file is an image, if so, send it to the images folder in User Uploads Folder
{
if (!is_dir($upload_config['upload_path'].'/'.$this->config->item('audio_dir')))
{
if(!mkdir($upload_config['upload_path'].'/'.$this->config->item('audio_dir')) )
die('Failed to create folder: '.$this->config->item('audio_dir') );
chmod($upload_config['upload_path'].'/'.$this->config->item('audio_dir'),0777);
}
copy($upload_config['upload_path'].'/'.$this->config->item('tmp_dir').'/'.$name, $upload_config['upload_path'].'/'.$this->config->item('audio_dir').'/'.$name);
// rename($upload_config['upload_path'].'/'.$this->config->item('audio_dir').'/'.$name, $upload_config['upload_path'].'/'.$this->config->item('audio_dir').$name);
unlink($upload_config['upload_path'].'/'.$this->config->item('tmp_dir').'/'.$name);
}
if ( $this->upload_model->insert($data) )
{
$this->load->view('upload_success');
}
else
{
redirect(current_url());
}
}
}
$this->load->view('upload',$data);
}
}
现在没有任何效果。我该如何解决这个问题?
更新: 此外,当我尝试上传文件(在本例中为 .mp4 视频)时,我收到错误消息“不允许您尝试上传的文件类型。”,即使我已将其设置为允许的类型。
更新 2:
我一直在尝试上传视频 (.mp4)。我确实将其设置为允许的类型。我试过上传pdf和图片,都上传成功了。
为什么服务器没有上传视频文件?我该如何解决这个问题?
【问题讨论】:
-
检查您的 $upload_config['upload_path'] 路径和权限。尝试手动创建 $upload_config['upload_path'] 并设置 0777 权限。
-
检查你的 base_url 路径和 upload_path 也
-
还是不行。我是否必须更改任何服务器配置。我正在使用 filezilla
-
@AghaUmairAhmed 我的 base_url 是 app.com/upload,我的上传路径设置为 assets/uploads
-
通过 cpanel 或您拥有的主机帐户授予文件夹写入权限
标签: php codeigniter file-upload