Hi, i have an upload script written in html and php as follows:
[code=php]
<form action=”<? $PHP_SELF ?>” method=”post” enctype=”multipart/form-data” name=”form” />
<label for=”file” > Filename: </label>
<input type=”file” name=”file” id=”file” class=”registration” />
<br />
<input type=”submit” name=”Submit” value=”Submit” onclick=”LimitAttach(this.form, this.form.file.value)” />
<p>
<?php
if (!isset($_POST[Submit])){
print “You need to select a file and click submit. “;
}
elseif ((($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”))
&& ($_FILES[“file”][“size”] < 20000)){
if ($_FILES[“file”][“error”] > 0){
echo “Error: ” . $_FILES[“file”][“error”] . “<br />”;
}
else{
print “Upload: ” . $_FILES[“file”][“name”] . “<br />”;
print “Type: ” . $_FILES[“file”][“type”] . “<br />”;
print “Size: ” . ($_FILES[“file”][“size”] / 1024) . ” Kb<br />”;
print “Stored in: ” . $_FILES[“file”][“tmp_name”];
}
}
else{
echo “Invalid file type or size.”;
}
?>
Along with the bit of javascript on the submit button tag i also have this in the header (copied from another source…):
[CODE]<SCRIPT LANGUAGE=”JavaScript”>
<!– Original: ArjoGod, Shauna Merritt –>
<!– Modified By: Ronnie T. Moore, Editor –>
<!– This script and many more are available free online at –>
<!– The JavaScript Source!! http://javascript.internet.com –>
<!– Begin
extArray = new Array(“.jpg”, “.jpeg”);
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf(“\”) != -1)
file = file.slice(file.indexOf(“\”) + 1);
ext = file.slice(file.indexOf(“.”)).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) form.submit();
else
alert(“Please only upload files that end in types: ”
+ (extArray.join(” “)) + “nPlease select a new ”
+ “file to upload and submit again.”);
}
// End –>
</script>
Now, the problem is if an image that does not end in .jpg is uploaded, the javascript does display the error message, but the page continues to load and return the “wrong file type error”. So is there anyway to stop the php script running if the alert dialog comes up?
Thanks for any help.