Page 1 of 1

Image upload function

Posted: Mon Jan 03, 2011 4:10 pm
by viji123
Hi,
I am developing a website using php and mysql. I am validating a form using javascript. In the same form I need to upload an image and store in a seperate folder. If I use the php upload function Javascript validations are not working. How can I achieve this by using both javascript and the PHP code. Can anybody please help me. It will be a great help to me.
Regards,
Viji

Re: Image upload function

Posted: Mon Jan 03, 2011 4:14 pm
by Neilos
It is usually helpful to see the code you have so far so that people can offer you advice based on your code and give you solutions that will work for your individual problem. Could you post your code please?

Re: Image upload function

Posted: Tue Jan 04, 2011 11:05 am
by viji123
Hi,
Thank you very much for your replies. Please find the code.

Code: Select all

<?php
if(isset($_POST["action"]) == "Upload Image")
{
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}
?>


<script type="text/javascript">
function displaymessage()
{
//Some validation code
var flag=0;
if(filepath!="")
{
alert(<?php echo("hiii") ?>);
}
if(str=="")
{
alert("Please enter the Company name.");
document.myform.txtCompanyname.focus();
flag=1;
return false;
}
...........................
.........................
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4)
    {
    alert(xmlhttp.responseText);
    }
  }
xmlhttp.open("POST","<?php bloginfo('template_url'); ?>/Vendorinformation.php?",true);
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhttp.send(Datatosend);
<?PHP echo("string"); ?> 
location.href="http://www.thillaimadhunayanar.com/?page_id=29";
}
</script>

<FORM name="frmvendor" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
<tr valign="top">
<td><h2><a href="<?php the_permalink() ?>" rel="bookmark" title="ראשי <?php the_title(); ?>">Vendor-Join us</a></h2>

<tr><td class="label2">Logo</span></td>
<td class="label1"><input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> </td></tr>

......................................................................................
......................................................................................

<tr> 
<td width="246">
<p><input type="file" name="image_file" size="20"></p>
<p><input type="submit" value="Upload Image" name="action"></p></td>
</tr>


<tr><td colspan="2">
<?php
echo '<input type="button" name="submit" id="submit" onclick="displaymessage()">';
?>
</td></tr>
			</tbody>
		</table>

Thanks,
Regards,
Viji

Re: Image upload function

Posted: Tue Jan 04, 2011 4:38 pm
by social_experiment

Code: Select all

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
//
?>
Checking the type of image isn't a foolproof method of ensuring only image files are uploaded. Rather tell the user only to upload one type (which is up to you) and enforce this by forcing the extension.

Code: Select all

<?php
 $fileName = $_FILES['file']['name'];
 $extension = '.jpg';
 $newFileName = $fileName.$extension;
 // assuming your original file name was 'peter', the value
 // of $newFileName will be peter.jpg
?>
If the type is the same as your forced extension there won't be any problems on display and you are certain only jpg files are uploaded. Probably a good idea to upload to outside the root folder as well as changing the filename.