Jump to content

Variable passed to each() is not an array or object


AdRock

Recommended Posts

I am having a nightmare with a bit of code which should work.

 

I have 2 pages with the same form and when they are submitted, they call their own function; one for inserting into database, the other for updating the database.  Both functions call the same function for uploading and resizing images which works perfectly.

 

When i use the function to insert into the database, it calls the upload_image() function and all images are uploaded and resized correctly.  When i call the function to update the database, it calls the upload_image() function also but the images won't upload.  Instead i get these errors

[sun Feb 15 22:47:57 2009] [error] PHP Notice:  Undefined index:  images in d:\\Apache\\htdocs\\projects\\newblackdown\\admin\\php\\script.php on line 224

[sun Feb 15 22:47:57 2009] [error] PHP Warning:  Variable passed to each() is not an array or object in d:\\Apache\\htdocs\\projects\\newblackdown\\admin\\php\\script.php on line

226

Here are the functions

 

function content_update($id, $table, $content_title, $content, $keywords, $desc) {

global $host,$dbUser,$dbPass,$dbName;

require_once("../php/database/connection.php");
require_once("../php/database/MySQL.php");

$db = & new MySQL($host,$dbUser,$dbPass,$dbName);

$content_title = htmlentities($content_title);
$keywords = htmlentities($keywords);
$desc = str_replace("\n", " ", $desc);
$desc = htmlentities($desc);
$date = date("Y/m/d H:i");
if(!isset($user)) $user = $_SESSION['user'];

$uploaded = upload_image();

if($uploaded = "Uploaded") {
	$sql="UPDATE $table SET content='$content', keywords='$keywords', description='$desc', user='$user', date='$date' WHERE id='$id'";

	//query to enter form data into database 	    
	$result = $db->query($sql);	

	if(!$result) { 
		return 'There has been an error adding to the database.  Please consult Adam and advise of the problem'; 
	}
}
else {
	return 'There has been an error uploading the image.  Please consult Adam and advise of the problem';
}
}

function upload_image() {
$idir = "../images/";   // Path To Images Directory
$tdir = "../images/";   // Path To Thumbnails Directory
$twidth = "200";   // Maximum Width For Thumbnail Images
$theight = "150";   // Maximum Height For Thumbnail Images

$counter = 0;
$count = sizeof($_FILES['images']['name']);

while(list($key,$value) = each($_FILES['images']['name'])) {
	if(!empty($value)) {
		$filename = $value;
		$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line

		$add = $idir . $filename;

		$copy = copy($_FILES['images']['tmp_name'][$key], $add);
		if($copy) {
			$simg = imagecreatefromjpeg($add);   // Make A New Temporary Image To Create The Thumbanil From
			$currwidth = imagesx($simg);   // Current Image Width
			$currheight = imagesy($simg);   // Current Image Height
			if ($currheight > $currwidth) {   // If Height Is Greater Than Width
				$zoom = $twidth / $currheight;   // Length Ratio For Width
				$newheight = $theight;   // Height Is Equal To Max Height
				$newwidth = $currwidth * $zoom;   // Creates The New Width
			} else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
				$zoom = $twidth / $currwidth;   // Length Ratio For Height
				$newwidth = $twidth;   // Width Is Equal To Max Width
				$newheight = $currheight * $zoom;   // Creates The New Height
			}
			$dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail
			imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete
			$palsize = ImageColorsTotal($simg);
			for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image
				$colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used
				ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use
			}
			imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It)
			imagejpeg($dimg, "$tdir" . $filename);   // Saving The Image
			imagedestroy($simg);   // Destroying The Temporary Image
			imagedestroy($dimg);   // Destroying The Other Temporary Image

			$counter++;   // Resize successful

			if($counter == $count ) return 'Uploaded';
		}
		else {
			print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed
		}
	}
}
}

 

the form has these input fields

<?php
$max_imgs = 4;
for($i=1; $i<=$max_imgs; $i++){
	echo '<p><input type="file" name="images[]" class="bginput" /></p>';
}
?>

 

it makes no sense becuase the form is the same and they both call the same function so why one doesn't do it when the other does is beyond me

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.