0

Hi, i found this image uploader in another post but i'm having a few problems with errors or upload confirmation not displaying at all and was wondering if someone could take a look.

original thread,the file is a zip near the bottom of the page

It seems the database code was disabled so i have made a few adjustments allowing me to upload the details to my own database.I dont think i have caused this with the changes i have made, this could be a possibility though so here is the file.

Attachments
<?php

require_once('../Connections/site.php');

// DATABASE CONNECTION PATH
/*** NOT PART OF THIS EXAMPLE ***
$db_login_path = 'access/db_login.php';
***/

// CLASS LIBRARY PATHS
/*** NOT PART OF THIS EXAMPLE ***
$db_image_path = 'db_image_path.php';
***/
$image_methods = 'image_methods.php';

// USER DEFINED DIRECTORY PATHS
$temp_path = 'images/temp';
$clean_path = 'images/gallery/clean';
$large_path = 'images/gallery/large';
$small_path = 'images/gallery/small';
make_paths();

// USER DEFINED IMAGE ATTRIBUTES
// image ratio 5:4
$large_width = 850;
$large_height = 600;
$small_width = 90;
$small_height = 60;
$min_width = 90;
$min_height = 60;

// image fill color
$fill_color = array( 71, 161, 195 ); // soft-blue RGB values
			
// HANDLE FILE UPLOAD
$submitted = $_REQUEST['submit'];
$image_name = $_REQUEST['name'];
$image_capt = $_REQUEST['capt'];

$temp_file = $_FILES['userfile']['tmp_name'];
$file_error = $_FILES['userfile']['error'];
$file_name = $_FILES['userfile']['name'];

$temp_file_path = "$temp_path/$file_name";
$clean_file_path = "$clean_path/$file_name";
$large_file_path = "$large_path/$file_name";
$small_file_path = "$small_path/$file_name";

$exists = is_file( $clean_file_path );

$errors = array();

if ( !$exists && is_uploaded_file( $temp_file ) && move_uploaded_file( $temp_file, $temp_file_path ) ) {
	// TRANSFORM UPLOADED IMAGE
	require_once $image_methods;
	$image = new Image_Methods();
	$image->load_path ( $temp_file_path );
	if ( $image->type ) {
		// ensure large enough source image
		if ( $min_width <= $image->width && $min_height <= $image->height ) {
      $aspect = ( $image->width > $image->height ) ? 'wide' : 'tall';
			// make all images constant-sized width and height
			// and don't lose any of the actual image
			switch ( $aspect ) {
				case 'wide': 
				$image->scale_width( $large_width ) or $errors[] = "Could not scale uploaded image to large size";
				$image->trim_fill_height( $large_height, TRUE, $fill_color ) or $errors[] = "Could not size uploaded image to large size";
				$image->publish( 'jpg', $large_file_path, 8.8 ) or $errors[] = "Could not scale uploaded image to small size";
				$image->scale_width( $small_width ) or $errors[] = "Could not publish uploaded image to images directory";
				$image->trim_fill_height( $small_height, TRUE, $fill_color ) or $errors[] = "Could not size uploaded image to small size";
				$image->publish( 'jpg', $small_file_path, 8.8 ) or $errors[] = "Could not publish uploaded image to images directory";
				break;
				case 'tall':
				$image->scale_height( $large_height ) or $errors[] = "Could not scale uploaded image to large size";
				$image->trim_fill_width( $large_width, $fill_color ) or $errors[] = "Could not size uploaded image to large size";
				$image->publish( 'jpg', $large_file_path, 8.8 ) or $errors[] = "Could not publish uploaded image to images directory";
				$image->scale_height( $small_height ) or $errors[] = "Could not scale uploaded image to small size";
				$image->trim_fill_width( $small_width, $fill_color ) or $errors[] = "Could not size uploaded image to small size";
				$image->publish( 'jpg', $small_file_path, 8.8 ) or $errors[] = "Could not publish uploaded image to images directory";
			}
			
		mysql_select_db($database_site, $site);
$query ="INSERT INTO gallery (image, image_thumb, description) VALUES
('$large_file_path','$small_file_path','$image_capt')"; 
$nav_links = mysql_query($query, $site) or $errors[] = "Could not upload images to database";
			
		} 
		else {
		  $errors[] = "Uploaded image width or height are too small, please upload a larger version";
			$errors[] = "Minimum width: $min_width pixels<br />Minimum height: $min_height pixels";
		}
	} else {
		$errors[] = "File is not a standard web-image file-type";
		$errors[] = "Allowed types: gif, jpg, png";
	}
	
	if ( empty( $errors ) ) {
	  copy( $temp_file_path, $clean_file_path ) or $errors[] = "Could not move uploaded image to clean images directory";
	}
  @unlink( $temp_file_path );
} elseif ( $submitted ) {
  if ( empty( $_FILES ) || $file_error == 2 ) { // oversized file
	  $errors[] = "Image exceeded maximum size of 1Mb";
		$errors[] = "1Mb (megabyte) is 1024Kb (kilobytes) or 1048576 bytes";
	} elseif ( $file_error == 4 ) { // no file specified
	  $errors[] = "Please select a file to upload from your local computer";
	} elseif ( $exists ) {
	  $errors[] = "The file you are trying to upload already exists on the server";
		$errors[] = "Please change the <em>file-name</em> of the file you are trying to upload or delete the existing one";
	} else {
	  $errors[] = "Could not locate or move uploaded image file (Error: $file_error)";
	}
}

// HANDLE UPLOAD RESPONSE MESSAGE
if ( $submitted ) {
	if ( empty( $errors ) ) {
		$class = 'pass-light';
		$output = 'Image successfully uploaded';
	} else {
		$class = 'fail-light';
		$output = 'There was a problem loading your image';
		foreach ( $errors as $error ) {
			$output .= "<p class=\"error\">$error</p>";
		}
	}
}

function make_paths () {
  global $temp_path, $clean_path, $large_path, $small_path;
	if ( !is_dir( $temp_path ) ) {
		mkdir( $temp_path, 0777, TRUE ) 
			or die( "Could not resolve temp files directory path" );
	}
	if ( !is_dir( $large_path ) ) {
		mkdir( $large_path, 0777, TRUE ) 
			or die( "Could not resolve large images directory path" );
	}
	if ( !is_dir( $small_path ) ) {
		mkdir( $small_path, 0777, TRUE ) 
			or die( "Could not resolve small images directory path" );
	}
	if ( !is_dir( $clean_path ) ) {
		mkdir( $clean_path, 0777, TRUE ) 
			or die( "Could not resolve clean images directory path" );
	}
	return TRUE;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image Upload</title>
<style type="text/css">

/* COLOR CLASSES */

.green-light {
  background: #EAFFD9;
}

.warn-light {
  background: #FFDFDA; /* pink */
}

.pass-light {
  color: #45BD6D; /* darker green */
}

.fail-light {
  color: #E86755; /* darker pink */
}

/* GENERAL CLASSES */

.left {
  text-align: left;
}

.right {
  text-align: right;
}

.form-font {
  font-size: 12px;
  font-family: "Courier New", Georgia;
}

.error {
  width: 300px;
  margin: 10px auto;
  text-align: left;
  font-weight: normal;
}

/* TAG STYLES */

body {
  text-align: center;
  padding: 50px;
  background: #FFF;
}


label {
  font-size: 14px;
  font-variant: small-caps;
  font-family: Arial;
}

table {
  margin: 0 auto;
  padding: 10px 20px 20px;
  text-align: center;
  background: silver;
  border: 1px solid indigo;
}

 /* UNIQUE ID STYLES */
 
#file {
  width: 250px;
}

#name {
  width: 245px;
}

#capt {
  width: 245px;
  height: 90px;
  overflow: auto;
}

#output {
  font-weight: bold;
  font-family: Arial;
  text-align: center;
  font-size:18px;
}

</style>
<script type="text/javascript">

window.onload = function () {

	var name_msg = 'Image Label [suggested]';
	var capt_msg = 'Image Caption [optional]';
	
  var name = document.getElementById('name');
	var capt = document.getElementById('capt');

	name.style.color = '#999';
	capt.style.color = '#999';

	name.value = name_msg;
	capt.value = capt_msg;
	
	name.onblur = function () {
		if ( !this.value ) {
			this.value = name_msg;
			this.style.color = '#999';
		}
	};
	name.onfocus = function () {
		if ( this.value == name_msg ) {
			this.value = '';
			this.style.color = '#000';
		}
	};
	capt.onblur = function () {
		if ( !this.value ) {
			this.value = capt_msg;
			this.style.color = '#999';
		}
	};
	capt.onfocus = function () {
		if ( this.value == capt_msg ) {
			this.value = '';
			this.style.color = '#000';
		}
	};
};

function populate_submit () {
  var form = document.getElementById('image_upload');
	form.action = form.action + '?submit=true';
}

</script>
</head>
<body>
<form id="image_upload" action="image_upload.php" method="POST" enctype="multipart/form-data">
  <input type="hidden" name="MAX_FILE_SIZE" value="1048576" /><!-- 1MB in bytes -->
  <table>
    <tr>
      <td class="right">&nbsp;</td><!-- There used to be nav buttons here -->
    </tr>
    <tr>
      <td><input id="name" class="form-font green-light" name="name" type="text" /></td>
    </tr>
    <tr>
      <td><textarea id="capt" class="form-font green-light" name="capt"></textarea></td>
    </tr>
    <tr>
      <td><input id="file" class="green-light" name="userfile" type="file" /></td>
    </tr>
    <tr>
      <td class="left"><input type="submit" value="Upload Image" onclick="populate_submit()" /></td>
    </tr>
   </table>
</form>
<span id="output" class="<? print $class ?>"><? print $output ?> </span>
</body>
</html>
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by cossay
0

What are the errors ? … and to be at the root of things … what are you trying to do and how are you planning to do them ? … tell us and I will give you feed back in how to do

0

If you all you want to do is to upload an image, then try this simple one.

<?php
//If user has not clicked the upload button, display the upload form
if(!$_POST['upload'])
{
?>
<form method = "post" enctype = "multipart/form-data" action = "upload.php">
<table width = "400" align = "center">
	<tr>
		<input type = "hidden" name = "MAX_FILE_SIZE" value = "120000">
		<td>Picture</td>
		<td><input type = "file" name = "myimage"></td>
	</tr>
	<tr>
		<td></td>
		<td><input type = "submit" name = "upload" value = "UPLOAD"></td>
	</tr>
</table>
</form>
<?php
}
else
{
	//If has clicked the upload button, checked for errors. I like to keep errors in an array 
	//and echo all of them to the user at once as I have done here. You decide how to handle 
	//errors yourself.
	
	if($_FILES['myimage']['error'] == UPLOAD_ERR_INI_SIZE || $_FILES['pic']['error'] == UPLOAD_ERR_FORM_SIZE)
	{
		$my_errors[] = "Picture too large for upload.";
	}
	
	if($_FILES['myimage']['error'] == UPLOAD_ERR_NO_FILE)
	{
		$my_errors[] = "No picture file specified.";
	}
	
	if($_FILES['myimage']['error'] == UPLOAD_ERR_PARTIAL)
	{
		$my_errors[] = "image upload was interrupted.";
	}
	
	if(sizeof($my_errors) > 0)
	{	
		//At least one error occurred so print the errors
		
		echo "File upload failed because of the following error(s) :";
		echo "<ol>";
		for($counter = 0; $counter < sizeof($my_errors); $counter++)
		{
			echo "<li>".$my_errors[$counter]."</li>";
		}
		echo "</ol>";
	}
	else
	{	
		//No errors were found, upload file to the approprite directory
		$destination = "where_you_want_to_store_the_file/".$_FILES['myimage']['name'];
		move_uploaded_file($_FILES['myimage']['tmp_name'], $destination);
		echo "Picture successfully uploaded";
	
	}
	
}
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.