Link to home
Start Free TrialLog in
Avatar of alanpollenz
alanpollenz

asked on

Why is $binFile_type not returning the proper file type?

I have a form and a script that I use to upload pdf files to a mysql database.  There is a line of code that checks to make sure that it is a pdf file being uploaded.  If it is, it allows to upload to continue.  If not, an error message is returned.

The script used to return a file type of application/pdf, which is what I am looking for, but now it is returning a file type of application/force-download, which causes an error by returning $goodtype="N".

The process worked for some time, but is now broken.  I don't know what caused this to happen, but it is a Fedora Core 4 box with php 4.4.2 that was recently upgraded from core 1 and php 4.4.1.

I know I can fix this but doing the extension check with:

      $ext = explode('.', $binFile_name);
      $extension = $ext[count($ext)-1];

but was curious if anyone knew why the original code is no longer functioning.

Basic Form:

################################################

  $date = date("F j, Y");
  print <<<END
Complete the following form to add an item to the archives.<br><br>
<form action="archive.add.do.php" name="archive" id="archive" method="post" enctype="multipart/form-data" onsubmit="return AddArchiveValidator(this)">
 <input type="hidden" name="max_file_size" value="5000000">
 <input type="hidden" name="action" value="upload">
 <table border=0 align="center">
  <tr>
   <td class="bold">Title: </td><td><input type="text" name="title" size="50" maxlength="255"></td>
  </tr>
  <tr>
   <td class="bold">File: </td><td><input type="file" name="binFile"></td>
  </tr>
  <tr>
   <td class="bold">Date:</td><td><input name="a_date" type="text" size="20" value='$date'></td>
  </tr>
  <tr>
   <td class="bold"></td><td><span class="bold">Note: </span>The date is used to determine the order of display (reverse chronological).  You may enter a different date, but please make sure it is in the same format as the pre-populated date.</td>
  </tr>
  <tr>
   <td class="bold">Type:</td><td><input type="radio" name="archive" value="1" checked>Newsletter&nbsp;&nbsp;&nbsp;<input type="radio" name="archive" value="2">Directory</td>
  </tr>
  <tr>
   <td align="center" valign="top" colspan="4"><input type="submit" value="Upload File"></td>
  </tr>
 </table>
</form>
END;

################################################

Add Archive Script:

################################################

  $a_unixtime=strtotime($a_date);
  if (isset($binFile) && $binFile != "none") {
    $data = addslashes(fread(fopen($binFile, "r"), filesize($binFile)));
    $goodtype="N";
    if ($binFile_type == "application/pdf") {
      $ext ="pdf";
      $goodtype = "Y";
      }
    if ($goodtype=="N") {
      $result_msg="The only permitted file types are PDF files.  Please verify the file type you attempted to upload and try again.";
      }
    else {
      $query = "INSERT INTO archives (bin_data, title, filename, filesize, filetype, archive, a_date, a_unixtime) VALUES ('$data', '$title', '$binFile_name', '$binFile_size', '$binFile_type', '$archive', '$a_date', '$a_unixtime')";
      $result = @mysql_query($query);
      $result_msg='<span class="bold">Success!</span> - The new file was successfully added to the archive database.<br><br>';
      }
    }

  print $result_msg;


################################################
ASKER CERTIFIED SOLUTION
Avatar of dr_dedo
dr_dedo
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dr_dedo.

PLEASE stop using short tags. They are NOT the way to call PHP code and have been removed in PHP6.


alanpollenz

I would say that you had register_globals enabled on your old version. Again, this is another bit of dead wood removed in PHP6.

As suggested by dr_dedo, the use of the super global $_FILES is the way to go.

Take a look at http://www.php.net/manual/en/features.file-upload.php
Avatar of alanpollenz
alanpollenz

ASKER

I did as dr_dedo suggested, and when uploading a jpg file, if returned image/jpeg, but when uploading a pdf, it returned application/force-download, rather than /pdf, as I expected.

Any ideas on why this is occuring?

Thanks.

Alan
how was this PDF file created ?? maybe it is related to its creation method, try to create a standard PDF file (by Adope) or get a sample PDF from anywhere, and test it
This particular file was created by me from within MS Word using Adobe Acrobat 6.  I just opened Abode Acrobat 6, and created a new file from web page (www.adobe.com) and tried to upload using <?=file_exists($_FILES['binFile']['tmp_name'])? $_FILES['binFile']['type']:"no file uploaded"?> as the action.  I still got the application/force-download as the file type.
Interestingly, in the database table where these entries are stored, all the previous file uploads have "application/pdf" in the 'filetype' field.  The entries that are displaying application/force-download when using <?=file_exists($_FILES['binFile']['tmp_name'])? $_FILES['binFile']['type']:"no file uploaded"?> just have "pdf" in the 'filetype' field.  Hmmm.
i don't get it! you are having type as application/force-download or as PDF ???

please notice that  $_FILES['binFile']['type'] is the right way to get file types
When I try to upload a file using my regular upload form, and I use your recommended action page of <?=file_exists($_FILES['binFile']['tmp_name'])? $_FILES['binFile']['type']:"no file uploaded"?>, it reports application/force-download, and it also puts that filetype in the database.  Prior to changing the server setup as described in the initial post in this thread, the filetype placed in the database was application/pdf.  I'm not sure what happened to change this.

Thanks.