I am trying to upload a single file in CodeIgniter 2.0.2 in a normal way.
The do_upload() function returns true but no file appears in the directory.
Directory permissions appear to be fine (after running other diagnostics not shown in code below), so it makes no sense that the file is not appearing in the upload directory.
Here is controller and view code, adapted straight from the CI docs.
Controller:
function do_upload_sql(){
// create directory
if (! is_dir(BACKUP_DIR)) {
mkdir(BACKUP_DIR, 0777);
}
// or if it exists and has restricted file permissions, change them
elseif(fileperms(BACKUP_DIR)!=0777){
chmod(BACKUP_DIR, 0777);
}
$config['upload_path'] = BACKUP_DIR;
$config['allowed_types'] = 'backup'; // an SQL backup file type
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
//$this->upload->initialize($config); //redundant, so commenting out
if ( ! $this->upload->do_upload())
{
$data['action'] = 'c_backup_restore/do_upload_sql';
$tab_error = array('error' => $this->upload->display_errors());
$data['error'] = $tab_error['error'];
$this->load->view('common/header');
$this->load->view('v_upload_sql', $data);
}
else
{
echo "success"; // yes it's getting here, but i get no file!
$data = array('upload_data' => $this->upload->data());
$file_upload = $data["upload_data"];
$this->restore_backup($file_upload); // go do something useful with the file
}
}
View:
<p>Select the backup file</p>
<div class="not_success"><?php echo $error;?></div>
<?php echo form_open_multipart($action);?>
<input type="file" name="userfile" size="30" />
<input type="submit" value="Upload" />
</form>
I found the bug using the xDebug profiler.
It turned out that the native php function
chdir(path)further downstream was the problem.@qwertzman’s link to paths in CI may prove to be helpful to determining exactly why the hangup occurs.