joshgarrod Posted April 17, 2012 Share Posted April 17, 2012 Hi, I have a script that currently works for uploading images and I have tried to modify it to upload PDF's and DOC's too, but I can't get it to work - it works fine with JPG or GIF. Any ideas? Thanks in advance. <?php include "scripts/connect.php"; $idir = "../documents/"; // Path To Images Directory if (isset ($_FILES['fupload'])){ $randomd=rand(0000,9999); //upload the image to tmp directory $url = $_FILES['fupload']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg" || $_FILES['fupload']['type'] == "image/gif" || $_FILES['fupload']['type'] == "image/pdf" || $_FILES['fupload']['type'] == "image/doc") { $file_ext = strrchr($_FILES['fupload']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['fupload']['tmp_name'], "$idir" . "$randomd" . $_FILES['fupload']['name']); // Move Image From Temporary Location To Permanent Location } } error_reporting (E_ALL ^ E_NOTICE); if ($_POST['submit']) { $document = mysql_real_escape_string("$idir" . "$randomd" . $_FILES['fupload']['name']); $name = mysql_real_escape_string($_POST['name']); $description = mysql_real_escape_string($_POST['description']); $SQL = " INSERT INTO documents"; $SQL .= " (document, name, description) VALUES "; $SQL .= " ('$document', '$name', '$description') "; $result = mysql_db_query($db,$SQL,$cid); $last=mysql_insert_id(); if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } header("location:document-added.php?ref=$last"); exit(); } ?>[code] Quote Link to comment Share on other sites More sharing options...
TOA Posted April 17, 2012 Share Posted April 17, 2012 Try changing 'image' to 'application' in the below code || $_FILES['fupload']['type'] == "image/pdf" || $_FILES['fupload']['type'] == "image/doc") { *Edit - the correct mime type for a .doc is application/msword, sorry. So it would be || $_FILES['fupload']['type'] == "application/pdf" || $_FILES['fupload']['type'] == "application/msword") { Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 17, 2012 Share Posted April 17, 2012 And, you should consider changing that condition check so you don't need all the OR conditions. $allowedTypes = array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'application/pdf', 'application/doc'); if (in_array($_FILES['fupload']['type'], $allowedTypes)) { //Process the file } Quote Link to comment Share on other sites More sharing options...
joshgarrod Posted April 17, 2012 Author Share Posted April 17, 2012 Thanks very much for your replies. It has worked for PDF but not for DOC. Quote Link to comment Share on other sites More sharing options...
TOA Posted April 17, 2012 Share Posted April 17, 2012 It has worked for PDF but not for DOC. Make sure you have the correct mime type from my post above. And that is only for .doc; if it's a .docx, you have to use a different mime type Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 18, 2012 Share Posted April 18, 2012 Did you ever think of simply echoing the type value to the page and uploading a .doc file? You would then see what the value was and could implement it in your code. This is very simple stuff that you should be able to do on your own. Don't get caught up in the details that you lose common sense. Although you would want to test with .doc and .docx files. Quote Link to comment Share on other sites More sharing options...
joshgarrod Posted April 18, 2012 Author Share Posted April 18, 2012 Thank you, I appreciate your help. I looked more into mime types which I had not really come accross before having only worked with images in the past. I changed "application/doc" to "application/msword" and it works! Thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.