Welcome Guest, Not a member yet? Register   Sign In
Randomly generated string seems to be the same if functions are called at very similar times
#1

[eluser]Dandy_andy[/eluser]
I've designed a photo upload page for my website and everything works fine. Images uploaded are stored in a directory that has a randomly generated path. However, when multiple users are trying to upload photos at the same time, the system seems to generate the same path. To overcome this, I've added an extra function to check the path first and see if an image already exist, in which case it adds an extra '1' onto the end of the file path. I was wondering whether this is caused by some caching issue because it only occurs if files are uploaded from different machines at the same time?

Code:
//upload photo file
public function upload_file() {
  
  $this->load->model('image_file');
  $mem_id = $this->session->userdata('mem_id');
  
  $string = $this->image_file->string_generate();
  $set_image_path = $this->image_file->set_image_path($string);
  
  //check if image exists in directory with same string (unlikely but better safe than sorry!)
  if (file_exists($set_image_path.$string.'.jpeg')) {$string .= '1';}
  
  $config['upload_path'] = $set_image_path;
  $config['allowed_types'] = 'gif|jpg|jpeg|png';
  $config['max_size'] = '4000';
  $config['max_width'] = '6000';
  $config['max_height'] = '4000';
  $config['file_name'] = $string.'.jpeg';
  $this->load->library('upload', $config);
  
  //create the directory
  mkdir($set_image_path, 0644, true);

  //do upload & process image (rezize and rename)
  $this->upload->do_upload('userphoto');
  $results = $this->upload->data();
  $error = $this->upload->display_errors('', '');
  
  if (($error) or (!$this->image_file->check_file_exists($set_image_path.$string.'.jpeg'))) {
   $this->session->set_flashdata('error', $error);
   redirect('user/upload_photo');
   }
  
  //write the data to the photo db
  $this->image_file->write_imagedata($mem_id, $string, $set_image_path);
  
  //load image library
  $this->load->library('image_lib');
  
  
  $config['image_library'] = 'gd2';
  $config['source_image'] = $set_image_path.$string.'.jpeg';
  $config['create_thumb'] = TRUE;
  $config['maintain_ratio'] = TRUE;
  $config['quality'] = 50;
  $config['width'] = 160;
  $config['height'] = 120;
  $this->image_lib->initialize($config);
  $this->image_lib->resize();
  
   //error check
   if (! $this->image_lib->resize()) {
   $error = $this->image_lib->display_errors('', '');
   $this->session->set_flashdata('error', $error);
   redirect('user/upload_photo');
   }
   else
    {
   $this->load->library('Notificationmssgs');
   $notification = $this->notificationmssgs->notifications();
   $this->session->set_flashdata('message', $notification[3]);
   }
  
  $this->image_lib->clear();
  
  $config['image_library'] = 'gd2';
  $config['source_image'] = $set_image_path.$string.'.jpeg';
  $config['create_thumb'] = FALSE;
  $config['maintain_ratio'] = TRUE;
  $config['quality'] = 50;
  $config['width'] = 560;
  $config['height'] = 420;
  $this->image_lib->initialize($config);
  $this->image_lib->resize();
  $this->image_lib->clear();
      
  redirect('user/upload_photo');

}//function submit profile

And the relevant model functions are:-

Code:
//set image path (generate random code which will also form filename)
public function set_image_path($string) {

  $pos = 6;
  $f = substr($string, 0, $pos);
  $first = chunk_split($f,2,"/");
  $photo_path = 'photos/'.$first;

  return $photo_path;

}


//check a file exists in the directory
function check_file_exists($path) {

if (file_exists($path)) {return true;} else {return false;}

}


//write image data to photo db
public function write_imagedata($mem_id, $string) {

  $this->load->helper('date');
  $datetime = unix_to_human(time(), TRUE, 'eu');
  
  //check to see if user already has an image in their photos db. If not, also copy the path of upload image to members db
  $this->db->where('mem_id', $mem_id);
  $this->db->from('photos');
  if ($this->db->count_all_results() <= 0) {$dbdata = array('photo_path' => $string, 'approved' => '0'); $this->db->where('mem_id', $mem_id); $this->db->update('members', $dbdata);}
  
   $dbdata = array(
   'mem_id' => $mem_id,
   'photo_path' => $string,
   'approved' => 0,
   'main' => 0,
   'votes' => 0,
   'rating' => 0,
   'voting' => 1,
   'posted' => $datetime,
   'pho_caption' => ''
  
   );
  
  $this->db->insert('photos',$dbdata);
}


Messages In This Thread
Randomly generated string seems to be the same if functions are called at very similar times - by El Forum - 09-12-2012, 11:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB