Jump to content

Uploading PDF, DOC and image files


joshgarrod

Recommended Posts

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]

Link to comment
Share on other sites

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") { 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.