Welcome Guest, Not a member yet? Register   Sign In
Need Help With File Uploading Class Allowed Types from Database
#1

[eluser]mm[/eluser]
Hi,

I have a table that contains all the allowed file types extension. I have successfully queried the table and manipulated the results so that the format will be like
Code:
'doc|pdf|txt'
Unfortunately, when I try to upload any file (with the allowed extension), I received a "The filetype you are attempting to upload is not allowed".

below is my code

Code:
$sql_allowed_types = $this->db->get('file_type');

$i=0;
foreach($sql_allowed_types->result() as $row):
if($i==0):
  $allowed_types = $row->file_ext;
else:
  $allowed_types = $allowed_types.'|'.$row->file_ext;
endif;
$i++;
endforeach;

$config['allowed_types'] = $allowed_types;
  
$config['max_size'] = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';

$this->load->library('upload', $config);

can anyone help me to check if there's anything wrong with my code, please?

Thanks in advance
#2

[eluser]Stefan Hueg[/eluser]
Make sure that you store the allowed filetypes in lowercase in your database.
Is your $allowed_types containing exactly what you've posted (doc|pdf|txt)?

I can't see anything else that could lead to your error. Hmmm...
#3

[eluser]mm[/eluser]
when i echo $allowed_types, the output is exactly in this format
Code:
doc|avi|mp3|jpg|docx|xls|xlsx|pdf|txt

anyway, just wondering, is there a limit to how many file types we can specify? i have just found out that i can successfully upload files with the first three allowed file extensions. in my case, i can upload .doc, .avi or .mp3, but failed if i upload any files from .jpg to .txt

if there's a limit, may i know where to look for it?
#4

[eluser]InsiteFX[/eluser]
Code:
foreach($sql_allowed_types->result() as $row):
if($i==0):
  $allowed_types = $row->file_ext;
else:
  $allowed_types .= '|'.$row->file_ext;
endif;
$i++;
endforeach;

$config['allowed_types'] = '"'.$allowed_types.'"';
#5

[eluser]mm[/eluser]
I've tried this too

Code:
$config['allowed_types'] = '"'.$allowed_types.'"';

same result. only the 1st 3 filetypes can be uploaded, the rest failed..
#6

[eluser]mm[/eluser]
i'm looking at Upload.php in Libraries and noticed that this piece of code actually caused the problem (in my case).

Code:
if (in_array($val, $image_types))
{
echo $this->file_temp;
if (getimagesize($this->file_temp) === FALSE)
{
  return FALSE;
}
}

in my list of allowed file types below

Code:
doc|avi|mp3|jpg|docx|xls|xlsx|pdf|txt

i noticed that after 'jpg', the process stopped and throws the "The filetype you are attempting to upload is not allowed" error. this code is actually in a foreach loop and if I'm uploading files with any other extension after the 'jpg', it failed because somehow it exits the loop on 'jpg'.

I'm sorry i don't really know how to explain this.

anyway, if i change return FALSE to return TRUE, everything works fine.

so, is it ok if i just change it to TRUE?
#7

[eluser]Riente[/eluser]
Hello everyone, I have kind of a similar problem.

I indicate two allowed file types $config['allowed_types'] = 'gif|jpg';
The point is only the first one works fine. I mean than I can upload gif files successfully, and when trying to upload a jpg file I get an error “The filetype you are attempting to upload is not allowed”.
If I write $config['allowed_types'] = 'jpg|gif'; (i.e. swap their positions) then I upload jpg-files just fine, but have problems uploading gif-files.

Here's my code:
Code:
$this->load->library('upload');
$config['upload_path'] = './img/logos/brands/';
$config['allowed_types'] = 'gif|jpg';
$config['max_size'] = '8192';
$config['encrypt_name'] = TRUE;
$config['max_width']  = '0';
$config['max_height']  = '0';
$this->upload->initialize($config);

if (!$this->upload->do_upload("file"))
{
$img = $data['item']['img'];
}
else
{
// ...
}

What can be the problem?
#8

[eluser]mm[/eluser]
Hi Riente,

I think it has something to do with this code in Libraries/Upload.php

Code:
$image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');

foreach ($this->allowed_types as $val)
{
$mime = $this->mimes_types(strtolower($val));

// Images get some additional checks
if (in_array($val, $image_types))
{
  if (getimagesize($this->file_temp) === FALSE)
  {
   return FALSE;
  }
}

i'm not really sure what the code is for but if i change $image_types to

Code:
$image_types = array();

everything works fine...
#9

[eluser]Riente[/eluser]
Well nope, it didn't work for me...
Images pass this check, whether it is jpg or gif. The problem appears somewhere else, and I can't understand why it uses only the first filetype of the row
#10

[eluser]Techno Heart[/eluser]
Controller:

Code:
function ShowUpload()
{
  $this->load->view('upload_view',array('error' => ' ' ));
}

function FileUpload()
{

  $sql_allowed_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');


  $i=0;
  foreach($sql_allowed_types as $row):
  if($i==0):
  $allowed_types = $row;
  else:
  $allowed_types = $allowed_types.'|'.$row;
  endif;
  $i++;
  endforeach;


  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = $allowed_types;

  $config['max_size'] = '100';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';

  $this->load->library('upload', $config);

  if ( ! $this->upload->do_upload())
  {
   $error = array('error' => $this->upload->display_errors());

   $this->load->view('upload_view', $error);
  }
  else
  {
   $data = array('upload_data' => $this->upload->data());

   $this->load->view('upload_success', $data);
  }
}

View:
upload_view:
Code:
<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('login/FileUpload');?>

<input type="file" name="userfile" size="20" />

<br /><br />

&lt;input type="submit" value="upload" /&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
upload_success.php:
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h3>Your file was successfully uploaded!</h3>

<ul>
&lt;?php foreach ($upload_data as $item => $value):?&gt;
<li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

<p>&lt;?php echo anchor('upload', 'Upload Another File!'); ?&gt;</p>

&lt;/body&gt;
&lt;/html&gt;

This code was working fine. Please check properly. dont forget to create upload folder in your root.




Theme © iAndrew 2016 - Forum software by © MyBB