...

View Full Version : undefined call to function error



nickyfraggle
12-09-2008, 06:10 PM
Hello,

This is my php code:


case "image":
//get all posted values
$image=$_POST['picture']; $id=$_POST['id'];
$make=$_POST['make']; $model=$_POST['model'];

$uploadDir = '/home/sites/test.co.uk/public_html/images/';

if(isset($_POST['upload'])) {
$fileName = $_FILES['picture']['name'];
$tmpName = $_FILES['picture']['tmp_name'];

$filePath = $uploadDir.$fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
//echo "Error uploading file";
}

if(!get_magic_quotes_gpc()){
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$insert=mysql_query("UPDATE cars SET image='$image' WHERE id='$id'", $link) or die("Update Query Error ".mysql_error());

}
if($result === true) {
$picture = open_image($uploadDir.$fileName);
if ($picture === false) { }
else{

function open_image($file) {
# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }

# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }

# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }

return false;
}
// Get original width and height
$width = imagesx($image);
$height = imagesy($image);

// Set a new width, and calculate new height
$new_width = 100;
$new_height = $height * ($new_width/$width);

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

imagejpeg($image_resized, '../../car_images/'.$fileName);
$url="success.php";
header ("Location: $url");

}
}

I'm getting this error :

Fatal error: Call to undefined function: open_image() in /home/sites/test.co.uk/public_html/functions.php on line 123

Any idea why this might be? I''m pulling my hair out and I don't understand it!!

Thanks,

Nicky

Fumigator
12-09-2008, 06:30 PM
Since your function is inside the "if" block, it is a conditional function, and does not exist until that "if" condition is run through. In other words, your script has not created the function yet, because it may never get created; If the "if" condition is true, that function will never exist.

Look at Example #2 on this page of the PHP manual:

http://us.php.net/manual/en/language.functions.php

nickyfraggle
12-09-2008, 07:50 PM
Ok, I see your point. I've removed all the if statements that would effect it and I'm still getting the same error.

Any ideas?

I'm using similar code elsewhere too and it isn't working either - without the if statements.

Nicola



$image=$_POST['picture']; $carid=$_POST['carid'];
$make=$_POST['make']; $model=$_POST['model'];

$uploadDir = '/home/sites/test.co.uk/public_html/images/';


$fileName = $_FILES['picture']['name'];
$tmpName = $_FILES['picture']['tmp_name'];

$filePath = $uploadDir.$fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
//echo "Error uploading file";
}

if(!get_magic_quotes_gpc()){
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$insert=mysql_query("UPDATE cars SET image='$image' WHERE carID='$carid'", $link) or die("Update Query Error ".mysql_error());


$picture = open_image($uploadDir.$fileName);

function open_image($file) {
# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }

# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }

# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }

return false;
}
// Get original width and height
$width = imagesx($image);
$height = imagesy($image);

// Set a new width, and calculate new height
$new_width = 100;
$new_height = $height * ($new_width/$width);

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

imagejpeg($image_resized, '../../images/'.$fileName);
$url="success.php";
header ("Location: $url");

oesxyl
12-09-2008, 08:41 PM
move this line:


$picture = open_image($uploadDir.$fileName);

after function definition:



function open_image($file) {
# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }

# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }

# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }

return false;
}


it's a good idea to define function at the begining of the file, before you use them or to move them into a separate file which will be included where you need.

best regards

Fumigator
12-09-2008, 09:01 PM
That function must still be within an "if" block, outside the code you're showing. Just do like what oesxyl suggests; put your functions all in one place rather than "inline" like that.

nickyfraggle
12-09-2008, 09:06 PM
Oh I understand now! I see where I was going wrong - blonde moment!!
Thank you both for your help!

Nicky



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum