Location: PHPKode > projects > Kwalbum > kwalbum/pages/UploadItemsPage.php
<?php
/**
 * Allow users with "add item" permission to upload files from the web browser.
 *
 * NOTE: adding items already on the server is an admin option so it is under
 * admin tools
 *
 * @package kwalbum
 * @version 2.1.2
 * @since 2.0
 */
ini_set("session.gc_maxlifetime","10800");
require_once 'include/IPage.php';

/**
 * ?p=UploadItems
 *
 * @package kwalbum
 * @version 2.1.1
 * @since 2.0
 */
class UploadItemsPage implements IPage
{
	private $error;

	function GetHead(& $title)
	{
		$title = 'Add Items';
		if (!USER_CAN_ADD)
			return '';

		return '<script type="text/javascript" language="javascript" src="ajax/uploadItems.js"></script>'.
				'<script type="text/javascript" language="javascript" SRC="ajax/scriptaculous/prototype.js"></script>'.
				'<script type="text/javascript" language="javascript" src="ajax/scriptaculous/scriptaculous.js"></script>'.
				'<link href="styles/edit.css" rel="stylesheet" type="text/css" />';
	}

	function ShowBody()
	{
		if (!USER_CAN_ADD)
		{
			echo 'You do not have permission to upload items.';
			return;
		}

		global $G, $P, $DB, $URLP;

		$hidden = (int)@$P['hidden'];
		$location = trim(@$G['loc']);
		$tags = trim(@$G['tags']);
		if (!$date = @$G['date'])
			$date = date('Y-m-d');

		if (isset ($P['act']))
		{
			require_once 'include/ItemAdder.php';
			echo "Adding items to database and creating resized and thumbnail versions of any pictures...<br/><br/>";
			flush();

			$IA = new ItemAdder();
			if (!USER_IS_ADMIN)
				$max = 2;
			else
				$max = 3;
			if ($hidden > $max)
				$hidden = $max;
			elseif ($hidden < 0)
				$hidden = 0;
			$location = trim($DB->FilterString($P['loc']));
			if (!$location)
				$location = 'unknown location';
			$tags = trim($DB->FilterString($P['tags']));
			$date = $IA->ReplaceBadDate($P['date']);

			if ($path = $IA->MakePath())
			{
				$errors = false;
				ksort($_FILES);
				foreach ($_FILES as $key => $file)
				{
					if ($file['size'] != 0)
					{
						// filename
						$oldFilename = $file['name'];
						$file['name'] = $IA->ReplaceBadFilenameChars($file['name']);

						$pos = strrpos($file['name'], '.');
						$filename = substr($file['name'], 0, $pos);
						$ext = strtolower(strrchr($file['name'], '.'));
						if ($ext == '.jpeg' or $ext == '.jpe')
							$ext = '.jpg';
						if ($ext == '.htm')
							$ext = '.html';

						if (!$IA->FilterFileType($ext))
						{
							echo '<p class="error">'.$filename.$ext.' is of a filetype that is not yet supported</p>';
							continue;
						}

						if (file_exists($path . $filename . $ext))
						{
							$fnum = 0;
							while (file_exists($path . $filename . '_' . $fnum . $ext))
								$fnum++;
							$filename .= '_' . $fnum;
						}
						$filename .= $ext;

						if (!move_uploaded_file($file['tmp_name'], $path.$filename))
						{
							echo "<span class='error'>$file[name] could not be saved to $path</span><br/>";
							flush();
						}
						else
						{
							chmod($path.$filename, 0664);

							if ($newId = $IA->AddItem($path, $filename, $location, $tags, $date, $hidden))
							{
								echo $oldFilename." uploaded and saved as <a href='".PAGE_URL."i=$newId'>$filename</a><br/>";
								flush();
							}
							else
							{
								unlink($path.$filename);
								$errors = true;
								break;
							}
						}
					}
				}
				if (!$errors)
					echo '<br/><b>Finished Uploading!</b><br/><br/>';
			}
			if (!isset($P['overLimit']))
				echo '<span class="error">Upload was over the allowed limit.  No files were uploaded.</span>';
		}

		require 'UploadItemsPage.html.php';
	}
}
?>
Return current item: Kwalbum