Jump to content
Larry Ullman's Book Forums

File Upload Ch 11 How2 Cad File Types


Recommended Posts

Chapter 111 script 11.2 works fine for uploading jpegs, pngs etc & when I add 'application/pdf' for pdf files also. But when I add e.g. 'application/DWG' it doesn't work. What is the correct code to upload file types: DXF, IGES, ACIs etc. please?

 

This is my code:

 

$allowed = array ('application/octet-stream', 'application/zip', 'application/pdf', 'application/dxf', 'application/x-autocad', 'application/x-dxf', 'drawing/x-dxf', 'image/vnd.dxf', 'image/x-autocad', 'image/x-dxf', 'zz-application/zz-winassoc-dxf','application/igs','application/sat','image\pjpeg', 'image\jpeg', 'image\JPG', 'image\X-PNG', 'image\PNG', 'image\png', 'image\x-png',);
  if (in_array($_FILES['upload']['type'], $allowed)) {

 

Link to comment
Share on other sites

Thanks for your help

.

The file names are: 1343939490.dwg  or wel4.dxf. I have been able to upload these files with a different script.

 

I have added 'application/DWG' to the array, but it still doesn't work:

 

$allowed = array ('application/pdf', 'application/DXF','application/dxf', 'application/DWG', 'application/dwg','image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png',);
  if (in_array($_FILES['upload']['type'], $allowed)) {

Link to comment
Share on other sites

The possible MIME types for just .dwg include: application/acad, application/x-acad, application/autocad_dwg, image/x-dwg, application/dwg, application/x-dwg, application/x-autocad, image/vnd.dwg, drawing/dwg.

 

Rather than add all of those, I would start by seeing what the browser is reporting as the MIME type of that particular file (and try this on a couple of browsers).

Link to comment
Share on other sites

Thanks. I tried all the options but none worked. I don't think it is a browser problem because I can upload these file types using this code:

 

<?php

$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Upload failed";
}

echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";

?>

 

But it would suit my purposes better to modify script 11.2

 

Link to comment
Share on other sites

use this function to check the mime type: [it's discussed on page 414 in chapter 13, not chapter 11]

$mime_type = finfo_file( finfo_open(FILEINFO_MIME_TYPE), $YOUR_FILE );

I also had some problems with mime types and headers for .ogg files. In the end I couldn't solve it but I will post that

as a separate topic at some stage in the future.

  • Upvote 1
Link to comment
Share on other sites

Also, perhaps you can point out the differences between the code that worked and the code that didn't work. That should help figure out what the problem is.

 

And by the way, there is a comma here at the end of your array.

 

$allowed = array ('application/pdf', 'application/DXF','application/dxf', 'application/DWG', 'application/dwg','image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png',);

  • Upvote 1
Link to comment
Share on other sites

I have not ben able to resolve the issue for uploading .dwg file etc. An alternative solution would be to allow for .zip file uploads, but this is proving problematic also..

 

I have used the upload_rtf.php file in chapter 13 to check if .zip file are expectable and get a success message, but the following code allows for .pdf upload but not .zip file: 

 

 

$allowed = array ('application/zip', 'application/pdf', 'application/octet-stream', 'application/DWG', );
  if (in_array($_FILES['upload']['type'], $allowed)) {

 

Any suggestions gratefully received

Link to comment
Share on other sites

I believe the comma ends up creating an empty value at the end of the array, but that's a technical answer that may not reflect actual problems caused by this syntax in the real world. 

 

The upload_rtf.php script uses the Fileinfo extension to verify a file's type. Are you actually using that or are you just checking the MIME type provided by the browser?

Link to comment
Share on other sites

Thanks. I used the upload_rtf.php as standalone file to test if a file was acceptable and got a positive result, but when I used the script I posted above the file was not uploaded.

I am unsure as to how to check the Mime type via the browser.

I also configured the Mime types in the control panel - it doesn't seem to be a configuration problem.

Link to comment
Share on other sites

The MIME type via the browser (i.e., what the browser identifies as the MIME type) is what you have above:

 

 
if (in_array($_FILES['upload']['type'], $allowed)) {
 
The upload_rtf.php script uses the Fileinfo extension and has a different syntax.
 
I have no idea what you mean about configuring the MIME types in the control panel.
Link to comment
Share on other sites

If you want to allow for multiple file upload types and use Fileinfo, you can use the in_array() technique but instead of using $_FILES['upload']['type'] for the value to match, you'd use finfo_file($fileinfo, $_FILES['upload']['tmp_name']):

if (in_array(finfo_file($fileinfo, $_FILES['upload']['tmp_name']), $allowed)) {...
Link to comment
Share on other sites

You can add file extension types in the control panel of the hosting server so I added for example

Content Type: applicationzip. Bu, as I said this is not the problem. When I used upload_rtf.php, and changed it to:

// Check the file:

if (finfo_file($fileinfo, $_FILES['upload']['tmp_name']) == 'application/zip') {

 

it retuned file is acceptable, but when I used upload_image with 'application/zip' in the $allowed array

it didn't work

Link to comment
Share on other sites

 Share

×
×
  • Create New...