multiple image upload with resize and save to DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dt22
Forum Commoner
Posts: 32
Joined: Sat Oct 10, 2009 2:53 am

multiple image upload with resize and save to DB

Post by dt22 »

i need to save multiple images and get additional info about the pic, create thumbnails and save the file to a folder and also file path to the database.

I looked at many tutorials and finally i find out one but its nit showing anything anyone pls help me to solve this?


FORM.PHP
<form enctype="multipart/form-data" action="loademup.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="300000">
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" />
</form>

loademup.php

<html>
<head><title>Enter Name and Descriptions of Pics</title></head>
<body>
<h1>Enter Name and Descriptions of Pics</h1>
<form action="view.php" method="post">
<?php
$dbh=mysql_connect("localhost", "root", "") or die ('I cannot connect to the database.');
mysql_select_db("builderskerala");

$uploaddir = 'uploads/';
$tot = count($userfile);
$num = 0;
for($q=0;$q<$tot;$q++){

if ($_FILES['userfile']['name'][$q] == "") continue;
$num = $num + 1;
$sql = "SELECT pic_id FROM pics ORDER BY pic_id DESC LIMIT 1";
$result = mysql_query($sql);

while($i = mysql_fetch_array($result)){
$new_id = $i['pic_id'] + 1;
$new_pic = "$new_id.jpg";
}

if (move_uploaded_file($_FILES['userfile']['tmp_name'][$q], $uploaddir . $new_pic)) {
$sql = "INSERT INTO `pics` ( `pic_id` , `name` , `descrip` )VALUES ('$new_id', NULL , NULL)";
mysql_query($sql);
} else {
echo "<strong>".$_FILES['userfile']['name'][$q]."</strong> did not upload!";
}

$new_thumb = "thumbs/$new_pic";
$sourcefile = "$uploaddir$new_pic";
$picsize = getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize;

if ($source_x > $source_y){
$dest_x = 200;
$dest_y = 150;
} else {
$dest_x = 150;
$dest_y = 200;
}

$targetfile = "$uploaddir$new_thumb";
$jpegqual = 75;
$source_id = imagecreatefromjpeg("$sourcefile");
$target_id = imagecreatetruecolor($dest_x, $dest_y);
$target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);
imagejpeg($target_id,"$targetfile",$jpegqual);
?>
<div style="clear: both;">
<a href="/images/<?=$new_pic?>">
<img src="/images/<?=$new_thumb?>" style="float: left" />
</a>
<strong><?=$_FILES['userfile']['name'][$q]?></strong><br /><br />
<strong>Name:</strong><br />
<input type="text" name="pic_name[<?=$num?>]" /><br /><br />
<strong>Description:</strong><br />
<textarea name="descrip[<?=$num?>]"></textarea>
<input type="hidden" name="pic_id[<?=$num?>]" value="<?=$new_id?>" />
</div>
<?php
}
?>
<input type="submit" value="Click to Save Descriptions" />
</form>
</body>
</html>

View.php
<html>
<head><title>View Pics and Descriptions</title></head>
<body>
<h1>View Pics and Descriptions</h1>
<?php
$dbh=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database.');
mysql_select_db ("builderskerala");$num = 0;
$tot = count($pic_id);

for ($q=0;$q<$tot;$q++){
$num = $num + 1;
$sql = "UPDATE pics SET descrip = '$descrip' , name = '$pic_name' WHERE pic_id = $pic_id[$num]";
mysql_query($sql);
?>
<div style="clear: both;">
<a href="/images/<?php echo "$pic_id[$num].jpg"?>">
<img src="/images/thumbs/<?php echo "$pic_id[$num].jpg"?>" style="float: left" />
</a>
<h2><?=$pic_name[$num]?></h2>
<p><?=$descrip[$num]?></p>
</div>
<?php
}
mysql_close();
?>
</body>
</html>

nothing is showing as output..pls help....
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: multiple image upload with resize and save to DB

Post by Jonah Bron »

You're probably getting a fatal error somewhere. Enable error reporting in your php.ini file.
dt22
Forum Commoner
Posts: 32
Joined: Sat Oct 10, 2009 2:53 am

Re: multiple image upload with resize and save to DB

Post by dt22 »

Jonah Bron wrote:You're probably getting a fatal error somewhere. Enable error reporting in your php.ini file.
Can u tell me how?

I my php.ini it shows:

; E_ALL - All errors and warnings
; E_ERROR - fatal run-time errors
; E_WARNING - run-time warnings (non-fatal errors)

Thks,,in advance.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: multiple image upload with resize and save to DB

Post by Jonah Bron »

Look for a line that starts with "error_reporting = ". Make sure it doesn't have a colon at the beginning of the line, and set it's value to "E_ALL | E_STRICT".
[text]error_reporting = E_ALL | E_STRICT[/text]
Then find the line that starts with "display_errors = ". Make sure it doesn't have a colon at the beginning of the line, and set it's value to "On".
[text]display_errors = On[/text]
Post Reply