0

I have problem in creating folder in server (centos), there is a form with file upload field, where the ajax callback always returned error as:

Warning: mkdir(): Permission denied in /home/sitename/public_html/inc/callback/request_update.php on line 90

Warning: move_uploaded_file(../../images/listing/16/805202.jpg): failed to open stream: No such file or directory in /home/sitename/public_html/inc/callback/request_update.php on line 95

Warning: move_uploaded_file(): Unable to move '/tmp/phpnrtrdp' to '../../images/listing/16/805202.jpg' in /home/sitename/public_html/inc/callback/request_update.php on line 95

and its indicate the line in file request_update.php is causing the error:

mkdir($output_dir, 0755, true);

request_update.php:

if(isset($_FILES['files'])){

foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){

    $file_name = $key.'_'.$_FILES['files']['name'][$key];
    $file_size = $_FILES['files']['size'][$key];
    $file_tmp = $_FILES['files']['tmp_name'][$key];
    $file_type = $_FILES['files']['type'][$key];

    //explode fine name and extension
    $ext_x = explode('.', $_FILES['files']['name'][$key]);
    $ext = strtolower(end($ext_x));
    $file_name = str_replace('.'.$ext, '', $_FILES['files']['name'][$key]);

    //new file name
    $output_dir = '../../images/listing/'.$list_id;
    $new_file_name[] = rand(1, 999999).'.'.$ext;
    $pathfile = $output_dir.'/'.end($new_file_name);

    // create directory if does not exist
    if(is_dir($output_dir) == false){
        mkdir($output_dir, 0755, true); /*this is where error indicate*/
    }

    if(is_dir($pathfile) == false){

        if(move_uploaded_file($file_tmp, $pathfile)){
            
            //watermark
            $water_path = '../../images/watermark.jpg';
            $watermark = WideImage::load($water_path);
            
            //resize original image
            WideImage::load($pathfile)->resize(300, 360)->merge($watermark, '50% – 25', '100% – 40', 80)->saveToFile($pathfile);

            
        }

    }

}

}

I've been tried the same code run in another shared server and even localhost, the folder can be created fine all the time, what is happen?

Community
  • 1
  • 1
conmen
  • 2,377
  • 18
  • 68
  • 98

1 Answers1

1

I would add to Simon Groenewolt comment-answer, is that you should start from checking if PHP can really write in the path you want, by using is_writable function:

Example:

$filename = 'test.txt';
if (is_writable($filename))
  {
    echo 'The file is writable';
  } 
else
  {
    echo 'The file is not writable';
  }

Just set proper permission, as stated by Log1c on the directory you're trying to write in, by running chmod a+w /yourpath/yourdir, which is 666, meaning read and write for all, a user, an owner and the world.


is_writable() Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.

Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody'). Safe mode limitations are not taken into account.

EDIT 1:

Make sure that PHP open_basedir setting, is also set to access /tmp. You could just disable it for testing purposes, to see what you get. Try checking what is open_basedir settings are set to, by running:

echo ini_get('open_basedir');
Community
  • 1
  • 1
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
  • yes, I just with embed this codes, it show `The file is not writable`, what shall I do in server setting? – conmen Apr 04 '14 at 20:19
  • @conmen What is the output of terminal command: `ls -lsa /path_to/images/listing/`? – Ilia Ross Apr 04 '14 at 20:25
  • output: `4 drwxr-xr-x 13 moment69 nobody 4096 Apr 4 19:41 ./ 4 drwxr-xr-x 5 moment69 nobody 4096 Apr 2 00:44 ../ 4 drwxr-xr-x 2 moment69 nobody 4096 Apr 4 00:18 1/ 4 drwxr-xr-x 2 moment69 nobody 4096 Apr 4 00:18 10/ ` – conmen Apr 04 '14 at 20:29
  • It seems that you're running Apache as nobody (mod_php) and you have for `group` *xr* value. Run `chmod 666 /path_to/images/listing` to make sure that `group` can also write and so far `group` (Apache running as nobody) can only *read/execute*, which is `(xr)`. I should solve your problem. – Ilia Ross Apr 04 '14 at 20:34
  • @conmen What is the output of `echo ini_get('open_basedir');` running from PHP? – Ilia Ross Apr 04 '14 at 20:39
  • I've deleted my answer, yours is more complete :-) – Simon Groenewolt Apr 07 '14 at 08:20