以下是使用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和图片,他们都成功上传了。
为什么服务器没有上传视频文件?我该如何解决这个问题?
答案 0 :(得分:0)
你的文档根路径不是你必须在constants.php中定义的基本URL,就像这个
一样define('UPLOAD_PATH', $_SERVER['DOCUMENT_ROOT'].'/yoururl/assets/uploads/');
当echo使用类似的东西时
UPLOAD_PATH.yourimg