It seem to be I have a problem on that code as this only go though the first one and then stopped...so other file extension is not allow...

//Check the file extension
if (($_FILES["file"]["type"] == "application/pdf") 
|| ($_FILES["file"]["type"] == "application/doc") 
|| ($_FILES["file"]["type"] == "application/docx")
|| ($_FILES["file"]["type"] == "application/ppt")
|| ($_FILES["file"]["type"] == "application/pptx") 
&& ($_FILES["file"]["size"] < 20000000))
{

Recommended Answers

All 8 Replies

All I can think of off hand is that the files on this must be under 1mb which is easy for .pdf's with graphics to surpass... I'd start with some basic troubleshooting techniques.

for instance:

if($_FILES['file']['error']==0){
    $flag = $flag+1;
    echo 'No errors reported on file upload<br>';
    }
if($_FILES['file']['size']<1000000){
    $flag = $flag+1;
    echo 'Size is confirmed under 1mb<br>';
    }
if(($_FILES['file']['type']=="application/msword")||($_FILES['file']['type']=="application/pdf")){
    $flag = $flag+1;
    echo 'Correct Filetype confirmed<br>';
    }

This way you know which ones are passing the checks.

Good luck mate!

I'm not seeing any problems with what you have here. Your issue is probably somewhere else in your code. I would suggest showing the rest of the code. It would be a good idea like thuoc.biz suggested, put in some error checking.

Just a suggested or an example on the way I like to write this

$allowed_ext = array('application/pdf', 'application/doc', 'application/docx', 'application/ppt','application/pptx');
$allowed_size = 20000000;

if (in_array($_FILES['file']['type'], $allowed_ext)) // check if file type allowed
{
    if(filesize($_FILES['file']['tmp_name']) <= $allowed_size) // check the size of the file
    {
        // continue with upload
    }
    else
    {
        // file is too large
    }
}
else
{
    // bad file type
}

I like it better this way so I can easily add new file types to my array, or if I decide to store my allowed file types in a database.

Thanks for the reply,

This is the code I got.

    <!--   This is form for inserting new record. -->


    <form id="FormName" action="checkdoc.php" method="post" name="FormName" enctype="multipart/form-data">
    <table width="448" border="0" cellspacing="2" cellpadding="0">

    <!--   This is the field for the record title. -->
    <tr><td width = "150"><div align="right"><label for="name">Title</label></div></td>
    <td><input id="name" name="title" type="text" size="25" value="" maxlength="50"></td></tr>


    <!--   This is the field for the record author. -->
    <tr><td width = "150"><div align="right"><label for="name">Author</label></div></td>
    <td><input id="author" name="author" type="text" size="25" value="<?php echo $_SESSION['user']; ?>" maxlength="50"></td></tr>

    <!--   This is the field for the record Categroy. -->
    <tr><td width = "150"><div align="right"><label for="risk">Categroy</label></div></td>
    <td><select id="Categroy" name="Categroy" size="1">
    <option value="Type A">Type A</option>
    <option value="Type B">Type B</option>
    <option value="Type C">Type C</option>
    <option value="Type D">Type D</option>
    </select></td></tr>

    <!--   This is the field for the record Sub Categroy. -->
    <tr><td width = "150"><div align="right"><label for="risk">Sub Categroy</label></div></td>
    <td><select id="Sub Categroy" name="Sub_Categroy" size="1">
    <option value="Small">Small</option>
    <option value="Medium">Medium</option>
    <option value="Large">Large</option>
    </select></td></tr>


    <!--   This is the input box for inserting a document. -->
    <tr><td width = "150"><div align="right"><label for="document">Document:</label></div></td>
    <td><input type="file" name="file"/ id="document"></td></tr>

    <!--   This is the field for the document dir. -->
    <tr><td width = "150"><div align="right"><label for="name">Document directory</label></div></td>
    <td><input id="docdir" name="docdir" type="text" size="25" value="" maxlength="50"></td></tr>

    <!--   This is the field for the record information. -->
    <tr><td width = "150"><div align="right"><label for="info">Description</label></div></td>
    <td><textarea id="description" name="description" rows="5" cols="40"></textarea></td></tr>

    <!--   This is the field for the record version. -->
    <tr><td width = "150"><div align="right"><label for="name">Document Version</label></div></td>
    <td><input id="version" name="version" type="text" size="25" value="V" maxlength="50"></td></tr>

    <!--   This is the field for approval for the record. -->
    <tr><td width = "150"><div align="right"><label for="info">Approval </label></div></td>
    <td><select id="approval" name="approval" size="1">
    <option value="pending">Pending</option>
    <option value="approved">Approved</option>

    </select></td></tr>


    <!--   This is the submit button. -->
    <tr><td width="150"></td><td><input type="submit" name="submitButtonName" value="Check"></td></tr>
    </tr><tr>
    <td width="150"></td>
    <td><input type="button" value="Cancel" onClick="history.go(-1);return true;"></td>
    </tr></table></form>

Below is the check

<?php

    //Required input fields
    $title = check_input($_POST['title'], "Please Go back and Enter a Title!");
    $author = check_input($_POST['author'], "Please Go back and Enter an Author!");
    $Categroy = check_input($_POST['Categroy'], "Please Go back and Enter a Categroy!");
    $Sub_Categroy = check_input($_POST['Sub_Categroy'], "Please Go back and Enter a Sub-Categroy!");
    $docdir = check_input($_POST['docdir'], "Please Go back and Enter the Document directory!");
    $description = check_input($_POST['description'], "Please Go back and Enter the Description!");
    $version = check_input($_POST['version'], "Please Go back and Enter document version!");

    //Take away unwanted characters, strips quotes, and add HTMLSpecialCharacters
    function check_input($data, $problem='')
    {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      if ($problem && strlen($data) == 0)
      {
         die($problem);
      }
      return $data;
    }

//approval have a default value so it don't need to check for input.
$approval = trim($_POST["approval"]);


//retrieving document's infomation
$docname = trim($_FILES["file"]["name"]);
$type2 = trim($_FILES["file"]["type"]);
$type = str_replace(array("application/"), "", $type2); //took away "application/"
$Rsize = trim($_FILES["file"]["size"]);
$size = round($Rsize / 1024,3);


if($_SERVER['REQUEST_METHOD'] == 'POST')
{

// all the superglobals to store file information and to verify a valid upload.
if (($_FILES["file"]["type"] == "application/pdf") 
|| ($_FILES["file"]["type"] == "application/doc") 
|| ($_FILES["file"]["type"] == "application/docx")
|| ($_FILES["file"]["type"] == "application/ppt")
|| ($_FILES["file"]["type"] == "application/pptx") 
&& ($_FILES["file"]["size"] < 20000000))
  {

// check for any error
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
// display the file information
    echo "<b>Title: </b>" . $title . "<br />";
    echo "<b>Document's name: </b>" . $docname . "<br />";
    echo "<b>Document's type: </b>" . $type . "<br />";
    echo "<b>Size: </b>" . $size . " Kb<br />"; //Round the decimal number to 3 numbers, e.g 0.00178 will be show as 0.002


// check for already existing files

if (file_exists("tmp/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
// saving the uploaded file temp to the "temp" folder

     move_uploaded_file($_FILES["file"]["tmp_name"],
     "tmp/" . $_FILES["file"]["name"]);
     //echo "Stored in: " . "tmp/" . $_FILES["file"]["name"];
     $file = "tmp/" .$_FILES["file"]["name"];
     $md5 = md5_file($file);
     $_SESSION['tempfile'] = $file;

      }
    }
  }
else

  {
  echo "file is incompatible, Plase try another document ";
  }
}

else
{
echo "Choose document first";
}



?>

I am going to try yours suggestion later on.

Thanks

Are you having issue in uploading any file??
If yes,then check the php.ini configuration file.Check if

file_uploads = On

This allows HTTP file upload.
Also check what is the maximum allowed size for upload.Increase it if you want to increase size of upload of each file.

upload_max_filesize = 2M

Thats not the case since the document I am trying to upload is less than 1MB...

The .pptx is 537kb and .docx is 87kb...

The thing is it seem to be it stopped reading adter pdf...as pdf is working fine...

your mime type is wrong
these are correct mime type for doc,docx and pptx

doc: application/msword
docx : "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation"

Check this for complete list http://technet.microsoft.com/en-us/library/ee309278%28office.12%29.aspx

@yy886,

This is part of an upload script i put together and using well:

$mime = array('image/gif', 'image/jpeg', 'image/jpg', 'image/png');
    if (in_array($_FILES['file']['type'], $mime) == FALSE) {
        echo "You are attempting to upload a \"{$_FILES['file']['type']}\" file type. <br/>
            This is not permitted.<br/>";
    } else {

$mime is an array that holds the accepted file types, the $_FILE[]['type'] is then checked to see if the uploaded mime type is allowed. If it is not, it is blocked.

Very similar to pixelsoul's example.

Have you got your mime types correct?

Have you checked your error log?

I would suggest adding a try{} and catch{}, as you may have an exception, or low value warning that is not showing up due to your settings

EDIT pipped by IIM :):)

@IIM
Yep got it working now! thanks guys!

I didnt know that there have a mime type untill now...

@Squidge
I will try to use your code as your one look a lot cleaner!

Thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.