I have this upload form working great. I am using the "\'" format to get single quotes from now on. But, I added a script to check the file extension and it wont upload anything. Anybody see anything wrong here...? It bring the error message up regardless it the file has a .pdf extension or not and wont uplaod.

--

<?php

echo '<html>
<head>
<title>test page</title>

<script type="text/JavaScript">
<!-- Begin
function TestFileType( fileName, fileTypes ) {
if (!fileName) return;
dots = fileName.split(".")
fileType = "." + dots[dots.length-1];
return (fileTypes.join(".").indexOf(fileType) != -1) ?
alert(\'That file is OK!\') :
alert("Please only upload PDF format files with the correct .pdf extension. Please select a new file and try again.");
}
// -->
</script>

</head>

<body>
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="hidden" name="filename" value="'.$name.'">
<input type="file" name="uploadfile">
<input type="button" name="Submit" value="Upload" onclick="TestFileType(this.form.uploadfile.value, [\'pdf\'])"><br>
</body>
</html>
';

?>

--

    Why not use this to check the file type?

    $file_type = $_FILES['uploadfile']['type'];
    $allowed = array("application/pdf"); //used to let you add more
    
    if(!in_array($file_type, $allowed)){ 
    //return false
    } else {
    //return true
    }
    

      two things one watch out for using quotes for example you have started the echo with a single quote i would use double then change all inside to single quotes.
      your problem is with ...
      onclick="TestFileType(this.form.uploadfile.value, ['pdf'])"
      this should be ....
      onclick="TestFileType(this.form.uploadfile.value, ['.pdf'])"

      i got the final code to be...

      <?
      echo "
      <html>
      <head>
      <title>test page</title>
      
      <script language=\"javascript\">
      function TestFileType( fileName, fileTypes ) {
      if (!fileName) return;
      dots = fileName.split(\".\")
      fileType = \".\" + dots[dots.length-1];
      return (fileTypes.join(\".\").indexOf(fileType) != -1) ?
      alert(\"That file is OK!\") :
      alert(\"Please only upload PDF format files with the correct .pdf extension. Please select a new file and try again.\");
      }
      </script>
      
      </head>
      
      <body>
      <form method=\"post\" action=\"upload.php\" enctype=\"multipart/form-data\">
      <input type=\"file\" name=\"uploadfile\">
      <input type=\"hidden\" name=\"filename\" value=\"{$name}\">
      <input type=\"button\" name=\"submit\" value=\"Upload\" onClick=\"TestFileType(this.form.uploadfile.value, ['.pdf'])\">
      </form>
      <br>
      </body>
      </html>";
      ?>
      

      but i would not use php at all to do this i would just use.

      <html>
      <head>
      <title>test page</title>

      <script language="javascript">
      function TestFileType( fileName, fileTypes ) {
      if (!fileName) return;
      dots = fileName.split(".")
      fileType = "." + dots[dots.length-1];
      return (fileTypes.join(".").indexOf(fileType) != -1) ?
      alert("That file is OK!") :
      alert("Please only upload PDF format files with the correct .pdf extension. Please select a new file and try again.");
      }
      </script>

      </head>

      <body>
      <form method="post" action="upload.php" enctype="multipart/form-data">
      <input type="hidden" name="filename" value="<?php echo $name ?>">
      <input type="file" name="uploadfile">
      <input type="button" name="submit" value="Upload" onClick="TestFileType(this.form.uploadfile.value, ['.pdf'])">
      </form>
      <br>
      </body>
      </html>

      there is no point in putting all that in an echo.
      hope that helps.

        .php! A stupid period was making me loose hair...

        So I dont need the entire page to be a php script, I can simply call php just to get the data from the one field? Cool...

        THANKS!

          This gets a step farther. The message saying that file is OK comes up but the file does not start uploading after clicking OK. I dont use JS much, sorry to bug you with this...

          Thanks!

            a bit more tricky but i think i have done it........

            <html>
            <head>
            <title>test page</title>
            
            <script language="javascript">
            function TestFileType( fileName, fileTypes ) {
            if (!fileName) return;
            dots = fileName.split(".")
            fileType = "." + dots[dots.length-1];
            if (fileTypes.join(".").indexOf(fileType) != -1) 
            {
            alert("That file is OK!");
            return true;
            }
            else
            {
            alert("Please only upload PDF format files with the correct .pdf extension. Please select a new file and try again.");
            return false;
            }
            }
            
            
            </script>
            
            </head>
            
            <body>
            <form method="post" action="upload.php" enctype="multipart/form-data" name="myform" onSubmit="return TestFileType(myform.uploadfile.value,['.pdf'])">
            <input type="hidden" name="filename" value="<?php echo $name; ?>">
            <input type="file" name="uploadfile">
            <input type="submit" name="submit" value="Upload">
            </form>
            <br>
            </body>
            </html>
            

            i think that has done it.....

              Write a Reply...