Location: PHPKode > projects > MyPHPBib > bib_upload.php
<?php
require_once ('login/user.inc.php');
include ('phpbib/phpbib.inc.php');
include ('header.inc.php'); 
?>

<body>

<?php include ('top.inc.php'); ?>
<?php $user->MembersOnly(); ?>

<div id="content"> 

<?php
	if (($_SERVER['REQUEST_METHOD']=='POST'))
	{

		global $config;

		global $HTTP_POST_FILES;

		// Get details of uploaded file
		$name = $HTTP_POST_FILES['file']['name'];
		$source = $HTTP_POST_FILES['file']['tmp_name'];
		$size = $HTTP_POST_FILES['file']['size'];
		$type = $HTTP_POST_FILES['file']['type'];
		
		// Extension
		$path_parts = pathinfo($name);
		$extension = $path_parts["extension"];

		if ($size == 0)
		{
					$html = "<script language=\"JavaScript\">";
					$html .= "alert('There was a problem uploading the file \"$name\". The file size might be larger than the setting for \"upload_max_filesize\" in the php.ini file for this Web site.');";
					$html .= "document.location = 'bib_display_reference.php?id=";
					$html .= $_POST['id'];
					$html .= "&format=report';";				
					$html .= "</script>";
					echo $html;
		}
		
		//echo "<p>file &quot;$name&quot; ($size bytes) is stored at $source</p>";

		// Bill wants to
		// 1. Rename file ref_id.extension

		//echo "Reference id = ", $_POST['id'], "<br/>";
		// 2. If a file with this name already exists in upload dir,
		//    rename it to move it out of the way


		// 1. Uploaded file is given a file name that matches the id
		// of the corresponding reference (with the extension appended).
		$ref_id = $_POST['id'];
		$filename = $config['upload_dir'] . "/" . $ref_id . "." . $extension;

		// 2. If a user has already uploaded a file of this type for this reference,
		// move it out of the way
		if (file_exists($filename))
		{
				// A file already exists, so move it out of the way and give it a new
				// name so we can still find it
				//echo "<b>File exists</b><br/>";

				$count = 1;
				while (file_exists($config['upload_dir'] . "/" . $ref_id . "." . $count . "." . $extension))
				{
					$count++;
				}
				//echo "Count=", $count, "<br/>";

				// Make backup of current file
				rename ($filename, $config['upload_dir'] . "/" . $ref_id . "." . $count . "." . $extension);
		}
		
		// 3. Move uploaded file to upload dir
		move_uploaded_file ($source, $filename);

		
		// 4. If a URL exists for this reference, move it to a note
		// so that we still have a record of that URL
		$r = new Reference();

		//echo "User id=", $user->GetUserId(), "<br/>";

		$r->MoveURLToNotes($ref_id, $user->GetUserId());


		// 5. Create a URL for the uploaded file

		$url = $config['base_url'] . "/" . $config['upload_url'] . "/" . $ref_id . "." . $extension;
		//echo "<a href=\"", $url, "\">", $ref_id . "." . $extension, "</a>";

		$r->SetURL($ref_id, $url);

				$html = "<script language=\"JavaScript\">";
				$html .= "alert('The file \"$name\" has been uploaded and can be viewed by clicking on the URL link for this reference.');";
				$html .= "document.location = 'bib_display_reference.php?id=";
				$html .= $_POST['id'];
				$html .= "&format=report';";				
				$html .= "</script>";
				echo $html;


						
	}

?>

 </div>

<?php include ('footer.inc.php'); ?>


</body>
</html>

Return current item: MyPHPBib