Image Upload

how to implement image upload in add from form with uniqueness

Page 1 of 1

2 Replies - 2779 Views - Last Post: 30 November 2008 - 03:05 PM Rate Topic: -----

#1 kapilje   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-November 08

Image Upload

Posted 29 November 2008 - 09:42 PM

addProduct.php

<?php 
require_once('../include/adminheader.php');
require_once('../fckeditor/fckeditor.php');?>
<?php

if(isset($_POST['Add']))
{
	//print_r($_REQUEST);
//exit();
 $Section =$_POST['Section'];
 $subSection = $_POST['subvalue'];
 $pname= $_POST['pname'];
 $filename=$_POST['pimage'];
$sValue = $_POST['FCKeditor1'] ;
$sValue1 = $_POST['FCKeditor2'] ;




function ext($var) // Takes file extension from file
{
return substr($_FILES["$var"]["name"],-3);
}
$directorio = "./images/";
// using for getting the uniqimage id
$uniq = uniqid("");// using for getting the uniqu imageid
//throw this we can use same nameimage with uniqeid

$archivo = $_FILES["pimage"]["name"];

//echo "name".$archivo;


$filename = $uniq.$archivo; //in this($filename)var we can add store same
//name image with uniqeid

//echo "uq9".$filename;
$size = $_FILES["pimage"]["size"];
//echo "ss".$size;

$extension = ext("pimage");

//echo "ef".$extension;

if($extension != "jpg" && $extension != "gif" && $extension != "png")
{
 "Invalid file type!";
}

elseif($size > "1024000")// size of the images

{
echo "The file must be less than 1024kb";
}
elseif(file_exists("$directorio/$filename"))
{
echo "There is already another file with the same name. Try another one";
}
else
{
move_uploaded_file($_FILES["pimage"]["tmp_name"], $directorio.$filename);
 "<br />File successfully uploaded!\n";
 "<br />Name: ".$filename."\n";
 "<br />Size: ".$size."\n";
"<br />Type: ".$extension."\n";
}







 $q= "insert into product set sec='$Section',sub='$subSection', p_name ='$pname', p_image='$filename', p_details ='$sValue', p_spec ='$sValue1' ";

$r= mysql_query($q);
if(mysql_insert_id())
Header('location:list_Product.php');
exit();
}
?>

<script type="text/javascript">
function validate_check()
{
if( document.form1.Section.value == '')
{  alert ( "Please Select Section Name." );
 return false;
}

else if(document.getElementById('Sub').value == '')
{  
 alert ( "Please Select Sub-Section Name." );
  var myValue = document.getElementById('Sub').value;
  //alert(myValue);
 return false;
}

else if( document.form1.pname.value == '')
{  alert ( "Please Enter Product Name." );
		document.form1.pname.focus();
		return false;
}
//else if( document.form1.FCKeditor1.value == '' )
//{ 
	   // alert ( "Please Enter Product Details." );
	   //document.form1.FCKeditor1.focus();
	  // return false;
	   
	   //}

if(document.getElementById('Sub').value != '')
{  
  var myValue = document.getElementById('Sub').value;
 // alert(myValue);
	document.form1.subvalue.value = myValue;
  // alert(document.form1.subvalue.value); 
  //document.getElementById("Sub").value = myValue;
  return true;
}

return true;
}
</script>
<script language="javascript" type="text/javascript">
function getXMLHTTP()
 { 

		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}}}return xmlhttp;
   } function getSubSection(s_id) {		
		
		var strURL="findSubSection.php?s_id="+s_id;
		
		var req = getXMLHTTP();
		if (req) 
			{req.onreadystatechange = function()
				{if (req.readyState == 4) 
					{if (req.status == 200)
						{						
						document.getElementById('SubSection').innerHTML=req.responseText;						
					
					} else 
						{
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}		
	}		
</script>


<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tbody><tr>
		<td>
			<table width="100%" align="left" border="0" cellpadding="0" cellspacing="0">
				<tbody><tr>
				<td> </td>
				</tr>
				<tr><td></td></tr>
			</tbody></table>
		</td></tr>
	<tr><td><b></b></td></tr>
<tr><td>

<form name="form1" method="post" action="AddProduct.php"  enctype="multipart/form-data">

<td><fieldset>

<legend color="darkblue">Add Product </legend>

<TABLE border='0' cellspacing='3' cellpadding='3' align='center' width='100%'>
<TR>
	<TD align='left'>Select Section </TD>
	<td  align='left'><font face='verdana, arial, helvetica' size='2' >
	<select name="Section" onchange="getSubSection(this.value)">
	<option value=''>Select Section</option>
		<?php

		$q = "select * from section order by s_name";
		$result= mysql_query($q);
	  
	  while($row2=mysql_fetch_array($result)) 
	  { 
	  ?>
	  <option value="<?php echo $row2[0]; ?>"><?php echo $row2[1]; ?></option>
	  <?php } ?>
	</font></td>
	<TD>  </TD>
</TR>
<TR>
	<TD align='left'>Select&nbsp;Sub-Section </TD>
	<td  align='left'><font face='verdana, arial, helvetica' size='2' >&nbsp;&nbsp;
  <div id="SubSection"> 
  <select name='Sub' id='Sub'>

	<option value=''>Select Section First</option>
		</select></div></font></td>
	<TD>  </TD>
</TR>
<TR>
	<TD align='left'>Product Name </TD>
	<td  align='left'><font face='verdana, arial, helvetica' size='2' >
	 <input type ='text'  name='pname' id='pname'></font></td>
	<TD>  </TD>
</TR>

<TR>
	<TD align='left'>Product Image</TD>
	<td  align='left'><font face='verdana, arial, helvetica' size='2' >
	 <input type ='file'  name='pimage' id='pimage'></font></td>
	<TD>  </TD>
</TR>

<TR>
	<TD align='left'>Details</TD>
	
	<td  align='left' colspan='2'><font face='verdana, arial, helvetica' size='2' >&nbsp;&nbsp;&nbsp;
 <?php 

 $sValu ='';

$oFCKeditor = new FCKeditor('FCKeditor1');
$oFCKeditor->BasePath = '../fckeditor/';
$oFCKeditor->Width  = '95%';
$oFCKeditor->Height = '400';
$oFCKeditor->Value = $sValu;
$oFCKeditor->Create();

?>
</font></td>
	
</TR>

<TR>
	<TD align='left'>Specifications</TD>
	
	<td  align='left' colspan='2'><font face='verdana, arial, helvetica' size='2' >&nbsp;&nbsp;&nbsp;
 <?php 

 $sValu1 ='';

$oFCKeditor = new FCKeditor('FCKeditor2');
$oFCKeditor->BasePath = '../fckeditor/';
$oFCKeditor->Width  = '95%';
$oFCKeditor->Height = '400';
$oFCKeditor->Value = $sValu1;
$oFCKeditor->Create();

?>
</font></td>
	
</TR>




<TR>
	<td> </td>
	<td  align='left' colspan='2' ><font face='verdana, arial, helvetica' size='2' >
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="submit" name="Add" value="Submit" onclick="return validate_check();">

	<input type="hidden" name="subvalue" id="subvalue" value="Sub" /> 
 </font></td>
	<TD></TD>
</TR>





</TABLE></fieldset>



</td>
</form>

		</td>
	</tr>
	<tr class="link" align="left" bgcolor="#ffffff">
		<td class="normal" colspan="2" width="100%" align="left">&nbsp;</td>
	</tr>
</tbody>

</table>

<?php 
include_once ('../include/footer.php')
?> 






editProduct.php

<?php 
require_once('../include/adminheader.php');
require_once('../fckeditor/fckeditor.php');
?>


<?php

if(isset($_POST['Update']))
{
$id=$_GET['id'];

$Section =$_POST['Section'];
$subSection = $_POST['Sub'];
$pname= $_POST['pname'];
$filename=$_POST['pimage'];
$sValue = $_POST['FCKeditor1'];
$sValue1 = $_POST['FCKeditor2'];


function ext($var) // Takes file extension from file
{
return substr($_FILES["$var"]["name"],-3);
}
$directorio = "./images/";
// using for getting the uniqimage id
$uniq = uniqid("");// using for getting the uniqu imageid
//throw this we can use same nameimage with uniqeid

$archivo = $_FILES["pimage"]["name"];

//echo "name".$archivo;


$filename = $uniq.$archivo; //in this($filename)var we can add store same
//name image with uniqeid

//echo "uq9".$filename;
$size = $_FILES["pimage"]["size"];
//echo "ss".$size;

$extension = ext("pimage");

//echo "ef".$extension;

if($extension != "jpg" && $extension != "gif" && $extension != "png")
{
 "Invalid file type!";
}

elseif($size > "1024000")// size of the images

{
 "The file must be less than 1024kb";
}
elseif(file_exists("$directorio/$filename"))
{
 "There is already another file with the same name. Try another one";
}
else
{
move_uploaded_file($_FILES["pimage"]["tmp_name"], $directorio.$filename);
 "<br />File successfully uploaded!\n";
 "<br />Name: ".$filename."\n";
 "<br />Size: ".$size."\n";
 "<br />Type: ".$extension."\n";
}





$q1= sprintf("update product set sec='$Section',sub='$subSection' ,p_name ='$pname',p_image='$filename',p_details='$sValue',p_spec='$sValue1' where pid=".$id);
//exit();
$r= mysql_query($q1);
"Updated Successfully";

Header('location:list_Product.php');
exit();
}?>

<script type="text/javascript">
function validate_check()
{

 if( document.form1.pname.value == '')
{  alert ( "Please Enter Product Name." );
		document.form1.pname.focus();
		return false;
}
else if( document.form1.FCKeditor11.value == '' || document.form1.FCKeditor11.value == null  || document.form1.FCKeditor11.value == '<p> </p>')
{ 
	 alert ( "Please Enter Product Details." );
		document.form1.FCKeditor11.focus();
		return false;
}
return true;
}
</script>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tbody><tr>
		<td>
			<table width="100%" align="left" border="0" cellpadding="0" cellspacing="0">
				<tbody><tr>
				<td> </td>
				</tr>
				<tr><td></td></tr>
			</tbody></table>
		</td></tr>
	<tr><td><b></b></td></tr>
<tr><td>

<?php

$id=$_GET['id'];
$sql="SELECT * FROM product WHERE pid='$id'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

$sec_id= $row['sec'];
$sub_id= $row['sub'];
$pname=  $row['p_name'];
$filename=$row['p_image'];
$p_details = $row['p_details'];
$p_spec = $row['p_spec'];

?>



<form name="form1" method="post" action='editProduct.php?id=<?php echo $id;?>' enctype="multipart/form-data" >

<td><fieldset>

<legend color="darkblue">Edit Product </legend>

<TABLE border='0' cellspacing='3' cellpadding='3' align='center' width='100%'>
<TR>
	<TD align='left'>Select Section </TD>
	<td  align='left'><font face='verdana, arial, helvetica' size='2' >
	<select name="Section" >
		<?php

		$q = "select * from section order by s_name";
		$result= mysql_query($q);
	  
	  while($row2=mysql_fetch_array($result)) 
	  { 
	  ?>
	  <option value=<?php echo $row2[0]; ?> <?php if($row2['sid']==$row['sec']){echo "selected";}?>><?php echo $row2[1]; ?></option>
	  <?php } ?>
	</font></td>
	<TD>  </TD>
</TR>
<TR>
	<TD align='left'>Select Sub Section </TD>
	<td  align='left'><font face='verdana, arial, helvetica' size='2' >
	<select name='Sub' id='Sub' >
		<?php

		$q21 = "select * from sub_section order by subname";
		$result21= mysql_query($q21);
	  
	  while($row21=mysql_fetch_array($result21)) 
	  { 
	  ?>
	  <option value=<?php echo $row21[0]; ?> <?php if($row21['subid']==$row['sec']){echo "selected";}?>><?php echo $row21[2]; ?></option>
	  <?php } ?>
	</font></td>
	<TD>  </TD>
</TR>
<TR>
	<TD align='left'>Product Name </TD>
	<td  align='left'><font face='verdana, arial, helvetica' size='2' >
	 <input type ='text'  name='pname' id='pname' value ='<?php echo $pname; ?>' ></font></td>
	<TD>  </TD>
</TR>

<TR>
	<TD align='left'>Image Preview</TD>
	<td  align='left'><font face='verdana, arial, helvetica' size='2' >
	
	 <img src = "./images/<?php echo $row['p_image']; ?>"  height=200 width=200 alt="<?php echo $row['p_image']; ?>" >

	
	<TD>  </TD>
</TR>



 
<TR>
	<TD align='left'>Product Image</TD>
	<td  align='left'><font face='verdana, arial, helvetica' size='2' >
	 
 		 
 <input type='file' name='pimage' id='pimage' value='' ></font></td>
 		
		
 		
 		
	
	<TD>  </TD>
</TR>  



<TR>
	<TD align='left'>Details</TD>
	
	<td  align='left' colspan='2'><font face='verdana, arial, helvetica' size='2' >&nbsp;&nbsp;&nbsp;
 <?php 

 $sValu ='';

$oFCKeditor = new FCKeditor('FCKeditor1');
$oFCKeditor->BasePath = '../fckeditor/';

$oFCKeditor->Width  = '95%';
$oFCKeditor->Height = '400';
$oFCKeditor->Value = $row['p_details'];
$oFCKeditor->Create();

?>
</font></td>	
</TR>

<TR>
	<TD align='left'>Specifications </TD>
	
	<td  align='left' colspan='2'><font face='verdana, arial, helvetica' size='2' >&nbsp;&nbsp;&nbsp;
 <?php 

 $sValu1 ='';

$oFCKeditor = new FCKeditor('FCKeditor2');
$oFCKeditor->BasePath = '../fckeditor/';

$oFCKeditor->Width  = '95%';
$oFCKeditor->Height = '400';
$oFCKeditor->Value = $row['p_spec'];
$oFCKeditor->Create();

?>
</font></td>	
</TR>
<TR>
	<td> </td>
	<td  align='left' colspan='2' ><font face='verdana, arial, helvetica' size='2' >
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="submit" name="Update" value="Submit" onclick="return validate_check();">
 </font></td>
	<TD></TD>
</TR>

</TABLE></fieldset>
</td>
</form></td>
	</tr>
	<tr class="link" align="left" bgcolor="#ffffff">
		<td class="normal" colspan="2" width="100%" align="left">&nbsp;</td>
	</tr>
</tbody>
</table>

<?php 
include_once ('../include/footer.php')
?>


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 29 November 2008 - 10:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Image Upload

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5238
  • View blog
  • Posts: 14,031
  • Joined: 18-April 07

Re: Image Upload

Posted 29 November 2008 - 10:02 PM

Can you elaborate on the problem you are having? The topic is not the greatest place to describe an entire problem in. We know you want to implement file uploading, but what do you mean by uniqueness?

Like files with no same name? Or the picture cannot be the same?

Please elaborate on what you are having troubles with. :)
Was This Post Helpful? 0
  • +
  • -

#3 akozlik   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 798
  • Joined: 25-February 08

Re: Image Upload

Posted 30 November 2008 - 03:05 PM

I'm with Martyr2 in that your question is way to vague. Can you clarify?

One common problem people have when uploading images from forms is to forget the enctype="multipart/form-data" attribute of the form

<form action="whatever.php" method="post" enctype="multipart/form-data">
<!-- Form information -->
</form>



Second, if you're looking to ensure uniqueness for an image you could always append the current timestamp to the filename. That will ensure that you always get a unique filename. On occasion I'll append the timestamp to the filename, and then md5() the whole thing. You wind up with an illegible filename, but it's unique and you'll know the size of the filename which can help for database optimizing.

Hope that helps some.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1