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
Image upload function
Moderator: General Moderators
Re: Image upload function
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
Hi,
Thank you very much for your replies. Please find the code.
Thanks,
Regards,
Viji
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
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Image upload function
Code: Select all
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
//
?>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
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering