Welcome Guest, Not a member yet? Register   Sign In
Codeigniter upload path invalid on webserver but valid on local host
#1

[eluser]Dandy_andy[/eluser]
I’m hoping someone can help me here as I’m pretty desperate now. Having tested my website on my localhost with no issues whatsoever, I have a photo upload function which doesn’t work now the site is on a remote web-server and judging by the searches I have done, this appears to be fairly common but I am very stuck. When someone tries to upload a photo, the error ‘The upload path does not appear to be valid’ is returned and I can’t figure out why. My suspicion is that the ‘MKDIR’ path isn’t correct relative to where the codeigniter upload function is trying to place the file. But I don’t understand why these scripts worked fine on my local host.
The structure of my site is as follows and I am trying to create subfolders in the main folder ‘PHOTOS’ to upload to:-

HTTPDOCS/
APPLICATION
SYSTEM
PHOTOS

Part of my model file (functions used to generate a random string to set the file path and the subdirectory structure):-
Code:
class Image_file extends CI_Model {

//generate random string for filename and path
public function string_generate() {

  $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  $size = strlen($chars);
  $string = '';  
  for ($i = 0; $i < 15; $i++) {  
  $string .= $chars[rand(0, $size - 1)];  
   }
  return $string;

}


//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;

}

And this relates to the controller (relevant part shown only):-

Code:
//upload photo file
public function upload_file() {
  
  $mem_id = $this->session->userdata('mem_id');
  $this->load->model('image_file');
  
  $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 .= substr($this->session->userdata('username'), 0, 4);}
  
  //echo '<br /><br /><br /><br /><br /><br /><br /><br />'.$set_image_path; exit();
  $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');

Can anyone help??? I've been trawling through pages and pages of people having a similar issue, but can't seem to solve this one.

Thanks.
#2

[eluser]Dandy_andy[/eluser]
I seem to have resolved this permissions issue by running PHP as a CGI application and adding a quick function to check if a directory already exists or not. I think the system was failing because some directories existed. This is how I changed the controller code for anyone interested and it now works...

Code:
//check if image exists in directory with same string (unlikely but better safe than sorry!)
  if (file_exists($set_image_path.$string.'.jpeg')) {$string .= substr($this->session->userdata('username'), 0, 4);}
  
  
  //check first if directory exists
  if (is_dir($set_image_path) == false)
   {
   mkdir($set_image_path, 0755, true);
   //echo 'created a new directory';
   }
  //echo 'nothing created';
  //exit();
  $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);




Theme © iAndrew 2016 - Forum software by © MyBB