/    Sign up×
Community /Pin to ProfileBookmark

handling 3 image upload

hi,

i have the script that will upload 3 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]
<?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 = “bbb”;
@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=<?=$siteurl?>][img]<?=$filen?>[/img][/url]
[url=<?=$siteurl?>][img]<?=$filen1?>[/img][/url]
[url=<?=$siteurl?>][img]<?=$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>

<? } ?>

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@bokehJan 14.2007 — Use a loop as explained in the sticky

[code=php]foreach($active_keys as $key)
{
@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key]);
}[/code]
Copy linkTweet thisAlerts:
@roshanjameerauthorJan 16.2007 — 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]
<?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=<?=$siteurl?>][img]<?=$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>
×

Success!

Help @roshanjameer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...