Categories
PHP

Upload PDF script

This was a quick hack to upload a PDF to Clearer Conscience.

This script was a quick hack to allow a PDF to be uploaded to replace an outdated one on Clearer Conscience. As the PDF link on the site is hard-coded, the new PDF must have the same name as the old one.

You can only upload a PDF. The old PDF will be renamed (as a back-up), and the new PDF will be uploaded and renamed and will therefore replace the old one.

See demo page here…

<?php
session_start();

// max file upload size to be used in script in bytes - pls note upload_max_filesize setting in .ini still applies
define('FILE_SIZE_LIMIT', 1048576); // 1MB
define("UPLOAD_DIR", "pdf_uploads".DIRECTORY_SEPARATOR);

// initialise an empty array for messages
$msgArr = array();

	function checkFolderExists($path)
	{
		$mode = 0777;
		// make dir if it does not already exist
		if(!is_dir($path))
		{
			if(mkdir($path, $mode))
			{
				// the mode parameter seems not to work consistently on Windows boxes
				chmod($path, 0777);
				return true;
			}
		}
		return false;
	}

	function checkFileExists($filename)
	{
		if(is_file(UPLOAD_DIR.$filename))
		{
			return true;
		}
		return false;
	}
	
	if (isset($_POST['submit']))
	{
		// checks for errors and checks that file is uploaded
		if ($_FILES['uploadedfile']['error'] == UPLOAD_ERR_OK && is_uploaded_file($_FILES['uploadedfile']['tmp_name']))
		{
			// array will make it easier to add other files types later if we want to (csv,htm,html,xml,css,rtf etc)
			$allowedExtensions = array("pdf");
	
			// get the file extension
			$fileExtension = strtolower(substr(strrchr($_FILES['uploadedfile']['name'],'.'),1));
			
			// check the file type - this gets the MIME type - not necessarily the best way to check, esp. for image files!!
			$fileType = $_FILES['uploadedfile']['type'];
			
			// checking the MIME type and the extension
			if ($fileType == "application/pdf" && in_array($fileExtension, $allowedExtensions))
			{
				// get the file size
				$fileSize = $_FILES['uploadedfile']['size'];
				
				// check file size
				if($fileSize > 0)
				{
					if($fileSize <= FILE_SIZE_LIMIT)
					{
						checkFolderExists(UPLOAD_DIR); // if it does not exist, it is created
						// if easy.pdf exists, rename it...
						if(checkFileExists("easy.pdf"))
						{
							if(rename(UPLOAD_DIR."easy.pdf", UPLOAD_DIR."easy".time().".pdf"))
							{
								$msgArr[] = 'Old PDF backed up!';
							}
							else
							{
								$msgArr[] = 'Could not rename old PDF!';
							}
						}
						if(copy($_FILES['uploadedfile']['tmp_name'], UPLOAD_DIR."easy.pdf"))
									$msgArr[] = 'Success: new PDF uploaded!';
					}
					else
					{
						$msgArr[] = 'That file is too big (limit set at 1MB)!';
					}
				}
				else
				{
					$msgArr[] = 'That file is empty!';	
				}
			}
			else
			{
				$msgArr[] = 'That file-type ('.$fileExtension.') is not permitted!';	
			}			 
		}
		else
		{
			$msgArr[] = 'No file selected or there was an error uploading your file!';	
		}
	}
?>
<html>
<body>
<h2>Upload PDF</h2>
<p>This script was a quick hack to allow a PDF to be uploaded to replace an outdated one on Clearer Conscience. As the PDF link on the site is hardcoded, the new PDF must have the same name as the old one.</p>
<p>You can only upload a PDF. The old PDF will be renamed (as a back-up), and the new PDF will be uploaded and renamed and will therefore replace the old one.</p>
<?php
	if(!empty($msgArr)) // the array is empty - there were no errors
	{
		foreach($msgArr as $msg) // there were errors - output them
		{
			echo '<p class="error">'.$msg.'</p>';	
		}
	}
?>

<form enctype="multipart/form-data" action="upload_pdf.php" method="post">
<fieldset>	
	<legend>Upload PDF file:</legend>
	<p><label for="uploadedfile">File:</label><input type="file" name="uploadedfile" /> (limit 1MB)</p>
	<p><input name="submit" type="submit" value="submit" /></p>
</fieldset>	
</form>
</body>
</html>

 

By foxbeefly

PHP / MySQL Developer

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.