Forum Moderators: coopster

Message Too Old, No Replies

the image upload and resizing script - PHP III

Did someone fixed the last upload bug?

         

tomda

10:05 am on Jan 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bag-O-Tricks for PHP III
*************************

See [webmasterworld.com...] for script and posts
the image upload and resizing script
*************************
Joeym wrote :
>hmmm..i am still messing with this image upload code, but >still have not been able to make it work.
>now i think it might not be the directory that is causing >the problem, but it is not adding the $new_pic to the end >of the directory when it is trying to copy it there.
************************
Did someone found out why the image upload copy is not working..
If yes, please let me know as it is the only bug I have.

I suspect the problem is with the [$q]... As I have modified almost everything except this.

By the way, how can I get variables from the form array, I have tried :
if(isset($_POST["usefile[]"])) {$userfile = $_POST["userfile[]"];} else {$userfile = "";}
but it always gives me $count always give me
count($userfile) equal to 1

Your support would be highly appreciated.
Tommy

Birdman

10:22 am on Jan 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Tommy,

What error are you recieving or what part of the script doesn't work?

I posted that script and did promise to clean it up, yet haven't :( Let me know what went wrong and I will try to help.

Birdman

tomda

11:23 am on Jan 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Birdman,

I manage to rectify the script and can upload and resize pictures. It is working fine.

The reason why I was interested by your script was the possibility to submit many pictures in once using form array. I think it is where the script is wrong (with the count function and the q variable). Still I do not know how to retrieve array variables from the form.

Anyway, thanks for the reply.

Still work on a script and will post it when completed if someone is interested.

Cheers

tomda

12:07 pm on Jan 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a modified version of Birdman's script - see above for the link.

It upload jpg/jpeg format only and has a size limit also.
It also show the size of the file in Kb, Mb, etc...
It upload data in a database and generate a new name automatically (using the id)
But it doesn't have the array form (so it is not possible to submit multiple pictures in once)

Enjoy and thanks to Birdman for scripting most of the Php

***********************
First make you database

CREATE TABLE `pics` (
`pic_id` int(11) NOT NULL auto_increment,
`pic_name` varchar(150) default NULL,
`descrip` text,
PRIMARY KEY (`pic_id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

***************************
Then the script of the phpfile (sorry someone needs to explain me how I put the script in the window for better viewing?)

<?
$server="localhost"; $user="root"; $pass=""; $database="test"; $table="pics";
$dbh= mysql_connect("$server","$user","$pass") or die ("Unable to connect server");
$db_select=mysql_select_db("$database") or die ("Unable to get table");

$sizelimit = "42000";
$uploaddir = "D:/EasyPHP2/EasyPHP1-7/www/script/uploadGD_pic/images/";
$serverdir = "http://localhost/script/uploadGD_pic/images/";
?>

<? function GetSize($s) {
if ($s <= 1024) return $s." bytess";
else if ($s <= 1048576) return $s = round($s/1024,0)." Kb";
else if ($s <= 11559501824) return $s = round($s/1048576,0)." Mb";}?>

<html>
<head><title>Enter Name and Descriptions of Pics</title></head>
<body>
<h1>Enter Name and Descriptions of Pics</h1>

<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="imagefile">
<input type="submit" name="Submit" value="Submit">

<?
if(isset($_POST['Submit'] )) {

if ($_FILES['imagefile']['type'] == "image/pjpg" ¦¦ $_FILES['imagefile']['type'] == "image/pjpeg") {

if ($_FILES['imagefile']['size'] >= $sizelimit) {
echo "Could Not Copy, Too High - Limit:";
$sizelimit = GetSize($sizelimit);
echo $sizelimit;}

else {
$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;}

copy ($_FILES['imagefile']['tmp_name'],"".$uploaddir."".$new_id. ".jpg") or die ("Could not copy");

echo "<br>";
echo "Name: ".$new_id. ".jpg - ";
$size = GetSize($_FILES['imagefile']['size']);
echo "Size: ".$size." - ";
echo "Type: ".$_FILES['imagefile']['type']."<br>";
echo "Large size path: ".$uploaddir."".$new_id. ".jpg<br>";
echo "Copy Done....";?>

<?
$sql = "INSERT INTO `pics` ( `pic_id` , `pic_name` , `descrip` )VALUES ('NULL', NULL , NULL);";
mysql_query($sql);

$new_thumb = "thumbs/" .$new_id. ".jpg";
$sourcefile = "".$uploaddir."".$new_id. ".jpg";
$picsize = getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize[1];

$dest_x = $source_x*16/100;
$dest_y = $source_y*16/100;

$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);
?>

<br><br><img src="<? echo "".$serverdir."".$new_thumb.""?>"><br><br>
Name: <? echo "" .$new_id. ".jpg"?><br><br>
Thumbnail path: <? echo "".$targetfile."".$new_id. ".jpg<br>";

} }
else {
echo "";
echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].") - Only Jpg/Jpeg format is authorized"; }}
?>
</body>
</html>
*************************
That's it

Tommy