Welcome Guest, Not a member yet? Register   Sign In
do_upload() returns true but no file is uploaded. CI 2.0.2
#1

[eluser]tinflute[/eluser]
http://stackoverflow.com/questions/10503...black-hole

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:
Code:
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:
Code:
<p>Select the backup file</p>
<div class="not_success">&lt;?php echo $error;?&gt;</div>
&lt;?php echo form_open_multipart($action);?&gt;
&lt;input type="file" name="userfile" size="30" /&gt;
&lt;input type="submit" value="Upload" /&gt;
&lt;/form&gt;
#2

[eluser]sailorob[/eluser]
Have you tried just running a manual fwrite to your BACKUP_DIR?
#3

[eluser]tinflute[/eluser]
no, i'll try that now and post the result. thx
#4

[eluser]tinflute[/eluser]
Ok, I borrowed the code below from php.net to test if fwrite works.
The function reports success, but yet again no file appears!!
So this suggest that it has to be some kind of path issue.
But the same path string is being used successfully for mkdir().
So then it should be a permissions issue, but that has been ruled out
by the fwrite test. Weird

Code:
function file_write_test($filename = '_no.txt',$content = 'hello world'){
  
  // Let's make sure the file exists and is writable first.
  if (is_writable($filename)) {
  
      // In our example we're opening $filename in append mode.
      // The file pointer is at the bottom of the file hence
      // that's where $somecontent will go when we fwrite() it.
      if (!$handle = fopen($filename, 'a')) {
           echo "Cannot open file ($filename)";
           exit;
      }
  
      // Write $somecontent to our opened file.
      if (fwrite($handle, $content) === FALSE) {
          echo "Cannot write to file ($filename)";
          exit;
      }
  
      echo "Success, wrote ($content) to file ($filename)";
  
      fclose($handle);
  
  } else {
      echo "The file $filename is not writable";
  }
}
#5

[eluser]sailorob[/eluser]
Whats the value of BACKUP_DIR? And were you appending to the $filename variable you passed into that above function?

Not to state the obvious but is it a full system path? Like "/var/www/website/files/"? If not, your file is just going to be written the path local to where your script is running, less you've set a chdir().
#6

[eluser]tinflute[/eluser]
define('BACKUP_DIR', 'C:\backup_database\\');
#7

[eluser]tinflute[/eluser]
the file i passed into the $filename variable was the complete path so BACKUP_DIR.$userfile
and this is what got echoed to the browser:
Success, wrote (hello world) to file (C:/backup_database/db_pk_v4.backup)
#8

[eluser]tinflute[/eluser]
AHA!
I changed
Code:
$config['allowed_types'] = '*';
And now if i upload a .txt file the fwrite() command succeeds at writing a file to the correct location.
So this means it has to do with the mime types.
My application/config/mimes.php has been extended with an entry for
Code:
'backup'=>'application/octet-stream',
But something must not be working in do_upload() in recognizing this type, because it fails to write the file.
I understand that CI 2.1 introduced a bug in do_upload() related to mime types (will look for the thread on this). I'm using 2.0.2.
Do I need to change something in core do_upload() or is my mime extension defined incorrectly?
Thanks for the help so far!!
#9

[eluser]InsiteFX[/eluser]
If running on Windows:

Right click the folder, click properties, open security tab, edit the users (or your computer name) tick the modify(I tick all).

It works, I also use php and save it to htdocs, it works without the need of running as admin, which wont do to folders!




Theme © iAndrew 2016 - Forum software by © MyBB