Jump to content

php upload multiple images


desithugg

Recommended Posts

the form

<?
$query = mysql_query("SELECT * FROM albums where owner = '$users_id'") or die(mysql_error()); 
$row = mysql_fetch_array( $query );
$album_check = $row["id"];
if($album_check!="")
{
echo"<form action='action.php?action=uploadimage' method='POST' enctype='multipart/form-data'>
<input type='hidden' name='MAX_FILE_SIZE' value='50000'>
<table align='center' cellspacing='0' cellpadding='0'>";
$max_no_img = "5";
for($i=1; $i<=$max_no_img; $i++){
echo"<tr><td class='smalltext' valign='top'><div height='100%' width='100%' style='height: 100%; border-top:1px solid #000000; border-left:1px solid #000000;'><b>Upload Image:</b></div></td><td class='smalltext' valign='top'><div height='100%' width='100%' style='height: 100%; border-top:1px solid #000000; border-right:1px solid #000000;'><input type='file' name='images[]' size='30'></div></td>
</tr><tr><td class='smalltext' valign='top'><div height='100%' width='100%' style='height: 100%; border-bottom:1px solid #000000; border-left:1px solid #000000;'><b>Description:</b></div></td><td class='smalltext' valign='top'><div height='100%' width='100%' style='height: 100%; border-bottom:1px solid #000000; border-right:1px solid #000000;'><textarea cols='30' rows='4' name='des[]'></textarea></div></td></tr>";
}
echo"<tr><td class='smalltext'><b>Album:</b></td><td class='smalltext'>
<select name='album'>";
$query = "SELECT * FROM albums where owner = '$users_id' order by id desc"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){$count++;
$t_name = $row["name"];
$t_id = $row["id"];
echo"<option value='".$t_id."'>".$t_name."</option>";
}
echo"</select></td></tr><tr><td class='smalltext' colspan='2'><center><input type='submit' value='Upload Image'></center></td></tr></table>";
}
else
{
echo"You must create an album before you can upload any images.";
}
?>

and here is the php code

<?
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$upload_dir = "uploads/"; //upload directory
$imgfile = $_FILES['images']; //the image file
$imgname = $_FILES['images']['name'];
$extention = getFileExtension($imgfile); //the extention of the image
if($extention=="jpg" OR $extention=="jpeg" OR $extention=="png" OR $extention=="gif"){ $file_type = "correct";}
if($file_type!="correct"){ send_error("One of the file was not one of the supported extentions."); } //if the extention is not suppored send error
$imgsize = GetImageSize($imgfile); //the size of the image
if (($imgsize[0] > 100) || ($imgsize[1] > 100))
{
$tmpimg = tempnam("/tmp", "MKUP");
system("djpeg $imgfile >$tmpimg");
system("pnmscale -xy 100 100 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");
unlink($tmpimg);
}
$rand_num = rand(1,10000000000000000000);
$finalname['resized'] = $users_id."-".imgname."-".$rand_num."_resized";
$finalname['original'] = $users_id."-".imgname."-".$rand_num."_original";
$newfile['resized'] = $upload_dir.$finalname['resized'];
$newfile['original'] = $upload_dir.$finalname['original'];
if(file_exists($finalname['resized']) OR file_exists($finalname['original']))
{
$rand_num = rand(1,100000000000000000000);
$finalname['resized'] = $users_id."-".imgname."-".$rand_num."_resized";
$finalname['original'] = $users_id."-".imgname."-".$rand_num."_original";
$newfile['resized'] = $upload_dir.$finalname['resized'];
$newfile['original'] = $upload_dir.$finalname['original'];
}
if (!copy($imgfile,$newfile['resized']) OR !copy($$_FILES['images'],$newfile['original']))
{
send_erro("Error uploading the files, please try again.");
}
?>

Basically what the php code does is resize the image and than upload the original and the resized to the server.

And all that works fine when I want to upload one image, but I want to make it so I can upload multiple with that form I have above.

Any idea's how? I'm not too great with loops or uploads.

Link to comment
https://forums.phpfreaks.com/topic/49102-php-upload-multiple-images/
Share on other sites

Yeah, use a for() loop to expand your form so you have the ability to type in more than 1 image directory.

 

Then use a for() loop to loop the php part until all the images are done.

 

So, it would be something like this (SOMETHING)...

Form:

<?php
$query = mysql_query("SELECT * FROM albums where owner = '$users_id'") or die(mysql_error()); 
$row = mysql_fetch_array( $query );
$album_check = $row["id"];
if($album_check!="")
{
echo"<form action='action.php?action=uploadimage' method='POST' enctype='multipart/form-data'>";
for($sio = 1; $sio <= 10; $sio++)
{
echo "<input type='hidden' name='MAX_FILE_SIZE' value='50000'>
<table align='center' cellspacing='0' cellpadding='0'>";
$max_no_img = "5";
for($i=1; $i<=$max_no_img; $i++){
echo"<tr><td class='smalltext' valign='top'><div height='100%' width='100%' style='height: 100%; border-top:1px solid #000000; border-left:1px solid #000000;'><b>Upload Image:</b></div></td><td class='smalltext' valign='top'><div height='100%' width='100%' style='height: 100%; border-top:1px solid #000000; border-right:1px solid #000000;'><input type='file' name='images[]' size='30'></div></td>
</tr><tr><td class='smalltext' valign='top'><div height='100%' width='100%' style='height: 100%; border-bottom:1px solid #000000; border-left:1px solid #000000;'><b>Description:</b></div></td><td class='smalltext' valign='top'><div height='100%' width='100%' style='height: 100%; border-bottom:1px solid #000000; border-right:1px solid #000000;'><textarea cols='30' rows='4' name='des[]'></textarea></div></td></tr>";
}
echo"<tr><td class='smalltext'><b>Album:</b></td><td class='smalltext'>
<select name='album'>";
$query = "SELECT * FROM albums where owner = '$users_id' order by id desc"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){$count++;
$t_name = $row["name"];
$t_id = $row["id"];
echo"<option value='".$t_id."'>".$t_name."</option>";
}
}
echo"</select></td></tr><tr><td class='smalltext' colspan='2'><center><input type='hidden' name='sio' value=".$sio."><input type='submit' value='Upload Image'></center></td></tr></table>";
}
else
{
echo"You must create an album before you can upload any images.";
}
?>

 

php

<?php
for($sio = 1; $sio <= $_POST['sio']; $sio++)
{
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$upload_dir = "uploads/"; //upload directory
$imgfile = $_FILES['images']; //the image file
$imgname = $_FILES['images']['name'];
$extention = getFileExtension($imgfile); //the extention of the image
if($extention=="jpg" OR $extention=="jpeg" OR $extention=="png" OR $extention=="gif"){ $file_type = "correct";}
if($file_type!="correct"){ send_error("One of the file was not one of the supported extentions."); } //if the extention is not suppored send error
$imgsize = GetImageSize($imgfile); //the size of the image
if (($imgsize[0] > 100) || ($imgsize[1] > 100))
{
$tmpimg = tempnam("/tmp", "MKUP");
system("djpeg $imgfile >$tmpimg");
system("pnmscale -xy 100 100 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");
unlink($tmpimg);
}
$rand_num = rand(1,10000000000000000000);
$finalname['resized'] = $users_id."-".imgname."-".$rand_num."_resized";
$finalname['original'] = $users_id."-".imgname."-".$rand_num."_original";
$newfile['resized'] = $upload_dir.$finalname['resized'];
$newfile['original'] = $upload_dir.$finalname['original'];
if(file_exists($finalname['resized']) OR file_exists($finalname['original']))
{
$rand_num = rand(1,100000000000000000000);
$finalname['resized'] = $users_id."-".imgname."-".$rand_num."_resized";
$finalname['original'] = $users_id."-".imgname."-".$rand_num."_original";
$newfile['resized'] = $upload_dir.$finalname['resized'];
$newfile['original'] = $upload_dir.$finalname['original'];
}
if (!copy($imgfile,$newfile['resized']) OR !copy($$_FILES['images'],$newfile['original']))
{
send_erro("Error uploading the files, please try again.");
}
}
?>

Archived

This topic is now archived and is closed to further replies.

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