Password-protected image upload

Soldato
Joined
18 Oct 2002
Posts
8,016
Hi all,

I'm on the lookout for a basic script/page where I can have a page for uploading images to a website (by the person I'm writing it for), into a single directory, but obviously password-protected so that only that person can upload an image.

I'm not really sure what search terms to use, and what exactly I'm looking for, so if anyone knows of anything, it'd be greatly appreciated.
 
So you need a script to just have a page that allows you to browse for an image, then uploads it to the server but it's password protected so not anyone can upload?

Will PHP do?
 
Last edited:
Here's something I made earlier ;)

Wrote this ages ago and cleaned it up a little. It could probably be written better, but it works fine for what I need (when I do use it). It allows you to upload a file to a server and to a particular directory which is set on the upload page.

Open index.php and check the first few lines which are:

PHP:
# Variables
$password = "password";
$include_thumb = 1;
$archiveArr = array("folder1/", 
					"this/is/multi/folder/test/",
					"blah",
					"more folders/",
					"../test me/");

$password is the password to upload with, $include_thumb when set to 1 creates a thumbnail of the image you are uploading as well as creating the main image itself, and $archiveArr is the array for holding the folder names (note you can use ../ to go back a directory).

In case anyone does want to clean it up, the code is:

PHP:
<?php

# Variables
$password = "password";
$include_thumb = 1;
$archiveArr = array("folder1/", 
					"this/is/multi/folder/test/",
					"blah",
					"more folders/",
					"../test me/");

if ( $_POST["submit"] ) {

	# Checks to see if the password is correct
	if ( $_POST["pass"] <> $password ) {
		$note = 'Incorrect password.';
	}
	
	# Checks to see if the folder specified actually exists
	if ( !is_dir($_POST["sltArchive"]) ) {
		$note = 'The folder specified does not exist.';
	}
	
	# Checks to make sure that the last character in the folder name is a /
	if ( substr($_POST["sltArchive"],strlen($_POST["sltArchive"]),-1) <> "/" ) {
		$_POST["sltArchive"] = $_POST["sltArchive"] . "/";
	}
	
	# Checks to make sure the folder is writeable
	if ( !is_writeable($_POST["sltArchive"]) ) { 
		$note = 'The folder cannot be written too';
	}
	
	# Sets directory and image names for upload
	$img_tmp_name = $_POST["sltArchive"] . date("m.d.y h.i.s") . " - " . $_FILES["img1"]["name"];
	$img_tmp_name_th = $_POST["sltArchive"] . date("m.d.y h.i.s") . " - th_" . $_FILES["img1"]["name"];
	
	# Checks to see if the image is already on the server
	if ( $_FILES["img1"]["name"] <> "" && file_exists($img_tmp_name) ) {
		$note = 'This image already exists on the server.';
	}

	# Checks if the image has no name
	if ( $_FILES["img1"]["name"] == "" ) {
		$note = 'The image supplied has no name.';
	}

	# Checks if the image is not a JPEG file
	if ( ! ( $_FILES["img1"]["type"] == "image/jpeg" || $_FILES["img1"]["type"] == "image/pjpeg" ) ) {
		$note = 'The image supplied is not JPEG.';
	}
	
	# Sets the quality of the image and the the quality of a thumbnail
	$quality = 90;
	$quality_th = 100;
	$thumb_size = 0.25;
		 
	if ( !isset($note) ) {
		if ( move_uploaded_file($_FILES["img1"]["tmp_name"], $img_tmp_name) ) {
			
			# Creates the core image
			$im = imagecreatefromjpeg($img_tmp_name);
			$image_x = imageSX($im);
			$image_y = imageSY($im);
			$im2 = imagecreatetruecolor($image_x,$image_y);
			imagecopyresampled($im2,$im,0,0,0,0,$image_x,$image_y,$image_x,$image_y);
			imagejpeg($im2, $img_tmp_name, $quality);
               
			# Checks if we can create a thumbnail, and do if so
			if ( $include_thumb == 1 ) {
				$im2 = imagecreatetruecolor(($image_x * $thumb_size), ($image_y * $thumb_size));
				imagecopyresampled($im2,$im,0,0,0,0,($image_x * $thumb_size),($image_y * $thumb_size),$image_x,$image_y);
				imagejpeg($im2, $img_tmp_name_th, $quality_th);
				imagedestroy($im2);
			}

			imagedestroy($im);
			$note = 'Image uploaded sucessfully';
		}
	}
}

if ( isset($note) ) {
	echo '<span style="color: red; font-weight: bold;">' . $note . '<br /><br />';
}
?>

<form name="frmAddImg" method="post" enctype="multipart/form-data">
	<table cellpadding="2" cellspacing="2" border="0">
		<tr>
			<td><b>Image</b></td>
			<td><input type="file" size="30" name="img1" accept="image/jpeg"></td>
		</tr>

		<tr>
			<td><b>Password</b></td>
			<td><input type="password" size="30" name="pass"></td>
		</tr>
		<tr>
			<td><b>Archive</b></td>
			<td><select name="sltArchive" style="cursor:pointer;cursor:hand;">
			<?php

				for ( $x = 0; $x < count($archiveArr); $x++ ) {
						echo "<option value=\"" . $archiveArr[$x] . "\">" . $archiveArr[$x] . "</option>\n";
				}
			?>
			</select></td>
	</table>

	<br />

	<table cellpadding="2" cellspacing="2" border="0" class="main">
		<tr>
			<td><input type="submit" name="submit" value="Upload" style="font-weight: bold;cursor:pointer;cursor:hand;"></td>
		</tr>
	</table>
</form>
 
Last edited:
just tried it myself as I am after something similar and got:

Fatal error: Call to undefined function imagecreatefromjpeg() in C:\wamproot\phptest\html\password-uploader.php on line 70

(am trying it on wampserver at work the moment, not yet tried it on my proper php setup at home)

Missing library file with that function in it?
 
just tried it myself as I am after something similar and got:



(am trying it on wampserver at work the moment, not yet tried it on my proper php setup at home)

Missing library file with that function in it?

you haven't got GD enabled by the look of it. check your php.ini and look for this.....

Code:
;extension=php_gd2.dll

remove the ; to uncomment it, restart apache and try again. :)
 
Back
Top Bottom