Jump to content

handling 3 imgae upload


mrjameer

Recommended Posts

hi,

i have the script that will upload 5 images in folder and also stores image path in mysql table.what happen if any body want to upload one image only or 2 images only.how can i handle it.
[code=php:0]
<?php
$siteurl = "http://localhost/ahmup";
$adminemail = "[email protected]";
$att_max_size = '50000000000000';
$att_filetypes = "gif|jpg|png|zip|swf|txt|bmp";

    $dbServer = "localhost";
$dbUser = "";
$dbPass = "";
$dbName = "ahmupco_up";
@mysql_connect($dbServer, $dbUser, $dbPass) or die("Couldn't connect to database server: " . mysql_error());
@mysql_select_db($dbName) or die("Couldn't connect to database: " . mysql_error());

if($_POST['submit'] && $_FILES['attached']['name'][0] && $_FILES['attached']['name'][1] && $_FILES['attached']['name'][2])
{
$ok_filetypes = explode("|",$att_filetypes);
if (!$_FILES['attached']['error'] && $_FILES['attached']['size'] > $att_max_size*1024){
echo "<CENTER>Sorry, but the attached file is too large. Please reduce the size of it\'s contents.</CENTER><BR><BR>";
// #err
$step = 1;
}
$filename = (!$_FILES['attached']['error'] ? substr( basename($_FILES['attached']['name'][0]), -30 ) : '');
$filename1 = (!$_FILES['attached']['error'] ? substr( basename($_FILES['attached']['name'][1]), -30 ) : '');
$filename2 = (!$_FILES['attached']['error'] ? substr( basename($_FILES['attached']['name'][2]), -30 ) : '');


$x = strtolower( substr($_FILES['attached']['name'][0], -3));
$x1 = strtolower( substr($_FILES['attached']['name'][1], -3));
$x2 = strtolower( substr($_FILES['attached']['name'][2], -3));


if($filename && !in_array($x, $ok_filetypes) ){
echo "<CENTER>Sorry, the filetype you have tried to upload is not allowed.</CENTER><BR><BR>";
$step = 1;
}
if($filename1 && !in_array($x1, $ok_filetypes) ){
echo "<CENTER>Sorry, the filetype you have tried to upload is not allowed.</CENTER><BR><BR>";
$step = 1;
}
if($filename2 && !in_array($x2, $ok_filetypes) ){
echo "<CENTER>Sorry, the filetype you have tried to upload is not allowed.</CENTER><BR><BR>";
$step = 1;
}


if(!$posterr){
if(!isset($_GET["ipaddress"]) || ($_GET["ipaddress"] == "")) {
$ipaddress = $_SERVER['REMOTE_ADDR'];
$local = 1;
} else {
$ipaddress = $_GET["ipaddress"];
$local = 0;
}
$uniq = substr( md5(uniqid (rand())), 0, 10 );
$uniq1 = substr( md5(uniqid (rand())), 0, 10 );
$uniq2 = substr( md5(uniqid (rand())), 0, 10 );


$ext = strtolower( substr($_FILES['attached']['name'][0], -3));
$ext1 = strtolower( substr($_FILES['attached']['name'][1], -3));
$ext2 = strtolower( substr($_FILES['attached']['name'][2], -3));


move_uploaded_file($_FILES['attached']['tmp_name'][0], $att_path."/".$uniq.".".$ext );
move_uploaded_file($_FILES['attached']['tmp_name'][1], $att_path."/".$uniq1.".".$ext1 );
move_uploaded_file($_FILES['attached']['tmp_name'][2], $att_path."/".$uniq2.".".$ext2 );


$strQuery  = "INSERT INTO images SET ";
$strQuery .= "filename='".$uniq.".".$ext."',";
$strQuery .= "ipaddress='{$ipaddress}',";
$strQuery .= "date='".time()."',";
$strQuery .= "pkey='{$uniq}',";

$strQuery1  = "INSERT INTO images SET ";
$strQuery1 .= "filename='".$uniq1.".".$ext1."',";
$strQuery1 .= "ipaddress='{$ipaddress}',";
$strQuery1 .= "date='".time()."',";
$strQuery1 .= "pkey='{$uniq1}',";

$strQuery2  = "INSERT INTO images SET ";
$strQuery2 .= "filename='".$uniq2.".".$ext2."',";
$strQuery2 .= "ipaddress='{$ipaddress}',";
$strQuery2 .= "date='".time()."',";
$strQuery2 .= "pkey='{$uniq2}',";


//$strQuery  = "INSERT INTO images(filename,ipaddress,date,pkey) VALUES($uniq.".".$ext,$ipaddress,'".time()."',$uniq)";

if($myuid){
$strQuery .= "user='{$myuid}',";
$strQuery1 .= "user='{$myuid}',";
$strQuery2 .= "user='{$myuid}',";

}
$strQuery .= "status='1'";
$strQuery1 .= "status='1'";
$strQuery2 .= "status='1'";


$result = mysql_query($strQuery) or die( mysql_error() );
$result1 = mysql_query($strQuery1) or die( mysql_error() );
$result2 = mysql_query($strQuery2) or die( mysql_error() );


$aid = mysql_insert_id();
// $aid1 = mysql_insert_id();

if($aid){
$filen = $siteurl."/".str_replace('./', '', $att_path)."/".$uniq.".".$ext;
$filen = str_replace('http://','%%',$filen);
$filen = str_replace('//','/',$filen);
$filen = str_replace('%%','http://',$filen);

$filen1 = $siteurl."/".str_replace('./', '', $att_path)."/".$uniq1.".".$ext1;
$filen1 = str_replace('http://','%%',$filen1);
$filen1 = str_replace('//','/',$filen1);
$filen1 = str_replace('%%','http://',$filen1);

$filen2 = $siteurl."/".str_replace('./', '', $att_path)."/".$uniq2.".".$ext2;
$filen2 = str_replace('http://','%%',$filen2);
$filen2 = str_replace('//','/',$filen2);
$filen2 = str_replace('%%','http://',$filen2);




$step = 2;
}else{
$step = 1;
}
}
}else{
$step = 1;
}
if($step == 1){
?>

[/code]
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<div align=center>
<form ENCTYPE="multipart/form-data" method="post" name="form1">
<INPUT NAME="attached[]" TYPE="file"  size="50"><br><br>
<INPUT NAME="attached[]" TYPE="file"  size="50"><br><br>
<INPUT NAME="attached[]" TYPE="file"  size="50"><br><br>
<input type="submit" name="submit" value="submit">
</form>
</div>
</td>
</tr>
</table>
<?
}else{ ?>
<div align="center">
  <br>
</div>
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center">
<img src="<?=$filen?>">&nbsp;<img src="<?=$filen1?>">&nbsp;<img src="<?=$filen2?>">&nbsp;

</div></td>
</tr>
<tr>
<td><div align="center"><br>

<textarea name="textarea" cols="100" wrap="soft" rows="3">
[url=http://<?=$siteurl?>][img]http://<?=$filen?>[/img][/url]
[url=http://<?=$siteurl?>][img]http://<?=$filen1?>[/img][/url]
[url=http://<?=$siteurl?>][img]http://<?=$filen2?>[/img][/url]

</textarea>
</div></td>
</tr>
<tr>
<td><div align="center"><br>

        <textarea name="textarea2" cols="100" rows="4"><?=$filen?><?=$filen1?><?=$filen2?>
</textarea>
      </div></td>
</tr>
<tr>
<td><div align="center"><br>
<br>
<textarea name="textarea3" cols="100" wrap="soft" rows="3">
<a href="<?=$siteurl?>" target="_blank">
<img alt="Image Hosted by <?=$sitename?>" src="<?=$filen?>" /></a>
<a href="<?=$siteurl?>" target="_blank">
<img alt="Image Hosted by <?=$sitename?>" src="<?=$filen1?>" /></a>

<a href="<?=$siteurl?>" target="_blank">
<img alt="Image Hosted by <?=$sitename?>" src="<?=$filen2?>" /></a>



</textarea>
</div></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
<? } ?>
Link to comment
https://forums.phpfreaks.com/topic/34150-handling-3-imgae-upload/
Share on other sites

first of all you dont have to repeat code the way you are doing. Use a for loop or make a function. My saying is: If you use the same code more than once, make a function.

Then you also could control how many images you want to upload.

Would be smart to make a form where you let the user type the amout of images he/she wants to upload. Then you could use that number to define a for loop:

$images_to_upload = 3

for($i=0; $i < $images_to_upload; $i++)
hi,

i have put all the code in for loop so that the user can dynamically create the upload boxes and upload the images.everything is ok.but the problem is i want to display the uploaded images.how i can put them in a loop.any of your help will be appreciated.i have used following line for display but it is displaying same image many times instead of displaying uploaded images.
<?php for($a=0;$a<$uploadNeed;$a++){ echo "<img src='".$filen."'>";}?>

thanks
mrjameer


upload6.php
[code=php:0]
<?php


if( $_POST['submit'])
{
//$uploads = count($_FILES['uploadFile'.$y]['name']);
//$countUploads = count( $_FILES['uploadFile'. $x]['name']);
for($a=0;$a<$uploadNeed;$a++)
{
$ok_filetypes = explode("|",$att_filetypes);
if (!$_FILES['uploadFile'. $a]['name'] && $_FILES['uploadFile'. $a]['name'] > $att_max_size*1024)
{
errform('<CENTER>Sorry, but the attached file is too large. Please reduce the size of it\'s contents.</CENTER><BR><BR>'); // #err
$step = 1;
}
$filename = (!$_FILES['uploadFile'. $a]['name'] ? substr( basename($_FILES['uploadFile'. $a]['name']), -30 ) : '');

$x = strtolower( substr($_FILES['uploadFile'. $a]['name'], -3));
if($filename && !in_array($x, $ok_filetypes) )
{
errform('<CENTER>Sorry, the filetype you have tried to upload is not allowed.</CENTER><BR><BR>');
$step = 1;
}
if(!$posterr)
{
if(!isset($_GET["ipaddress"]) || ($_GET["ipaddress"] == ""))
{
$ipaddress = $_SERVER['REMOTE_ADDR'];
$local = 1;
}
else
{
$ipaddress = $_GET["ipaddress"];
$local = 0;
}

$uniq = substr( md5(uniqid (rand())), 0, 10 );

$ext = strtolower( substr($_FILES['uploadFile'. $a]['name'], -3));


move_uploaded_file($_FILES['uploadFile'. $a]['tmp_name'], $att_path."/".$uniq.".".$ext );
 

//move_uploaded_file($_FILES['attached']['tmp_name'], $att_path."/".$uniq.".".$ext );

$strQuery  = "INSERT INTO images SET ";
$strQuery .= "filename='".$uniq.".".$ext."',";
$strQuery .= "ipaddress='{$ipaddress}',";
$strQuery .= "date='".time()."',";
$strQuery .= "pkey='{$uniq}',";

if($myuid)
{
$strQuery .= "user='{$myuid}',";
}
$strQuery .= "status='1'";
$result = mysql_query($strQuery) or die( mysql_error() );
$aid = mysql_insert_id();
if($aid)
{
$filen = $siteurl."/".str_replace('./', '', $att_path)."/".$uniq.".".$ext;
$filen = str_replace('http://','%%',$filen);
$filen = str_replace('//','/',$filen);
$filen = str_replace('%%','http://',$filen);

$step = 2;
}


else
{
$step = 1;
}

}

}
}
else{
$step = 1;
}

if($step == 1)
{
?>
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<div align=center>
<form ENCTYPE="multipart/form-data" method="post" name="form1">
<p>
  <?
  // start of dynamic form
  $uploadNeed = $_POST['uploadNeed'];
  for($y=0;$y<$uploadNeed;$y++){
  ?>
    <input name="uploadFile<? echo $y;?>" type="file" id="uploadFile<? echo $y;?>">
  </p>
  <?
  // end of for loop
  }
  ?>
  <p><input name="uploadNeed" type="hidden" value="<? echo $uploadNeed;?>">
<input type="submit" name="submit" value="submit">
</form>
</div>
</td>
</tr>
</table>
<?
}else{ ?>

<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center">
<!--here i want to display all the images which are uploaded now-->
<!--here i want to display all the images which are uploaded now-->
<!--here i want to display all the images which are uploaded now-->
<?php for($a=0;$a<$uploadNeed;$a++){ echo "<img src='".$filen."'>";}?>
</div></td>
</tr>
<tr>
<td> <textarea name="textarea" cols="100" wrap="soft" rows="3">
[url=http://<?=$siteurl?>][img]http://<?=$filen?>[/img][/url]


</textarea>
</div></td>
</tr>
<tr>
<td>
        <textarea name="textarea2" cols="100" rows="4">
<?=$filen?></textarea>
      </div></td>
</tr>
<tr>
<td>
<textarea name="textarea3" cols="100" wrap="soft" rows="3">
<a href="<?=$siteurl?>" target="_blank"><img alt="Image Hosted by <?=$sitename?>" src="<?=$filen?>" /></a>

</textarea>
</div></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
<? } ?>

[/code]
upload.php
<body>
<form name="form1" method="post" action="upload6.php">
  <p>Enter the amount of boxes you will need below. Max = 9.</p>
  <p>
    <input name="uploadNeed" type="text" id="uploadNeed" maxlength="1">
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
</body>

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.