zokarcan
08-27-2003, 05:16 PM
This javascript codes is not ok. Where is the error. please help.
<html>
<head>
<title>UPLOAD</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
function check(){
var file=document.form1.pic1.value
if (file.size > 10000)
{
alert("File must be smaller then 100 KB.");
return false
}
}
</script>
</head>
<body>
<form action="upload.asp" method="post" enctype="multipart/form-data"
name="form1" onsubmit="return check();">
<input type="file" name="pic1">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
Charles
08-27-2003, 05:23 PM
"document.form1.pic1.value" is a string that represents the file name selected. Strings do not have a "size" property. You cannot just go around making up things and you cannot get the size of the file the user wants to upload.
zokarcan
08-27-2003, 05:36 PM
how can I check file size with javascript?
gcrowan
08-27-2003, 05:43 PM
I wish I had time to write out the code you need but the problem is in the "var file=document.form1.pic.value". The value of the input is the file pathway. Ex. images/image.gif.
The file pathway doesn't have a size property. You will need to use image.width and image.height to check for the correct size. You should also write in a check for the correct file type so you won't have someone try to upload an .exe file. Sorry I'm being kinda cryptic but I have to run. Later I can give you a nice php upload script if you're interested.
<html>
<head>
<title>UPLOAD</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
function check(f){
var file=f.pic1.value
if (file.size > 10000)
{
alert("File must be smaller then 100 KB.");
return false
}
}
</script>
</head>
<body>
<form action="upload.asp" method="post" enctype="multipart/form-data"
name="form1" onsubmit="return check(this);">
<input type="file" name="pic1">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
zokarcan
08-27-2003, 05:56 PM
It doesnt work this javascript codes
Charles
08-27-2003, 06:05 PM
Originally posted by zokarcan
how can I check file size with javascript? You cannot.
gcrowan
08-27-2003, 06:30 PM
Of course it doesn't work. Try this.
<?php
$my_max_file_size = "50000"; # in bytes
$image_max_width = "300";
$image_max_height = "350";
$the_path = "/images";
$error = false;
$registered_types = array(
"application/x-gzip-compressed" => ".tar.gz, .tgz",
"application/x-zip-compressed" => ".zip",
"application/x-tar" => ".tar",
"text/plain" => ".html, .php, .txt, .inc (etc)",
"image/bmp" => ".bmp, .ico",
"image/gif" => ".gif",
"image/pjpeg" => ".jpg, .jpeg",
"image/jpeg" => ".jpg, .jpeg, .JPG, .JPEG",
"application/x-shockwave-flash" => ".swf",
"application/msword" => ".doc",
"application/vnd.ms-excel" => ".xls",
"application/octet-stream" => ".exe, .fla (etc)"
); # these are only a few examples, you can find many more!
$allowed_types = array("image/bmp","image/gif","image/pjpeg","image/jpeg");
# --
function validate_upload($the_file) {
global $error,$my_max_file_size, $image_max_width, $image_max_height,$allowed_types,$the_file_type,$registered_types;
$start_error = "Error:";
if ($the_file == "") { # do we even have a file?
$error .= " You did not upload anything!";
return $error;
}else { # check if we are allowed to upload this file_type
if (!in_array($the_file_type,$allowed_types)) {
$error .= " The file that you uploaded was of a type that is not allowed, you are only allowed to upload files of the type: ";
while ($type = current($allowed_types)) {
$error .= " " . $registered_types[$type] . " (" . $type . ")";
next($allowed_types);
}
return $error;
}
if (ereg("image",$the_file_type) && (in_array($the_file_type,$allowed_types))) {
$size = GetImageSize($the_file);
list($foo,$width,$bar,$height) = explode("\"",$size[3]);
if ($width > $image_max_width) {
$error .= " Your image should be no wider than " . $image_max_width . " Pixels";
return $error;
}
if ($height > $image_max_height) {
$error .= " Your image should be no higher than " . $image_max_height . " Pixels";
return $error;
}
}
if ($error){
$error = $start_error . $error;
return $error;
}
}
} # END validate_upload
# --
function upload($the_file) {
global $the_path,$the_file_name;
$error = validate_upload($the_file);
if (!$error){ # no error we can continue
if (!@copy($the_file, $the_path . "/" . $the_file_name)) {
$error .= "\nSystem failure, check the path to and the permissions for the upload directory";
unset($update);
}
}
} # END upload
if(isset($task)){
if($task=="upload"&&$the_file!=""){
upload($the_file);
}
}
?>
<html>
<head>
<title></title>
<?php
if($error){
echo "<script language=\"JavaScript\" type=\"text/javascript\">\r\n";
echo "alert(\"".$error."\");\r\n";
echo "</script>\r\n";
}
?>
</head>
<body>
<form action="<? echo $PHP_SELF " method="post" enctype="multipart/form-data">
<input type="hidden" name="task" value="upload">
<input type="file" name="the_file" size="17">
<input type="submit" value="Submit">
</form>
</body>
</html>