/    Sign up×
Community /Pin to ProfileBookmark

Trouble Getting File Upload to work

I’ve followed the tut in how to upload a file. However, im getting an error message and can’t figure out why. Any help would be appreciated.

Error:

[CODE]Fatal error: Call to undefined function: error() in /homepages/31/d221003102/htdocs/dsc046768903/freelance/mutuba/testing/admin/editmp3.php on line 92[/CODE]

Points to the line:

[code=php]@move_uploaded_file($_FILES[$fieldname][‘tmp_name’], $uploadFilename)
or error(‘receiving directory insuffiecient permission’, $uploadForm);[/code]

Code:

[code=php]// filename: upload.processor.php

// first let’s set some variables

// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER[‘PHP_SELF’]), ”, $_SERVER[‘PHP_SELF’]);

// make a note of the directory that will recieve the uploaded file
$uploadsDirectory = $_SERVER[‘DOCUMENT_ROOT’] . ‘media/mp3player/music/’;

// make a note of the location of the upload form in case we need it
$uploadForm = ‘editmp3.php’;

// fieldname used within the file <input> of the HTML form
$fieldname = ‘file’;

// Now let’s deal with the upload

// possible PHP upload errors
$errors = array(1 => ‘php.ini max file size exceeded’,
2 => ‘html form max file size exceeded’,
3 => ‘file upload was only partial’,
4 => ‘no file was attached’);

if($_POST[‘submitadd’])
{

// check for PHP’s built-in uploading errors
($_FILES[$fieldname][‘error’] == 0)
or error($errors[$_FILES[$fieldname][‘error’]], $uploadForm);

// check that the file we are working on really was the subject of an HTTP upload
@is_uploaded_file($_FILES[$fieldname][‘tmp_name’])
or error(‘not an HTTP upload’, $uploadForm);

// make a unique filename for the uploaded file and check it is not already
// taken… if it is already taken keep trying until we find a vacant one
// sample filename: 1140732936-filename.jpg
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.’-‘.$_FILES[$fieldname][‘name’]))
{
$now++;
}

// now let’s move the file to its final location and allocate the new filename to it
@move_uploaded_file($_FILES[$fieldname][‘tmp_name’], $uploadFilename)
or error(‘receiving directory insuffiecient permission’, $uploadForm);

$sql = “INSERT INTO `music` ( `id` , `title` , `composer` , `venue`, `file`, `active`)
VALUES (”,'” . $_POST[‘title’] . “‘, ‘” . $_POST[‘composer’] . “‘, ‘” . $_POST[‘venue’] . “‘, ‘” . $uploadFilename . “‘, ‘” . $_POST[‘active’] . “‘)”;

mysql_query($sql);
header(“Location: editmp3.php?msg=1”);

// The following function is an error handler which is used
// to output an HTML error page if the file upload fails
function error($error, $location, $seconds = 5)
{
header(“Refresh: $seconds; URL=”$location””);
echo ‘<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”‘.”n”.
‘”http://www.w3.org/TR/html4/strict.dtd”>’.”nn”.
‘<html lang=”en”>’.”n”.
‘ <head>’.”n”.
‘ <meta http-equiv=”content-type” content=”text/html; charset=iso-8859-1″>’.”nn”.
‘ <link rel=”stylesheet” type=”text/css” href=”stylesheet.css”>’.”nn”.
‘ <title>Upload error</title>’.”nn”.
‘ </head>’.”nn”.
‘ <body>’.”nn”.
‘ <div id=”Upload”>’.”nn”.
‘ <h1>Upload failure</h1>’.”nn”.
‘ <p>An error has occured: ‘.”nn”.
‘ <span class=”red”>’ . $error . ‘…</span>’.”nn”.
‘ The upload form is reloading</p>’.”nn”.
‘ </div>’.”nn”.
‘</html>’;
exit;
} // end error handler

}else
{
require(“backendheader.php”);

?>
<div id=”leftnav”>
<?php
require(“leftnav.php”);
?>
</div>
<!– End of leftnav id –>
<div id=”main”>

<?php

if($validentry!=0){
$fillsql = “SELECT * FROM music WHERE id = ” . $validentry . “;”;
$fillres = mysql_query($fillsql);
$fillrow = mysql_fetch_assoc($fillres);
echo “<h1>Edit Song</h1>”;
echo “<p>click<a href=’editmp3.php’> here </a> to add a new song</p>”;
}
else{
echo “<h1>Add New Song</h1>”;
}

//**********************************
//Check if a confirmation message needs to be printed
//**********************************

if($_GET[‘msg’] == 1){
echo “<h5 class=’confirm’>Song Succesfully Added</h5>”;
}
if($_GET[‘msg’] == 2){
echo “<h5 class=’confirm’>Song Succesfully Edited</h5>”;
}
if($_GET[‘msg’] == 3){
echo “<h5 class=’confirm’>Song Succesfully Deleted</h5>”;
}
?>

<form action=”<?php echo $SCRIPT_NAME ?>” enctype=”multipart/form-data” method=”post”>
<table>
<input type=”hidden” name=”id” value=”<?php echo $validentry;?>” />
<tr>
<td>Title:
</td>
<td>
<input type=”text” name=”title” size=”50″ value=”<?= trim($fillrow[‘title’]); ?>”>
</td>
</tr>
<tr>
<td>Composer:
</td>
<td>
<input type=”text” name=”composer” size=”50″ value=”<?= trim($fillrow[‘composer’]); ?>”>
</td>
</tr>
<tr>
<td>Venue:</td>
<td>
<input type=”text” name=”venue” size=”50″ value=”<?= trim($fillrow[‘venue’]); ?>”>
</td>
</tr>
<tr>
<td>File:</td>
<td>
<input type=”file” name=”file”>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”25000″ />
</td>
</tr>
<tr>
<td>Active:</td>
<td>
<select name=”active”>
<option value=”yes”>yes</option>
<option value=”no”>no</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<?php
if($validentry!=0){
echo “<input type=’submit’ name=’submitedit’ value=’Edit Song’ />”;
}
else{
echo “<input type=’submit’ name=’submitadd’ value=’Add Song’ />”;
}
?>
</td>
</tr>
</table>
</form>[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@rootJan 14.2009 — Read the error message carefully..

And especially this bit: "Call to undefined function: error() "

Paying even closer to the fact that [b]error()[/b] is [b]undefined[/b]
Copy linkTweet thisAlerts:
@bryceray1121authorJan 16.2009 — I've nearly got the script working. However, it is still not uploading the file. The script did execute without an error message though. When i looked in my mysql database where i want it to just put the modified name of the file it put this:/homepages/31/d22100 instead of 12345-filename.mp3 (for example). And it was not uploaded to the directory, any suggestions?

EDIT: After looking at the specifications for move_uploaded_file() i see that the variable $uploadFilename is actually the destination folder not the filename. But i'm still not sure how the name of the file is created and the file is renamed. And then i'm not sure which variable should contain the filename so i can save it to my database. I see that $_FILES[$fieldname]['tmp_name'] is supposed to be the filename but when i save that to my mysql database i get this: /tmp/phpkOjo5J or somethign similar. And the file is still not uploading, could have something to do with the filenaming problems though.

Thanks for your time.

[code=php]// filename: upload.processor.php

// first let's set some variables

// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// make a note of the directory that will recieve the uploaded file
$uploadsDirectory = "/homepages/31/d221003102/htdocs/dsc046768903/freelance/mutuba/testing/media/mp3player/music";

// make a note of the location of the upload form in case we need it
$uploadForm = 'editmp3.php';

// fieldname used within the file <input> of the HTML form
$fieldname = 'file';

// Now let's deal with the upload

// possible PHP upload errors
$errors = array(1 => 'php.ini max file size exceeded',
2 => 'html form max file size exceeded',
3 => 'file upload was only partial',
4 => 'no file was attached');


if($_POST['submitadd'])
{

// The following function is an error handler which is used
// to output an HTML error page if the file upload fails
function error($error, $location, $seconds = 5)
{
header("Refresh: $seconds; URL="$location"");
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."n".
'"http://www.w3.org/TR/html4/strict.dtd">'."nn".
'<html lang="en">'."n".
' <head>'."n".
' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."nn".
' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."nn".
' <title>Upload error</title>'."nn".
' </head>'."nn".
' <body>'."nn".
' <div id="Upload">'."nn".
' <h1>Upload failure</h1>'."nn".
' <p>An error has occured: '."nn".
' <span class="red">' . $error . '...</span>'."nn".
' The upload form is reloading</p>'."nn".
' </div>'."nn".
'</html>';
exit;
} // end error handler

// check for PHP's built-in uploading errors
($_FILES[$fieldname]['error'] == 0)
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);

// check that the file we are working on really was the subject of an HTTP upload
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
or error('not an HTTP upload', $uploadForm);

// make a unique filename for the uploaded file and check it is not already
// taken... if it is already taken keep trying until we find a vacant one
// sample filename: 1140732936-filename.jpg
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
{
$now++;
}

// now let's move the file to its final location and allocate the new filename to it
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);

$sql = "INSERT INTO music ( id , title , composer , venue, file, active)
VALUES ('','" . $_POST['title'] . "', '" . $_POST['composer'] . "', '" . $_POST['venue'] . "', '" . $uploadFilename . "', '" . $_POST['active'] . "')";

mysql_query($sql);
header("Location: editmp3.php?msg=1");

}else
{
require("backendheader.php");

?>
<div id="leftnav">
<?php
require("leftnav.php");
?>
</div>
<!-- End of leftnav id -->
<div id="main">


<?php

if($validentry!=0){
$fillsql = "SELECT * FROM music WHERE id = " . $validentry . ";";
$fillres = mysql_query($fillsql);
$fillrow = mysql_fetch_assoc($fillres);
echo "<h1>Edit Song</h1>";
echo "<p>click<a href='editmp3.php'> here </a> to add a new song</p>";
}
else{
echo "<h1>Add New Song</h1>";
}

//**********************************
//Check if a confirmation message needs to be printed
//**********************************

if($_GET['msg'] == 1){
echo "<h5 class='confirm'>Song Succesfully Added</h5>";
}
if($_GET['msg'] == 2){
echo "<h5 class='confirm'>Song Succesfully Edited</h5>";
}
if($_GET['msg'] == 3){
echo "<h5 class='confirm'>Song Succesfully Deleted</h5>";
}
echo $uploadsDirectory;
?>

<form action="<?php echo $SCRIPT_NAME ?>" enctype="multipart/form-data" method="post">
<table>
<input type="hidden" name="id" value="<?php echo $validentry;?>" />
<tr>
<td>Title:
</td>
<td>
<input type="text" name="title" size="50" value="<?= trim($fillrow['title']); ?>">
</td>
</tr>
<tr>
<td>Composer:
</td>
<td>
<input type="text" name="composer" size="50" value="<?= trim($fillrow['composer']); ?>">
</td>
</tr>
<tr>
<td>Venue:</td>
<td>
<input type="text" name="venue" size="50" value="<?= trim($fillrow['venue']); ?>">
</td>
</tr>
<tr>
<td>File:</td>
<td>
<input type="file" name="file">
<!--<input type="hidden" name="MAX_FILE_SIZE" value="25000" />-->
</td>
</tr>
<tr>
<td>Active:</td>
<td>
<select name="active">
<option value="yes">yes</option>
<option value="no">no</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<?php
if($validentry!=0){
echo "<input type='submit' name='submitedit' value='Edit Song' />";
}
else{
echo "<input type='submit' name='submitadd' value='Add Song' />";
}
?>
</td>
</tr>
</table>
</form>[/code]
Copy linkTweet thisAlerts:
@rootJan 19.2009 — I have not any idea on this because the best people to answer this will be the hosts where your server resides, you will need to know what limits they impose, where the temporary directory is or if they do not allow uploads, etc.

Some information is easily obtained from the use of phpinfo() function. You may find the information in the PHP info dump.

[code=php]<?php echo phpinfo(); ?>[/code]
put in a file and save it as inf.php and then call the inf.php via the web browser and then look at the phpinfo for your domain. It is advised to delete the inf.php file after you have finished with it.
Copy linkTweet thisAlerts:
@bryceray1121authorJan 21.2009 — My hosting is through 1and1 and i've worked with them but they arn't being very helpful at all.

This is the output of that command. I'm not sure what to look for? Anything helpful? or should i continue to bother my host.
[code=php]System Linux infong 2.4 #1 SMP Tue Dec 18 22:34:10 UTC 2007 i686 GNU/Linux
Build Date Aug 12 2008 13:27:58
Configure Command '../configure' '--with-pear=/usr/lib/php' '--prefix=/usr' '--with-mysql=/usr/' '--with-zlib' '--enable-debug=no' '--enable-safe-mode=no' '--enable-discard-path=no' '--with-gd' '--with-png-dir' '--enable-track-vars' '--with-db' '--with-gdbm' '--enable-force-cgi-redirect' '--with-ttf' '--enable-ftp' '--with-mcrypt' '--enable-dbase' '--enable-memory-limit' '--enable-calendar' '--enable-wddx' '--with-jpeg-dir' '--enable-bcmath' '--enable-gd-imgstrttf' '--enable-shmop' '--enable-mhash' '--with-mhash' '--with-openssl' '--enable-xslt' '--with-xslt-sablot' '--with-dom' '--with-dom-xslt' '--with-dom-exslt' '--with-imap' '--with-curl' '--with-iconv' '--with-freetype-dir' '--with-bz2' '--with-gettext' '--enable-exif' '--with-idn' '--enable-mbstring=all' '--with-kerberos' '--with-imap-ssl' '--with-sqlite' '--with-zip'
Server API CGI
Virtual Directory Support disabled
Configuration File (php.ini) Path /homepages/31/d221003102/htdocs/dsc046768903/freelance/mutuba/testing/php.ini
PHP API 20020918
PHP Extension 20020429
Zend Extension 20050606
Debug Build no
Zend Memory Manager enabled
Thread Safety disabled
Registered PHP Streams php, http, ftp, https, ftps, compress.bzip2, compress.zlib
[/code]
×

Success!

Help @bryceray1121 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...