Welcome Guest, Not a member yet? Register   Sign In
File uploading class - Uploaded files take some time to be available.
#1

[eluser]Pedro Correia[/eluser]
I'm using the file uploading and came across a small but extremely frustrating problem with i'll explain with some "pseudo" code:

Code:
public function somemethod(){
...
  // uploading an image file
  if ($this->upload->do_upload('image_file'))
  {
    // get the upload data and store it in an array
    $data['upload'] = $this->upload->data();
    
    // take the upload data array and save it to a db
    if(!$this->mymodel->save_upload_data($data)){
  
      // db write fails, delete that stray image from our file system
      if(file_exists($uploaded_image_path))
        unlink($uploaded_image_path);

    }    
  }
  ...
}

The problem i'm having is that when the database write part fails and i try to delete the stray image file, it is not in its final upload destination path.

It only is visible and available for manipulation about a second later.

I've read some similar complaints on the php.net documentation comments where a couple of users state that the move_uploaded_file sometimes has a sort of asynchronous behavior.

However using php's copy function behaves the same.

I've tried to delete the file in the File Uploading class code right after the file has been moved from its temp_dir to its final destination and it works just fine.

Doing it from a controller fails.

I was developing on a windows machine and thought it might me the cause, with some file system quirks, but running my code on linux (debian) gets the same results.

I'm using a workaround and storing the "stray" image paths somewhere else and deleting them later but it's really frustrating that the File Uploading class do_upload() method returns true and it's only "trueish".

Anyone have any insight on this one?

Thanks in advance,
Pedro
#2

[eluser]jjDeveloper[/eluser]
I have not tried this but would this not work?

Code:
public function somemethod(){
...
  // uploading an image file
  if ($this->upload->do_upload('image_file'))
  {
    // get the upload data and store it in an array
    $data['upload'] = $this->upload->data();
    
    // take the upload data array and save it to a db
    if(!$this->mymodel->save_upload_data($data)){
  
      // db write fails, delete that stray image from our file system
      if(file_exists($uploaded_image_path))
        unlink($data['upload']['full_path']);

    }    
  }
  ...
}

or just simplify your data array

Code:
public function somemethod(){
...
  // uploading an image file
  if ($this->upload->do_upload('image_file'))
  {
    // get the upload data and store it in an array
    $data = $this->upload->data();
    
    // take the upload data array and save it to a db
    if(!$this->mymodel->save_upload_data($data)){
  
      // db write fails, delete that stray image from our file system
      if(file_exists($uploaded_image_path))
        unlink($data['full_path']);

    }    
  }
  ...
}
#3

[eluser]Pedro Correia[/eluser]
Hi jjDeveloper,

Thanks for your reply but unfortunately it does not work.

From "watching" the process it "feels" like, even though the do_upload method returns true, by the time i'm done with the db writes, or in case of failure as my example illustrates, the file_exists() function returns false.

The file only becomes available about a couple of seconds later.

It's really strange and i have no explanation for it.

Thank you for you help anyway.
#4

[eluser]jjDeveloper[/eluser]
Where ae you declaring $uploaded_image_path? And is it giving the statement the correct path?

I just don't see where the unlink is going to find this variable in your snippet.




Theme © iAndrew 2016 - Forum software by © MyBB