Jump to content

Recommended Posts

I get this error: Fatal error: Call to undefined function uploadphoto() in ....... on line 39

 

I use phpFlickr to upload my photos from my website to Flickr.

Here's the code

 

<?php

include("includes/header.php");

if($_SESSION['usernr'] != true){

?>
<section id="content" class="body">
<h1>Sorry!</h1>
You are not authorized to view this page!
</section>
<?php

}else{

//Include phpFlickr
require_once("phpFlickr/phpFlickr.php");

$error=0;
$f = null;
if($_POST){
if(!$_POST['name'] || !$_FILES["file"]["name"]){
$error=1;
}else{
if ($_FILES["file"]["error"] > 0){
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}else if($_FILES["file"]["type"] != "image/jpg" && $_FILES["file"]["type"] != "image/jpeg" && $_FILES["file"]["type"] != "image/png" && $_FILES["file"]["type"] != "image/gif"){
$error = 3;
}else if(intval($_FILES["file"]["size"]) > 525000){
$error = 4;
}else{
$dir= dirname($_FILES["file"]["tmp_name"]);
$newpath=$dir."/".$_FILES["file"]["name"];
rename($_FILES["file"]["tmp_name"],$newpath);
//Instantiate phpFlickr
$status = uploadPhoto($newpath, $_POST["name"]);
if(!$status) {
$error = 2;
}
}
}
}

function uploadPhoto($path, $title) {
$apiKey = "*****************";
$apiSecret = "*****************";
$permissions = "write";
$token = "*****************";

$f = new phpFlickr($apiKey, $apiSecret, true);
$f->setToken($token);
return $f->async_upload($path, $title);
}
?>

<section id="content" class="body">
<div id="doc" class="yui-t7">
<div id="hd" role="banner"><h1>Foto Uploader</h1></div>
<div id="bd" role="main">
<div id="mainbar" class="yui-b">
<?php

if (isset($_POST['name']) && $error==0) {
echo " <h2>Your file is uploaded to <a href='http://www.flickr.com/photos/78262663@N02/' target='_blank'>DK1885 his photos!</a></h2>";
}else {
if($error == 1){
echo " <font color='red'>Please enter a file name.</font>";
}else if($error == 2) {
echo " <font color='red'>Your photo can't be uploaded.</font>";
}else if($error == 3){
echo " <font color='red'>Please only upload JPG, JPEG, PNG or GIF files.</font>";
}else if($error == 4){
echo " <font color='red'>The file size is larger than 512Kb.</font>";
}
?>
<h2>Upload your photos!</h2>
<form method="post" accept-charset="utf-8" enctype='multipart/form-data'>
<p>Name:   <input type="text" name="name" value="" ></p>
<p>Picture: <input type="file" name="file"/></p>
<p><input type="submit" value="Submit"></p>
</form>
<?php
}
?>
</div>
</div>
<div id="ft"></div>
</div>
</section>

<?php
}
include("includes/footer.php");

?>

Link to comment
https://forums.phpfreaks.com/topic/260258-fatal-error/
Share on other sites

Your function definition is inside an ELSE clause. PHP will NOT define that function until that section of code executes (since the function definition is conditional).  Move the function definition outside of your IF clause (recommended), or move the call to after the definition.

 

Also, indent your code, so you can easily see things like that.

 

Link to comment
https://forums.phpfreaks.com/topic/260258-fatal-error/#findComment-1333965
Share on other sites

Your function definition is inside an ELSE clause. PHP will NOT define that function until that section of code executes (since the function definition is conditional).  Move the function definition outside of your IF clause (recommended), or move the call to after the definition.

 

Also, indent your code, so you can easily see things like that.

 

The code is indented on my host, but it didn't do the indentation when i copied it over.

That pretty much solves the issue.

Thanks mate!

 

<?php
include("includes/header.php");

function uploadPhoto($path, $title) {
	$apiKey = "******";
	$apiSecret = "******";
	$permissions = "write";
	$token = "******";

	$f = new phpFlickr($apiKey, $apiSecret, true);
	$f->setToken($token);
	return $f->async_upload($path, $title);
}

if($_SESSION['usernr'] != true){
?>
<section id="content" class="body">
<h1>Sorry!</h1>
You are not authorized to view this page!
</section>
<?php
}else{

//Include phpFlickr
require_once("phpFlickr/phpFlickr.php");

$error=0;
$f = null;
if($_POST){
if(!$_POST['name'] || !$_FILES["file"]["name"]){
	$error=1;
}else{
	if ($_FILES["file"]["error"] > 0){
		echo "Error: " . $_FILES["file"]["error"] . "<br />";
	}else if($_FILES["file"]["type"] != "image/jpg" && $_FILES["file"]["type"] != "image/jpeg" && $_FILES["file"]["type"] != "image/png" && $_FILES["file"]["type"] != "image/gif"){
		$error = 3;
	}else if(intval($_FILES["file"]["size"]) > 525000){
		$error = 4;
	}else{
		$dir= dirname($_FILES["file"]["tmp_name"]);
		$newpath=$dir."/".$_FILES["file"]["name"];
		rename($_FILES["file"]["tmp_name"],$newpath);
		//Instantiate phpFlickr
		$status = uploadPhoto($newpath, $_POST["name"]);
		if(!$status) {
		$error = 2;
		}
	}
}
}
?>

<section id="content" class="body">
<div id="doc" class="yui-t7">
<div id="hd" role="banner"><h1>Foto Uploader</h1></div>
<div id="bd" role="main">
<div id="mainbar" class="yui-b">
<?php

if (isset($_POST['name']) && $error==0) {
echo " <h2>Your file is uploaded to <a href='http://www.flickr.com/photos/78262663@N02/' target='_blank'>DK1885 his photos!</a></h2>";
}else {
if($error == 1){
	echo " <font color='red'>Please enter a file name.</font>";
}else if($error == 2) {
	echo " <font color='red'>Your photo can't be uploaded.</font>";
}else if($error == 3){
	echo " <font color='red'>Please only upload JPG, JPEG, PNG or GIF files.</font>";
}else if($error == 4){
	echo " <font color='red'>The file size is larger than 512Kb.</font>";
}
?>
<h2>Upload your photos!</h2>
<form method="post" accept-charset="utf-8" enctype='multipart/form-data'>
<p>Name:   <input type="text" name="name" value="" ></p>
<p>Picture: <input type="file" name="file"/></p>
<p><input type="submit" value="Submit"></p>
</form>
<?php
}
?>
</div>
</div>
<div id="ft"></div>
</div>
</section>

<?php
}
include("includes/footer.php");

?>

 

It's weird that the indentation gets done correct now..

Link to comment
https://forums.phpfreaks.com/topic/260258-fatal-error/#findComment-1334225
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.