Beste bezoekers,

op mijn site kunnen mensen plaatjes uploaden, maar als ik meerdere plaatjes achter elkaar upload, krijg ik na +- 6x een plaatje te uploaden, de volgende foutmelding:
Error:
"User "username" has already more than 'max_user_connections' active connections"

Als ik dan gewoon de hoofdpagina wil zien krijg ik dezelfde melding.

Hier de code die ik gebruik om te uploaden.
<script type="text/javascript">

function highlight(field) {
field.focus();
field.select();
}
-->
</script>
<table width="100%" border="1" cellspacing="0" bordercolor="#000000" bgcolor="#CCCCCC">
<tr>
<td width="60%" height="60" valign="top"><p>
<?php
include "includes/filter.php";
//Image Upload Script
//Created by Lappy512
//Part of ImageHostSript.
//See conf_global.php
?>

<?php
// $userfile is where the file is on the webserver
$userfile = $HTTP_POST_FILES['imagefile']['tmp_name'];

// $userfile is original file name
$userfile_name = $HTTP_POST_FILES['imagefile']['name'];

// $userfile_size is size in bytes
$userfile_size = $HTTP_POST_FILES['imagefile']['size'];

// $userfile_type is mime type e.g. image/gif
$userfile_type = $HTTP_POST_FILES['imagefile']['type'];

// $comments are the comments, but we need logic if there aren't comments.
if($_POST['comments'])
{
$comments = $_POST['comments'];
if (get_magic_quotes_gpc() == 0)
{
$comments = addslashes($comments);
}
$comments = nl2br($comments);
}
else
{
//they didn't comment
$comments = 'Geen Commentaar bij dit plaatje';
}

// $url are is the URL, but we need logic if there isn't one.
if($_POST['url'])
{
$url = $_POST['url'];
if (get_magic_quotes_gpc() == 0)
{
$url = addslashes($url);
}
$url = nl2br($url);
}
else
{
//they didn't comment
$url = 'http://www.pornholio.be';;
}

//Sporadic MIME-TYPES?
if ($userfile_type == 'image/x-png')
{
$userfile_type = 'image/png';
}
if ($userfile_type == 'image/pjpeg')
{
$userfile_type = 'image/jpeg';
}

// $userfile_error is any error encountered
$userfile_error = $HTTP_POST_FILES['imagefile']['error'];

//PHP 4.2.0+ code ONLY. This code will not work with PHP 4.1 or less
if ($userfile_error > 0)
{
echo 'Problem: ';
switch($userfile_error)
{
case 1: echo 'File exceeded Maximum upload filesize'; break;
case 2: echo 'File exceeded Maximum upload filesize'; break;
case 3: echo 'File partially uploaded'; break;
case 4: echo 'No File Uploaded'; break;
}
exit;
}
//end of code for 4.2.0+

switch($userfile_type)
{
case 'image/gif':
break;
case 'image/png':
break;
case 'image/jpeg':
break;
case 'image/bmp':
echo "BMP File format not supported. Please upload a JPEG, PNG, or a GIF. Thanks. ";
exit;
default:
echo "Problem: File is not a supported image filetype. Please upload a JPEG, PNG, or a GIF. Thanks. ";
echo "<br> your file has a MIME-TYPE of $userfile_type";
exit;
break;
}
//Lets try connecting to mySQL
@ $db = mysql_pconnect($mysql['host'], $mysql['user'], $mysql['pass']);
//IT"S NOT WORKING!
if (!$db)
{
die("error");
mysql_close();
}
mysql_select_db($mysql['db']);

//We need to get a date for our Database
$date = time();

//Put the data there!
$query = "INSERT INTO `images` ( `id` , `size` , `downloads` , `lastuse` , `type` , `comments`, `url`, `ip` , `report` ) VALUES ('', " . $userfile_size . ", '0', " . $date . ", " . '\'' . $userfile_type . '\'' . ", " . '\'' . $comments . '\'' . ", " . '\'' . $url . '\'' . ", " . '\'' . $_SERVER['REMOTE_ADDR'] . '\'' . " , '' );";

//QUERY!
$result = mysql_query($query);
if (!$result)
{
die("MySQL insert error");
mysql_close();
}

$userfile_name = mysql_insert_id();

//put the file where we want it
$upfile = './uploads/' . $userfile_name;
//need PHP 4.0.3
if(is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
mysql_close();
}
}
else
{
echo 'Problem: Possible File upload attack. Filename: '.$userfile_name;
exit;
mysql_close();
}

echo '<br>Het bestand is succesvol geupload<br/><br/>';

//get current stats
$query = "SELECT * FROM `stat_cache`";

$result = mysql_query($query);

if (!$result)
{
die("MySQL Select error");
mysql_close();
}
$stat = mysql_fetch_array($result);

//update statistics
//files update
$files = $stat['files'] + 1;
$query = "UPDATE `stat_cache` SET files=" . $files . " WHERE 1";
$result = mysql_query($query);
if (!$result)
{
die("MySQL Update error");
mysql_close();
}
//space update
$totalspace = $stat['space'] + $userfile_size;
$query = "UPDATE `stat_cache` SET space=" . $totalspace . " WHERE 1";
$result = mysql_query($query);
if (!$result)
{
die("MySQL Update error");
mysql_close();
}


//1337 h4x0rs can resize everything, even though we can't outputz0rz to everything.
//Resample it now!
// The file

$filename = $upfile;
$thumb = './uploads/' . 'thumb_' . $userfile_name;
// Set a maximum height and width
$width = 150;
$height = 150;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
if(($width_orig < $width) && ($height_orig < $height))
{
$width = $width_orig;
$height = $height_orig;
}
else
{
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
if($userfile_type == 'image/jpeg')
{
$image = imagecreatefromjpeg($filename);
}
if($userfile_type == 'image/png')
{
$image = imagecreatefrompng($filename);
}
if($userfile_type == 'image/gif')
{
$image = imagecreatefromgif($filename);
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
if($userfile_type == 'image/jpeg')
{
imagejpeg($image_p, $thumb);
}
if($userfile_type == 'image/png' or $userfile_type == 'image/gif')
{
imagepng($image_p, $thumb);
$userfile_type = 'image/png';
}

$userfile_size = filesize($upfile);
$id = $userfile_name;

//Put the data there!
$query = "INSERT INTO `thumbs` ( `id` , `size` , `downloads` , `lastuse` , `type` )VALUES ('$id', '$userfile_size', '0', '$date', '$userfile_type');";

//QUERY!
$result = mysql_query($query);

if (!$result)
{
die("MySQL insert error 2 " . mysql_error());
mysql_close();
}

//get current stats
$query = "SELECT * FROM `stat_cache`";

$result = mysql_query($query);

if (!$result)
{
die("MySQL Select error");
mysql_close();
}
$stat = mysql_fetch_array($result);
//update statistics
//space update
$totalspace = $stat['space'] + $userfile_size;
$query = "UPDATE `stat_cache` SET space=" . $totalspace . " WHERE 1";
$result = mysql_query($query);
if (!$result)
{
die("MySQL Update error");
mysql_close();
}
?>

<FORM action="../nowhere" method="post" class="table_decoration">
<b><a href="<?php $siteurl; ?>">Click hier om nog een foto te uploaden</a></b><br>
<br/>HTML:<br>
<input onclick="highlight(this);" style="width: 500px;" size="80" value="<a href='<?php echo $siteurl; ?>viewer.php?id=<?php echo $id; ?>'><img src='<?php echo $siteurl; ?>thumb/<?php echo $id; ?>.jpeg'></a>">
<br/>BBCODE:<br>
<input onclick="highlight(this);" style="width: 500px;" size="80" value="[img]<?php echo $siteurl; ?>thumb/<?php echo $id; ?>.jpeg[/img]">
<br/>Thumbnail for Forums(1):<br>
<input onclick="highlight(this);" style="width: 500px;" size="80" value="[IMG]<?php echo $siteurl; ?>uploads/thumb_<?php echo $id; ?>[/IMG]">
<br/>Thumbnail for Forums(2):<br>
<input onclick="highlight(this);" style="width: 500px;" size="80" value="[img]<?php echo $siteurl; ?>uploads/thumb_<?php echo $id; ?>[/img]">
</FORM>

<br/><center>URL: <a href='<?php echo $siteurl; ?>view/<?php echo $id; ?>'><?php echo $siteurl; ?>viewer.php?id=<?php echo $id; ?></a></center>
<center><br/><img src="<?php echo $siteurl; ?>image/<?php echo $id; ?>.jpeg"></center>
<br/><center>Commentaar: <br><?php echo (stripslashes($comments)); ?>
<br>Geupload door :
<?php echo "<br/><a href=" . $url . " target=\"_blank\">" . $url . "</a><br/></center><br/>";
?>
<br><br><b><a href="<?php $siteurl; ?>">Click hier om nog een foto te uploaden</a> </b>
</center><?php mysql_close(); ?>
</p></td>
</tr>
</table>
Okay, kan jij ondertussen voor mij een samenvatting van de koran schrijven? We willen je best helpen maar zitten hier niet om boekwerken te debuggen...

Zoiezo zou je kunnen beginnen door het script eens om te zetten naar de huidige standaard. $HTTP_POST_FILES kan je bijv. vervangen door $_FILES(). En je query's kunnen wat mij betreft ook wel wat netter en efficienter.

Overigens heeft jou probleem weinig met het script te maken. Je mysql heeft een maximaal aantal connecties wat hij tegelijkertijd wilt verwerken van één gebruiker. Verhoog die en je probleem is opgelost.
en waarom sluit je je mysql connectie zo vaak als je hem in hetzelfde script toch blijft gebruiken?
He Arjan,

Sorry voor de rare post. Ik drukte per ongeluk op enter en toen stond ie er.

Ik heb hem een beetje uitgekleed en heb dat $_FILES() nu ook gebruikt. Ben nog niet zo heel lang bezig met php. Ben bezig met een cursus en heb dit script van internet gehaald en een beetje aangepast.

Ik host mijn website bij een derde en kan de max_users niet aanpassen. Ik denk zelf dat het iets heeft te maken met het feit dat ik de connectie na het uploaden moet verbreken.

Ik hoop dat er een goede oplossing gevonden kan worden.

Hierbij het uitgekledescript:

<?php
// $userfile is where the file is on the webserver
$userfile = $_FILES()['imagefile']['tmp_name'];

// $userfile is original file name
$userfile_name = $_FILES()['imagefile']['name'];

// $userfile_size is size in bytes
$userfile_size = $_FILES()['imagefile']['size'];

// $userfile_type is mime type e.g. image/gif
$userfile_type = $_FILES()['imagefile']['type'];

// $comments are the comments, but we need logic if there aren't comments.
if($_POST['comments'])
{
$comments = $_POST['comments'];
if (get_magic_quotes_gpc() == 0)
{
$comments = addslashes($comments);
}
$comments = nl2br($comments);
}
else
{
//they didn't comment
$comments = 'Geen Commentaar bij dit plaatje';
}

// $url are is the URL, but we need logic if there isn't one.
if($_POST['url'])
{
$url = $_POST['url'];
if (get_magic_quotes_gpc() == 0)
{
$url = addslashes($url);
}
$url = nl2br($url);
}
else
{
//they didn't comment
$url = 'http://www.pornholio.be';;
}

//Sporadic MIME-TYPES?
if ($userfile_type == 'image/x-png')
{
$userfile_type = 'image/png';
}
if ($userfile_type == 'image/pjpeg')
{
$userfile_type = 'image/jpeg';
}

// $userfile_error is any error encountered
$userfile_error = $_FILES()['imagefile']['error'];

//PHP 4.2.0+ code ONLY. This code will not work with PHP 4.1 or less
if ($userfile_error > 0)
{
echo 'Problem: ';
switch($userfile_error)
{
case 1: echo 'File exceeded Maximum upload filesize'; break;
case 2: echo 'File exceeded Maximum upload filesize'; break;
case 3: echo 'File partially uploaded'; break;
case 4: echo 'No File Uploaded'; break;
}
exit;
}
//end of code for 4.2.0+

switch($userfile_type)
{
case 'image/gif':
break;
case 'image/png':
break;
case 'image/jpeg':
break;
case 'image/bmp':
echo "BMP File format not supported. Please upload a JPEG, PNG, or a GIF. Thanks. ";
exit;
default:
echo "Problem: File is not a supported image filetype. Please upload a JPEG, PNG, or a GIF. Thanks. ";
echo "<br> your file has a MIME-TYPE of $userfile_type";
exit;
break;
}
//Lets try connecting to mySQL
@ $db = mysql_pconnect($mysql['host'], $mysql['user'], $mysql['pass']);
//IT"S NOT WORKING!
if (!$db)
{
die("error");
mysql_close();
}
mysql_select_db($mysql['db']);

//We need to get a date for our Database
$date = time();

//Put the data there!
$query = "INSERT INTO `images` ( `id` , `size` , `downloads` , `lastuse` , `type` , `comments`, `url`, `ip` , `report` ) VALUES ('', " . $userfile_size . ", '0', " . $date . ", " . '\'' . $userfile_type . '\'' . ", " . '\'' . $comments . '\'' . ", " . '\'' . $url . '\'' . ", " . '\'' . $_SERVER['REMOTE_ADDR'] . '\'' . " , '' );";

//QUERY!
$result = mysql_query($query);
if (!$result)
{
die("MySQL insert error");
mysql_close();
}

$userfile_name = mysql_insert_id();

//put the file where we want it
$upfile = './uploads/' . $userfile_name;
//need PHP 4.0.3
if(is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
mysql_close();
}
}
else
{
echo 'Problem: Possible File upload attack. Filename: '.$userfile_name;
exit;
mysql_close();
}

echo '<br>Het bestand is succesvol geupload<br/><br/>';

//get current stats
$query = "SELECT * FROM `stat_cache`";

$result = mysql_query($query);

if (!$result)
{
die("MySQL Select error");
mysql_close();
}
$stat = mysql_fetch_array($result);

//update statistics
//files update
$files = $stat['files'] + 1;
$query = "UPDATE `stat_cache` SET files=" . $files . " WHERE 1";
$result = mysql_query($query);
if (!$result)
{
die("MySQL Update error");
mysql_close();
}
//space update
$totalspace = $stat['space'] + $userfile_size;
$query = "UPDATE `stat_cache` SET space=" . $totalspace . " WHERE 1";
$result = mysql_query($query);
if (!$result)
{
die("MySQL Update error");
mysql_close();
}


//1337 h4x0rs can resize everything, even though we can't outputz0rz to everything.
//Resample it now!
// The file

$filename = $upfile;
$thumb = './uploads/' . 'thumb_' . $userfile_name;
// Set a maximum height and width
$width = 150;
$height = 150;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
if(($width_orig < $width) && ($height_orig < $height))
{
$width = $width_orig;
$height = $height_orig;
}
else
{
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
if($userfile_type == 'image/jpeg')
{
$image = imagecreatefromjpeg($filename);
}
if($userfile_type == 'image/png')
{
$image = imagecreatefrompng($filename);
}
if($userfile_type == 'image/gif')
{
$image = imagecreatefromgif($filename);
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
if($userfile_type == 'image/jpeg')
{
imagejpeg($image_p, $thumb);
}
if($userfile_type == 'image/png' or $userfile_type == 'image/gif')
{
imagepng($image_p, $thumb);
$userfile_type = 'image/png';
}

$userfile_size = filesize($upfile);
$id = $userfile_name;

//Put the data there!
$query = "INSERT INTO `thumbs` ( `id` , `size` , `downloads` , `lastuse` , `type` )VALUES ('$id', '$userfile_size', '0', '$date', '$userfile_type');";

//QUERY!
$result = mysql_query($query);

if (!$result)
{
die("MySQL insert error 2 " . mysql_error());
mysql_close();
}

//get current stats
$query = "SELECT * FROM `stat_cache`";

$result = mysql_query($query);

if (!$result)
{
die("MySQL Select error");
mysql_close();
}
$stat = mysql_fetch_array($result);
//update statistics
//space update
$totalspace = $stat['space'] + $userfile_size;
$query = "UPDATE `stat_cache` SET space=" . $totalspace . " WHERE 1";
$result = mysql_query($query);
if (!$result)
{
die("MySQL Update error");
mysql_close();
}
?>
Je gebruikt mysql_pconnect(), die doet bijna hetzelfde als mysql_connect(), met twee grote verschillen.

Ten eerste, wanneer je verbinding maakt, zal de functie eerst proberen een (persistente) link te openen die al open staat met dezelfde host, username en password. Als er een gevonden wordt zal een identifier ervoor worden teruggegeven, in plaats van een nieuwe verbinding te openen.

Ten tweede, de verbinding met de SQL server zal niet worden gesloten wanneer de executie van het script klaar is. In plaats daarvan zal de link open blijven voor toekomstig gebruik (mysql_close() zal geen links sluiten die werden gemaakt door mysql_pconnect()).

mysql_connect() gebruiken dus :).l
:( Je bent me net voor. Ik wou presies 't zelfde posten.
Offtopic: Gooi die vieze en volkomen overbodige backtics ` bij het grofvuil. Die horen absoluut niet thuis in een goede en nette SQL-query.

Je wordt dan ook gedwongen om correcte tabel- en kolomnamen te gebruiken, daar kun je in de toekomst nog een hoop voordeel van hebben.

Nooit meer backtics in je queries zetten!
Beste allemaal,

Bedankt voor de hulp. Het probleem is opgelost. @frank
Je hebt helemaal gelijk, maar ik wil voor de rest niets meer aan die website doen. Ik heb een nieuw project waar ik mee aan de slag ga en voor deze website ga ik gewoon mensen inhuren die het goed doen. Ben gewoon een beginneling en heb helemaal de kennis niet. Wilde gewoon even stoeien met PHP en wat ik tot nu toe heb opgezet is voldoende. Bedankt allemaal

Reageren