CodeIgniter Forums

Full Version: Problems with Forms input files....
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Paulodemoc[/eluser]
Hello all.... i program with codeigniter for some time now. I've already worked with files uploads, including image redimensioning and cropping. But now Im having some problems...

I made a view with a form with 3 fields: text for a title, textarea for description and file for uploading a pdf...

i've used this same for for another controller, and it worked just fine, i've just changed the field names and ids....

when i input some title and description, and select any file type, it works just fine, but the upload fails because the filetypes are not supported (it is suposed to fail, 'cause I want to upload PDFs only....)

But that's the problem... when i select a pdf (and it is suposed to work), the form_validation->run() was failing... when i checked why, i found this: everytime i select a pdf on the file input and click submit, the $_POST and $_FILES globas are going empty..... i don't know why... and wasn't able to find help... would anybody here know the reason?

bellow is the code from my controller and from my view...:

view:
Code:
<?php echo form_open_multipart("pdfs/pdf/$mode",array("id"  =>  "formInsertPdf")); ?>
  <fieldset>
    <legend>Título</legend>
    <label>
      &lt;input type="text" id="titulo" name="titulo" size="40" value="&lt;?php echo set_value('titulo'); ?&gt;"&gt;
      <font size="1"><br>
      &lt;?php echo "<font color='red'>".strip_tags(utf8_encode(form_error('titulo')))."</font>"; ?&gt;</font></label>
  </fieldset>
  <fieldset>
    <legend>Descrição</legend>
    <label>
      &lt;textarea id="descricao" name="descricao" cols="50" rows="10"&gt;&lt;?php echo set_value('descricao'); ?&gt;&lt;/textarea&gt;
      <font size="1"><br>
      &lt;?php echo "<font color='red'>".strip_tags(utf8_encode(form_error('descricao')))."</font>"; ?&gt;</font></label>
  </fieldset>
  <fieldset>
    <legend>Arquivo</legend>
    <label>
      &lt;input type="file" id="arquivo" name="arquivo" label="Arquivo" size="40"/&gt;
      <br>
      <font size="1">
      &lt;?php if(isset($file_error)) echo "<font color='red'>".strip_tags($file_error)."</font>"; else echo "<font color='red'>".strip_tags(form_error('arquivo'))."</font>"; ?&gt;
      </font></label>
  </fieldset>
  <fieldset>
    <center>
      &lt;input type="submit" id="submit" name="submit" value="Enviar"&gt;
    </center>
  </fieldset>
  &lt;/form&gt;

and the controller
...
Code:
function pdf($mode, $id = '') {
$data['mode']        =        $mode;
$data['pdf']        =        $this->Pdf_model->getPdf($id);

//I DID THIS TO CHECK IF THE POST AND FILES WAS GETTING ANY VALUES
//print_r($_FILES); echo '<br/>'; print_r($_POST); die;

if ($this->form_validation->run() == FALSE) {
  $this->load->view('admin/pdf', $data);
}
else {
  if($mode == 'insert')
  {
   $pdf['titulo']        =        $_POST['titulo'];
   $pdf['descricao']    =        $_POST['descricao'];
   $this->folder                 =       'uploads/pdfs/';
   if (!is_dir($this->folder)) mkdir($this->folder,0777);
   $pdf['status']        =        true;
   $file_pdf            =        $this->_uploadFile('arquivo','pdf');
   if (!is_array($file_pdf)) {
    $data['file_error']       =           $file_pdf;
    $data['msg']              =           'Ocorreu um erro ao enviar o PDF!';
    $this->load->view('admin/pdf',$data);
   }
   else {
    $pdf['arquivo']             =          $file_pdf['file'];
    $pdf['arquivo_url']         =          $file_pdf['fileURL'];
    $pdf['index']        =        $this->Pdf_model->getPdfsQt() + 1;
    if($this->Pdf_model->insertPdf($pdf))
    {
    $data['msg']              =           'PDF adicionado com sucesso!';
    redirect('pdfs/listar', 'refresh');
    }
   else {
    $data['msg']              =           'Ocorreu um erro ao enviar o PDF!';
    $this->load->view('admin/pdf',$data);
    }
  }
}
elseif($mode == 'update')
{
$pdf                        =        $this->Pdf_model->getPdf($id);
$newPdf['idPdf']            =        $id;
$newPdf['titulo']            =        $_POST['titulo'];
$newPdf['descricao']        =        $_POST['descricao'];
$newPdf['status']            =        true;
if($_FILES['arquivo']['name'] != '')
{
$this->folder             =       'uploads/pdfs/';
if (!is_dir($this->folder)) mkdir($this->folder,0777);
$file_pdf                =        $this->_uploadFile('arquivo','pdf');
if (!is_array($file_pdf)) {
$data['file_error']           =           $file_pdf;
$data['msg']              =           'Ocorreu um erro ao atualizar o PDF!';
$this->load->view('admin/pdf', $data);    
}
else {
$newPdf['arquivo']          =          $file_pdf['file'];
$newPdf['arquivo_url']      =          $file_pdf['fileURL'];
unlink('uploads/pdfs/'.$pdf->arquivo);
}
}
else
{
$newPdf['arquivo']              =          $pdf->arquivo;
$newPdf['arquivo_url']          =          $pdf->arquivo_url;
$newPdf['index']                    =        $pdf->index;
if($this->Pdf_model->updatePdf($newPdf))
{
$data['msg']              =           'PDF atualizado com sucesso!';
redirect('pdfs/listar', 'refresh');
}
else
{
$data['msg']              =           'Ocorreu um erro ao atualizar o PDF!';
$this->load->view('admin/pdf', $data);                    
}
}
}
}
...


I would appreciate any help...

Thanks in advance.

El Forum

[eluser]TheFuzzy0ne[/eluser]
First of all, I'd re-assess you're logic if I were you. I'm not even sure that having if-else-elseif is even valid PHP syntax, so I think that this might have something to do with the problem.

El Forum

[eluser]Paulodemoc[/eluser]
If you look closer, you'll notice that there isn't any if-else-elseif...

And i've downloaded another pdf file from the internet and it worked... i just can't seen to find out why it isn't working with the pdfs i have here...

(It ain't mime problem, because the problem occurs even before the upload function...)

El Forum

[eluser]Dam1an[/eluser]
I gotta agree with Fuzz on this, just looking at that code makes my head hurt Sad
Also, not sure if its just the code you pasted but you're missing a closing brace somewhere
And even for the one line ifs, I would still use braces as it improves readability so you can figure out wtf is going on

Edit: And the structure of the if elses is
Code:
if
else
    if
        if
        if
        else
            if
            else
    else if
        if
            if
            if
            else
        else
            if
            else

I added indentation to make it easier to follow

El Forum

[eluser]Paulodemoc[/eluser]
The code is confusing because of the topic editor... i was having problems putting spaces... but i'll do that now.....

El Forum

[eluser]Dam1an[/eluser]
[quote author="Paulodemoc" date="1243363349"]The code is confusing because of the topic editor... i was having problems putting spaces... but i'll do that now.....[/quote]

I c&p code snippets all the time and it always appears fine :-S

El Forum

[eluser]Paulodemoc[/eluser]
I've got problems because i ran out of characteres on the topic... that's why i had to remove the spaces....
Anyway... this is starting to loose its focus... ¬.¬ If you don't know, then please don't even bother posting, please....

El Forum

[eluser]TheFuzzy0ne[/eluser]
How about we do it the other way around? If you can't post legible code, then don't bother posting at all...

If you run out of characters, you can either attach the code to the post, or spread it out over two posts.