[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>
<input type="text" id="titulo" name="titulo" size="40" value="<?php echo set_value('titulo'); ?>">
<font size="1"><br>
<?php echo "<font color='red'>".strip_tags(utf8_encode(form_error('titulo')))."</font>"; ?></font></label>
</fieldset>
<fieldset>
<legend>Descrição</legend>
<label>
<textarea id="descricao" name="descricao" cols="50" rows="10"><?php echo set_value('descricao'); ?></textarea>
<font size="1"><br>
<?php echo "<font color='red'>".strip_tags(utf8_encode(form_error('descricao')))."</font>"; ?></font></label>
</fieldset>
<fieldset>
<legend>Arquivo</legend>
<label>
<input type="file" id="arquivo" name="arquivo" label="Arquivo" size="40"/>
<br>
<font size="1">
<?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>"; ?>
</font></label>
</fieldset>
<fieldset>
<center>
<input type="submit" id="submit" name="submit" value="Enviar">
</center>
</fieldset>
</form>
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.