Welcome Guest, Not a member yet? Register   Sign In
Form ignoring File Input / Upload fields [RESOLVED]
#1

[eluser]mindSmile[/eluser]
Hi everyone. I'm making an edit form that includes both text information and 2 file uploads, the first is a zip, the second is an image. So far I have the text updating fine, but the file uploading isn't working. First, I accidentally had a regular form_open instead of form_open_multipart, and when it was form_open, I could see the name of the file selected by logging it with firephp, but it was giving the error "You did not select a file to upload". When I saw that I needed to change it to form_open_multipart, the form started ignoring any file input areas altogether, and I could no longer see the name of the file selected. Using $this->output->enable_profiler(TRUE); I see that it is gathering the other POST data from the text fields, but I'm not even getting the "You did not select a file" error anymore, and none of the files are being uploaded.

I've checked file permissions on the source and the destination. It's still working perfectly in my "add" form where a new item is created instead of being updated. I don't need anything about the uploaded file to be added to my database, I just need to use my script to move the file to where it needs to be once the darn thing will upload. I haven't been trying to upload a new image file yet, so you won't see a do_upload() for that field at this point.

Here's the code (doing my best to only include the relevant parts)
View
Code:
<?php
    $hidden = array(
        'id' => $book->id,
        'title_slug' => $book->title_slug
        );
    $form_dest = 'admin/update_book/';
    echo form_open_multipart($form_dest, '', $hidden);
    ?>
    
    <label for="order_title">Title - Shortened: </label>
    &lt;?php
        $data = array(
            'name' => 'order_title',
            'id' =>'order_title',
            'value' => $book->order_title,
            'maxlength' => '255',
            'size' => '50');
        echo form_input($data);
    ?&gt;
    
    <label for="author">Author: </label>
    &lt;?php
        $data = array(
            'name' => 'author',
            'id' =>'author',
            'value' => $book->author,
            'maxlength' => '255',
            'size' => '50'
            );
        echo form_input($data);
    ?&gt;
    
    <label for="zipped_book">Zipped Book File:</label>
    &lt;?php
        $data = array(
            'name' => 'zipped_book',
            'id' =>'zipped_book'
            );
        echo form_upload($data);
    ?&gt;
    
    <label for="cover_path">Cover Art:</label>
    &lt;?php
        $data = array(
            'name' => 'cover',
            'id' => 'coverart',                        
            );
        echo form_upload($data);
    ?&gt;
    
    <label for="level">Is this file ready to be published on the site?</label>
    &lt;?php
        $options = array(
            '0' => 'Uploaded Only',
            '1' => 'Tested, Awaiting Approval',
            '2' => 'Approved and Published'
            );
        echo form_dropdown('level',$options,'0');
    ?&gt;
    
    &lt;?php echo form_submit('submit','Update this Book','id="submit" class="blue_button"'); ?&gt;

Controller
Code:
class Admin extends Controller{
function update_book(){
$this->load->model('Admin_model');
$data = $this->Admin_model->update_book();}
}

Model
[code]
class Admin_model extends Model{
function update_book(){
$id = $this->input->post('id');
$book = $this->Books_model->getBookByID($id);
$language = $this->Books_model->langCodeToLang($book->lang);
$slug = $book->title_slug;
$language = strtolower($language);
$file_path = realpath(APPPATH . '../assets/books/'.$language.'/'.$slug .'/');
$coverpath = $file_path . 'cover.jpg';


$this->load->library('upload');
$this->load->helper('file');

$this->output->enable_profiler(TRUE);
$new_file = $this->input->post('zipped_book');
if($new_file != ''){
//Delete the Current Files
if(is_dir($file_path)){
delete_files($file_path,TRUE);
rmdir($file_path);
}
//Upload the Zipped Folder
//Configure the Zipped Book Upload
$zipped_book = array(
'upload_path' => realpath(APPPATH . '../assets/books/'.$language.'/'),
'allowed_types' => 'zip',
);

//Do the Upload
$this->upload->initialize($zipped_book);
$field_name = 'zipped_book';
if($this->upload->do_upload($field_name)){
$book_data = $this->upload->data();
$upload_path = $book_data['full_path'];
}else {
$error = $this->upload->display_errors();
$this->firephp->warn($error); }
}

$new_cover = $this->input->post('cover');
if($new_cover != ''){
$this->firephp->log('A new cover is being uploaded.');
}
}
#2

[eluser]mindSmile[/eluser]
*crickets* anyone? anyone at all?
#3

[eluser]mindSmile[/eluser]
Here's the profiler output:

Quote: URI STRING
/admin/update_book
CLASS/METHOD
admin/update_book
MEMORY USAGE
3,168,668 bytes
BENCHMARKS
Loading Time Base Classes 0.2904
Controller Execution Time ( Admin / Update Book ) 0.7083
Total Execution Time 0.9989
GET DATA
No GET data exists
POST DATA
$_POST['id'] 52
$_POST['title_slug'] three_muskateers
$_POST['inc_charset']
$_POST['title_eng']
$_POST['title_nat']
$_POST['order_title']
$_POST['author']
$_POST['level'] 0
$_POST['submit'] Update this Book
DATABASE: polyglot_project QUERIES: 3
0.0005 SELECT *
FROM (`ci_sessions`)
WHERE `session_id` = 'a3d3a05c57c17a6030fc23c72948adac'
AND `user_agent` = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en'
0.0004 UPDATE `ci_sessions` SET `last_activity` = 1282512849, `session_id` = 'c8e05fc753111caee43b1b698ceb9ec1' WHERE session_id = 'a3d3a05c57c17a6030fc23c72948adac'
0.0003 SELECT *
FROM (`books`)
WHERE `id` = '52'
#4

[eluser]guidorossi[/eluser]
I think you should be accessing file information via $this->upload->data() or via $_FILE, not with this->input->post()
#5

[eluser]mindSmile[/eluser]
I was using $this->input->post('zipped_book') to check if the user was trying to update that field or leaving it blank, so that if they aren't trying to change it the script wouldn't go ahead and delete what the new stuff would be replacing and try to do the upload. Is there another way to test if the user has input something in that field, or should I just tell it to do the upload and then test if the upload worked to keep the script from doing things unnecessarily?
#6

[eluser]guidorossi[/eluser]
Well, you can do this:
if($_FILES['fieldname']['name'] != ""){
...
}
#7

[eluser]mindSmile[/eluser]
Great! Using that instead of $this->input->post seems to have fixed the problem! Thank you so much!




Theme © iAndrew 2016 - Forum software by © MyBB