• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem uploading PDF Files

#1
[eluser]sherpa[/eluser]
Hi Everybody. I want to share my problem here. My problem is that i can't upload PDF files. I have created a function which meants to upload as many PDF files as i want passing file arrays and filepath arrays to the function as parameters. Everytime I try, it says The filetype you are attempting to upload is not allowed. However, i can upload JPG and GIF files with the same code changing extension without any errors. I don't know whats wrong with my following code. Hope you guys help me..;


Code:
class Post extends Controller
{
    function Post()
    {
        parent::Controller();
    }


    function product()
    {
        if(isset($_FILES['pdf']) AND strlen($_FILES['pdf']['name'])>0)
        {
            $pdf_path    = array('./pixs/products/pdf');
            $pdf_files    = array('pdf'); // FILE INPUT NAME
            $PDFUpload    = $this -> UploadPDF($pdf_path, $pdf_files)
        }
        
    }    

    function UploadPDF($upload_path=array(),$files=array())
    {
        $config['allowed_types']= 'pdf';
        $pdfPath[]        = array();
        $data[]            = array();
                        
        $count_files         = count($files);
                        
        for($i = 0; $i < $count_files; $i++)
        {
            $config['upload_path'] = $upload_path[$i];
            $this -> load -> library('upload',$config);

            if(isset($files[$i]) || is_array($files[$i]))
            {
                if(!$this -> upload -> do_upload($files[$i]))
                {
                    echo $this->upload->display_errors('<p>', '</p>');
                }
                else
                {
                        $data[$i]     = array('upload_data' => $this->upload->data());
                    $pdfPath[$i]     = $data[$i]['upload_data']['full_path'];
                                            
                    return $pdfPath;
                }
            }
        }
        return FALSE;
    }
}

#2
[eluser]xwero[/eluser]
you should check what kind of file type that gets send along with the data
Code:
echo $_FILES['userfile']['type']
If the filetype is not in the pdf array of the mimes.php file in the config directory just add it and you are good to go.

#3
[eluser]sherpa[/eluser]
It returns application/pdf and its already in mimes.php.
array('application/pdf', 'application/x-download')

But it is still not working.

#4
[eluser]xwero[/eluser]
try this
Code:
function UploadPDF($upload_path=array(),$files=array())
    {
        $this -> load -> library('upload'); // moved outside loop
        $config['allowed_types']= 'pdf';
        $pdfPath[]        = array();
        $data[]            = array();
                        
        $count_files         = count($files);
                        
        for($i = 0; $i < $count_files; $i++)
        {
            $config['upload_path'] = $upload_path[$i];
            $this->upload->initialize($config); // added

            if(isset($files[$i]) || is_array($files[$i]))
            {
                if(!$this -> upload -> do_upload($files[$i]))
                {
                    echo $this->upload->display_errors('<p>', '</p>');
                }
                else
                {
                        $data[$i]     = array('upload_data' => $this->upload->data());
                    $pdfPath[$i]     = $data[$i]['upload_data']['full_path'];
                                            
                    return $pdfPath;
                }
            }
        }
        return FALSE;
    }

#5
[eluser]sherpa[/eluser]
Thanks a lot xwero
It works perfectly now.

Thanks a lot again.

By the way wot was the problem XWERO??

#6
[eluser]Muppit[/eluser]
I too had this problem - took me ages to find this post and when I did I followed the advice to check the file type being passed.

Turns out it looked like this: 'aplication/unknown' - why I have no idea but explains why the uploader was spitting the dummy.

Anyway, because it's 4:30pm Friday and because I'm dying for an ice-cold Steinlager I fixed the issue by amending this line:

Code:
'pdf'    =>    array('application/pdf', 'application/x-download'),
to:
Code:
'pdf'    =>    array('application/pdf', 'application/x-download', 'application/unknown'),

In the file located here: system/application/config/mimes.php

I realise this is bad programming practices but like I said, it will band-aid that irritating error message until I can figure out why my PDF's are so crappy.

Cheers,

Muppit

#7
[eluser]lay-z-cow[/eluser]
Hi,

is there any better solution than the one of Muppit?

I have the same problem. My upload works perfect, but it won't let me upload pdf-files:

"The filetype you are attempting to upload is not allowed."

Even though it is.

Same matter with docx and other office2007 files.

Any ideas?

Greets,

Chris

#8
[eluser]Unknown[/eluser]
I had the same problem in FIREFOX 3.0 but not in IE 7.0

so my quick fix, since I was only uploading pdfs was to force
Code:
$_FILES['programa']['type']='application/pdf';


with that i had it working with both. BTW when I inspected

Code:
echo $_FILES['programa']['type']

it returned forcedownload or something like that

#9
[eluser]Unknown[/eluser]
Hi, I had the same when I was trying to upload PDF files from FireFox 2.0, I've fixed changing the 'pdf' array in config/mimes.php to:
'pdf' => array('application/pdf', 'application/x-download', 'application/force-download'),

Perhaps there's variations in each browser, I tried in IE7, Safari 3 and FF2 (which was the only one who got the 'application/force-download' mime type).

--Erik

#10
[eluser]Unknown[/eluser]
Hello everyone, I have been getting this message and reading what you all have done to fix it. The problem is that I am a true layman, as Sargent Schultz (Hogans Heroes TV show)would say "I know NOTHING!" however I am a great robot- I can follow exact directions if one of you can please tell me what to do step by step. I would be most grateful, Thanks


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.