Question

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?

Was it helpful?

Solution

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');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top