Files cannot be uploaded


Symptoms

The FilesAction complains about not being able to sucesfully move_uploaded_file() after an upload, even though the upload directory has been created and has correct permissions:
Warning: move_uploaded_file() [function.move-uploaded-file]: SAFE MODE Restriction in effect. The script whose uid is 24822 is not allowed to access /uploads/SandBox owned by uid 99 in /actions/files.php on line 127
There was an error uploading your file.


Cause

PHP safe mode is activated, and the server and scripts run as different users.

Applies to


Version 1.1.6.0 (and very probably all earlier and future versions, since this is a "property" of the environment where the Wikka is run). This solution can only be applied when you have FTP acess to your website.

Solution


Instead of mkdir(), we have to create the directories using FTP. The following code was found in the comments to the mkdir() function on www.php.net.

Open the file actions/files.php and before the mkdir_r() function definition, add the following code:
// create directory through FTP connection
function FtpMkdir($path, $newDir) {
 
  $server='ftp.server.com';           // ftp server
  $connection = ftp_connect($server); // connection
 
 
  // login to ftp server
  $user = "me";
  $pass = "pass";
  $result = ftp_login($connection, $user, $pass);
 
  // check if connection was made
  if ((!$connection) || (!$result)) {
    return false;
    exit();
  } else {
      ftp_chdir($connection, $path);        // go to destination dir
      if(ftp_mkdir($connection, $newDir)) { // create directory
        ftp_site($connection, "CHMOD 775 $newDir") or die("FTP SITE CMD failed.");
        return $newDir;
      } else {
        return false;    
      }    
    ftp_close($connection); // close connection
  }  
}

Change the login information accordingly.

Now, find the following code (still in actions/files.php)
if (! function_exists('mkdir_r')) {
    function mkdir_r ($dir) {
        if (strlen($dir) == 0) return 0;
        if (is_dir($dir)) return 1;
        elseif (dirname($dir) == $dir) return 1;        
        return (mkdir_r(dirname($dir)) and mkdir($dir));
    }
}


and change it to
if (! function_exists('mkdir_r')) {
    function mkdir_r ($dir) {
        if (strlen($dir) == 0) return 0;
        if (is_dir($dir)) return 1;
        elseif (dirname($dir) == $dir) return 1;        
        return (mkdir_r(dirname($dir)) and FtpMkdir('/PathToWikkaDirOnFTP',$dir));
    }
}

(Note: Don't forget to change the '/PathToWikkaDirOnFTP' according to your set-up. I believe that a simple '/' will probably sufficient in the majority of cases)

Refinement

This could be refined so that a new FTP connection isn't created for every directory we're creating recursively. Also, the parameters could probably be placed in wikka.config.php but since this is a very specific fix for a very specific situation, I don't think it's worth the effort. And lastly, the FtpMkdir() function could be placed in a conditional block to prevent it from being redeclared (but how real that possibility is, I don't know).

CategoryWorkaround
There are 4 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki