Problem with upload script and CHMOD's

Discussion in 'PHP' started by petershaman, Nov 15, 2008.

  1. #1
    Hi
    I have a gorgeous flash image gallery, it would work perfectly fine but there's a small problem I can't upload anything thru it's upload script, since I'm getting an error 403. It's so because server sets by default CHMOD 600, whilst gallery needs to be set 644 to operate correctly.

    In this upload php script there is a command which theoretically should change permissions for uploaded files, but it's not doing the job, there's obviously some bug over there, and here's my question, could you be so kind and take a glimpse at this code, and tell me how to shrink it? Cheers

    	/**
    	 * Upload file
    	 *
    	 */
    	function uploadAction()
    	{
    		$this->_setNoRender();
    		if (!isset($_FILES['Filedata'])) {
    			header("HTTP/1.1 500 Internal Server Error");
    			echo "Error. File not found";
    			return;
    		}
    		$imageData = $_FILES['Filedata'];
    		if (!ivFilepath::matchSuffix($imageData['name'], $this->conf->get('/config/settings/allowedExtentionsArr'))) {
    			header("HTTP/1.1 403 Forbidden");
    			echo "Error. Wrong extention";
    		} else {
    			$fullpath = ROOT_DIR . $this->path . $imageData['name'];
    			$result = @move_uploaded_file($imageData['tmp_name'], $fullpath);
    			if ($result) {
    				chmod($fullpath, 0777);
    				$FSItem = ivFSItem::create($fullpath);
    				$FSItem->generateThumb();
    				$filename = ROOT_DIR . $this->path . 'folderdata.xml';
    				if (is_file($filename)) {
    					$file = file_get_contents($filename);
    					$file = preg_replace('/\s*fileCount=\"\d*\"\s*/i', ' ', $file);
    					$result = @file_put_contents($filename, $file);
    				}
    				echo "File {$imageData['name']} succesfully uploaded";
    			} else {
    				header("HTTP/1.1 500 Internal Server Error");
    				echo "Error. File {$imageData['name']} wasn't uploaded";
    			}
    		}
    	}
    
    PHP:
     
    petershaman, Nov 15, 2008 IP
  2. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    webserver needs permission to chmod().. so try making everything in the target directory and below owned by the web-server;

    cd /path/to
    chown -R webserverunixuser:webserverunixgroup targetDir
     
    rene7705, Nov 15, 2008 IP
  3. petershaman

    petershaman Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I've set 777 for folders and 644 for files and doesn't work at all. Interesting thing is that I have installed the same gallery on the other server and everything works perfectly fine over there…

    This server sets 600 on upload for files, whilst the other one sets 644. I've had those problems before, but resolved using this:

    $copy = @move_uploaded_file($tmp_name,'../'.$path.$file_name);
    @chmod('../'.$path.$file_name,644);
    PHP:
    That was an older version of this gallery, but in the new version they changed upload script (as shown in the first post), and this doesn't seems to be working anymore… :/
     
    petershaman, Nov 15, 2008 IP