Jump to content

[SOLVED] Image upload script working. But not sure about loop...


StefanRSA

Recommended Posts

I am working on a image upload form.

I want to use more than 1 upload field and my code allows for all to work fine.

My problem is that only 2 images gets uploaded even if there is more than 2 fields...

This code allow for different image extensions and create 3 re-sized images of each uploaded image

I hard coded the amount of fields in my script to be set as 4 upload fields with

$pictot = 4;

Please have a look at my script and tell me what I am ding wrong as only 2 image sets are uploaded even if I use all 4 fields... AT this stage only one image set display and will sort out the rest as soon as I can fix the loop....

 

Thanks

 

<?php 
error_reporting(0);
$pictot = 4;
$change="";
$imgNumb=1;
define ("MAX_SIZE","1000");
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

$errors=0;
  if($_SERVER["REQUEST_METHOD"] == "POST")
{
do{
	$image =$_FILES["img$imgNumb"]["name"];
$uploadedfile = $_FILES["img$imgNumb"]['tmp_name'];
     

	if ($image) 
	{

		$filename = stripslashes($_FILES["img$imgNumb"]['name']);

  		$extension = getExtension($filename);
		$extension = strtolower($extension);


if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
		{

			$change='<div class="msgdiv">Unknown Image extension </div> ';
			$errors=1;
		}
		else
		{

$size=filesize($_FILES["img$imgNumb"]['tmp_name']);


if ($size > MAX_SIZE*1024)
{
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}


if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES["img$imgNumb"]['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES["img$imgNumb"]['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else 
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;

list($width,$height)=getimagesize($uploadedfile);

$newwidthX=600;
$newheightX=($height/$width)*$newwidthX;
if($newheightX > 500) {
$newheight=500;
$newwidth=($newwidthX/$newheightX)*$newheight;
} else {
$newheight = $newheightX;
$newwidth = $newwidthX; } 
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth1=130;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

$newwidth2=65;
$newheight2=($height/$width)*$newwidth2;
$tmp2=imagecreatetruecolor($newwidth2,$newheight2);


imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height);

/// SET IMAGE NAMES HERE!!!!!!!!!!!!!!!!!!!!!!!!!
$image_name=time().'.'.$extension;
$filename = "../images/big/600". $image_name;
$filename1 = "../images/big/130". $image_name;
$filename2 = "../images/big/65". $image_name;

imagejpeg($tmp,$filename,70);
imagejpeg($tmp1,$filename1,70);
imagejpeg($tmp2,$filename2,70);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
imagedestroy($tmp2);
}}
$imgNumb++;
} while($_FILES["img$imgNumb"][name]);
}

//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors) 
{

   // mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'");
	$change=' <div class="msgdiv">Image Uploaded Successfully!<br>

   </div>';
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta content="en-us" http-equiv="Content-Language">

    <title>picture demo</title>

    

  <style type="text/css">
  .help
{
font-size:11px; color:#006600;
}
body {
     color: #000000;
background-color:#999999 ;
    background:#999999 url(<?php echo $user_row['img_src']; ?>) fixed repeat top left;


font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif; 

}
	.msgdiv{
width:759px;
padding-top:8px;
padding-bottom:8px;
background-color: #fff;
font-weight:bold;
font-size:18px;-moz-border-radius: 6px;-webkit-border-radius: 6px;
}
#container{width:763px;margin:0 auto;padding:3px 0;text-align:left;position:relative; -moz-border-radius: 6px;-webkit-border-radius: 6px; background-color:#FFFFFF }
</style>

  </head><body>
     <div align="center" id="err">
<?php echo $change; ?>  </div>
   <div id="space"></div>
   

  
  
  
  <div id="container" >
    
   <div id="con">
   
      
      
        <table width="502" cellpadding="0" cellspacing="0" id="main">
          <tbody>
            <tr>
              <td width="500" height="238" valign="top" id="main_right">

		  <div id="posts">
		      <img src="<?php echo $filename; ?>" />      <img src="<?php echo $filename1; ?>"  />      <img src="<?php echo $filename2; ?>"  />
		    <form method="post" action="" enctype="multipart/form-data" name="form1">
			<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
               <tr><Td style="height:25px"> </Td></tr>
	<tr>
          <td width="150"><div align="right" class="titles">Picture 
            : </div></td>
          <td width="350" align="left">
            <div align="left">
            <?
              for($i=1;$i<=$pictot;$i++){
echo '<tr>
<td>-</td><td>-</td><td>-</td><td>-</td><td>Image '.$i.'</td><td><input type="file" name="img'.$i.'"></td>
</tr>';
}
            ?>
            </div></td>
		  
        </tr>
	<tr><Td></Td>
	<Td valign="top" height="35px" class="help">Image maximum size <b>1000 </b>kb</span></Td>
	</tr>
	<tr><Td></Td><Td valign="top" height="35px"><input type="submit" id="mybut" value="       Upload        " name="Submit"/></Td></tr>
        <tr>
          <td width="200"> </td>
          <td width="200"><table width="200" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="200" align="center"><div align="left"></div></td>
                <td width="100"> </td>
              </tr>
          </table></td>
        </tr>
      </table>
			</form>

  
		  
		  
		  </div>
		  
		  
		  
		  
		  </td>
            
            </tr>
          </tbody>
     </table>
      

      
    
</div>
       
  </div>
  

    
</body></html>

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.