Welcome Guest, Not a member yet? Register   Sign In
Trying to Upload Multiple Photos with Thumbnail Creation
#1

[eluser]RMinor[/eluser]
I am getting a strange error when i try to upload multiple photos and create thumbnails of them. The error is "The action you have requested is not allowed.".

Here is my controller:
Code:
public function index()
{
    // Retrieve page information
    $data['page_info'] = $this->Global_model->pageInfo($this->_page);
    // Retrieve number of photo fields we need
    $data['photo_fields'] = $this->_photos;
    // Load the form helper
    $this->load->helper('form');
    // Check if the submit button was pressed
    if ($this->input->post('upload')) {
        // Load the form validation library
        $this->load->library('form_validation');
        // Set form validation rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required|is_unique[person.person_name]');
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|is_unique[person.person_email]');
        $this->form_validation->set_rules('phone', 'Phone Number', 'trim|required');
        $this->form_validation->set_rules('age', 'Age Check', 'trim|required');
        // If form validation fails
        if ($this->form_validation->run() == FALSE) {
  
        // If form validation is successful
        } else {
            // Insert person information into the database
            $name = $this->input->post('name');
            $email = $this->input->post('email');
            $phone = $this->input->post('phone');
            $age_check = $this->input->post('age');
            $event_id = $this->_event_id;
            $ip = $_SERVER['REMOTE_ADDR'];
            if ($this->Person_model->initialSubmit($name, $email, $phone, $age_check, $event_id, $ip)) {
                // Load the upload library
                $this->load->library('upload');
                // Load the image library
                $this->load->library('image_lib');
                // Configure the upload library
                $config['upload_path'] = './photos/';
                $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['max_size'] = '5120'; // 5MB
                // Loop through each photo uploaded
                foreach ($_FILES as $key => $value) {
                    // If the photo field is not empty
                    if (!empty($key['name'])) {
                        // Initialize the upload library
                        $this->upload->initialize($config);
                        // If upload fails
                        if (!$this->upload->do_upload($key)) {
                            // Retrieve the errors
                            $data['upload_errors'] = array('error' => $this->upload->display_errors());
                        // If the upload was successful
                        } else {
                            // Assign uploaded photo information to a variable
                            $photo_info = $this->upload->data();
                            // Configure the image library
                            $config_thumb['create_thumb'] = TRUE;
                            $config_thumb['maintain_ratio'] = TRUE;
                            $config_thumb['width'] = 150;
                            $config_thumb['height'] = 150;
                            $config_thumb['source_image'] = $photo_info['full_path'];
                            // Assign the photo filename
                            $photo = $photo_info['file_name'];
                            // Initialize the image library
                            $this->image_lib->initialize($config_thumb);
                            // If resize was successful
                            if ($this->image_lib->resize()) {
                                // Assign the thumbnail filename
                                $thumbnail = $photo_info['raw_name'] . '_thumb' . $photo_info['file_ext'];
                                // Insert photo information into the database
                                if ($this->Person_model->insertPhoto($photo, $thumbnail)) {
                                    $data['success'] = TRUE;
                                }
                                // Clear current data from the image library
                                $this->image_lib->clear();
                            }
                        }
                    }
                }
            }
        }
    }
    // Load the required view and data
    $this->load->view('submit_view', $data);
}


Here is my view (well, the form anyway):
Code:
<form method="post" action="" name="contactform" id="contactform" class="contact-form" />
    <div>
        <label for="name" accesskey="U">Full Name</label>
        &lt;input name="name" type="text" id="name" size="30" value="" /&gt;
    </div>
    <div>
        <label for="email" accesskey="E">Email Address</label>
        &lt;input name="email" type="text" id="email" size="30" value="" /&gt;
    </div>
    <div>
        <label for="phone" accesskey="P">Phone Number</label>
        &lt;input name="phone" type="text" id="subject" value="" /&gt;
    </div>
    &lt;?php for ($i = 0; $i < $photo_fields; $i++) { ?&gt;
        <div>
            <label for="photo[]">Photo</label>
            &lt;input name="photo[]" type="file" id="photo[]" value="" /&gt;
        </div><br />
    &lt;?php } ?&gt;
    <div>
        <label for="age">I verify that I am at least 18 years of age</label>
        &lt;input name="age" type="radio" id="age" value="Yes" /&gt; Yes&nbsp;
        &lt;input name="age" type="radio" id="age" value="No"&gt; No
    </div><br />
    &lt;input type="submit" class="form-submit" id="submit" value="Submit" /&gt;
    &lt;/form&gt;
#2

[eluser]rip_pit[/eluser]
Quote:The action you have requested is not allowed

I notice that this message is often generated by the csrf protection when enabled.
So you can't submit the same form twice.

I didn't read all the code but try first to disable csrf
(disable globaly from the config/config.php or if you have it, add your form url in the csrf exlusion list in the same cfg file to disable only for this form. -this option may not be available in all codeigniter versions-)
#3

[eluser]RMinor[/eluser]
I just checked that as I got your reply. I was able to fix that issue. Now, let's see if I can get this upload to work. I noticed that my form had the wrong enctype. I made that correction also. Now when I print_r my $_FILES array I get proper data. However, I am not sure if I am going about reading the fields into the upload library properly. Am I writing them properly in the form?
#4

[eluser]RMinor[/eluser]
Hi everyone. I am happy to report that I have come up with a solution to accomplish what I want. I will post it below for anyone who ever needs to upload multiple photos and create thumbnails of them. Enjoy!

Controller:
Code:
public function index()
{
    // Retrieve page information
    $data['page_info'] = $this->Global_model->pageInfo($this->_page);
    // Retrieve number of photo fields we need
    $data['photo_fields'] = $this->_photos;
    // Load the form helper
    $this->load->helper('form');
    // Check if the submit button was pressed
    if ($this->input->post('upload')) {
        // Load the form validation library
        $this->load->library('form_validation');
        // Set form validation rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required|is_unique[person.person_name]');
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|is_unique[person.person_email]');
        $this->form_validation->set_rules('phone', 'Phone Number', 'trim|required|is_unique[person.person_phone]');
        $this->form_validation->set_rules('age', 'Age Check', 'trim|required');
        // If form validation fails
        if ($this->form_validation->run() == FALSE) {

        // If form validation is successful
        } else {
            // Insert person information into the database
            $name = $this->input->post('name');
            $email = $this->input->post('email');
            $phone = $this->input->post('phone');
            $age_check = $this->input->post('age');
            $event_id = $this->_event_id;
            $ip = $_SERVER['REMOTE_ADDR'];
            if ($person_id = $this->Person_model->initialSubmit($name, $email, $phone, $age_check, $event_id, $ip)) {
                // Load the upload library
                $this->load->library('upload');
                // Configure the upload library
                $config['upload_path'] = './photos/';
                $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['max_size'] = '5120'; // 5MB
                $config['max_width'] = '720';
                $config['max_height'] = '720';
                // Retrieve number of items in the files array
                $photos = count($_FILES);
                // Loop through each photo uploaded
                for ($i = 0; $i < $photos; $i++) {
                    // Initialize the upload library
                    $this->upload->initialize($config);
                    // If upload fails
                    if (!$this->upload->do_upload('photo' . $i)) {
                        // Retrieve the errors
                        $data['upload_errors'] = array('error' => $this->upload->display_errors());
                    // If the upload was successful
                    } else {
                        // Load the image library
                        $this->load->library('image_lib');
                        // Assign uploaded photo information to a variable
                        $photo_info = $this->upload->data();
                        // Configure the image library
                        $config_thumb['create_thumb'] = TRUE;
                        $config_thumb['maintain_ratio'] = TRUE;
                        $config_thumb['width'] = 100;
                        $config_thumb['height'] = 100;
                        $config_thumb['source_image'] = $photo_info['full_path'];
                        // Assign the photo filename
                        $photo = $photo_info['file_name'];
                        // Initialize the image library
                        $this->image_lib->initialize($config_thumb);
                        // If resize was successful
                        if ($this->image_lib->resize()) {
                            // Assign the thumbnail filename
                            $thumbnail = $photo_info['raw_name'] . '_thumb' . $photo_info['file_ext'];
                            // Insert photo information into the database
                            if ($this->Person_model->insertPhoto($photo, $thumbnail, $person_id)) {
                                $data['success'] = TRUE;
                            }
                            // Clear current data from the image library
                            $this->image_lib->clear();
                        }
                    }
                }
            }
        }
    }
    // Load the required view and data
    $this->load->view('submit_view', $data);
}

View (form only):
Code:
&lt;form method="post" action="" name="contactform" id="contactform" class="contact-form" enctype="multipart/form-data" /&gt;
    <div>
        <label for="name">Full Name</label>
        &lt;input name="name" type="text" id="name" size="30" value="" /&gt;
    </div>
    <div>
        <label for="email">Email Address</label>
        &lt;input name="email" type="text" id="email" size="30" value="" /&gt;
    </div>
    <div>
        <label for="phone">Phone Number</label>
        &lt;input name="phone" type="text" id="subject" value="" /&gt;
    </div>
    &lt;?php for ($i = 0; $i < $photo_fields; $i++) { ?&gt;
        <div>
            <label for="photo&lt;?php echo $i; ?&gt;">Photo</label>
            &lt;input name="photo&lt;?php echo $i; ?&gt;" type="file" id="photo&lt;?php echo $i; ?&gt;" value="" /&gt;
        </div><br />
    &lt;?php } ?&gt;
    <div>
        <label for="age">I verify that I am at least 18 years of age</label>
        &lt;input name="age" type="radio" id="age" value="Yes" /&gt; Yes&nbsp;
        &lt;input name="age" type="radio" id="age" value="No"&gt; No
    </div><br />
    &lt;input type="submit" class="form-submit" name="upload" id="submit" value="Upload" /&gt;
&lt;/form&gt;

Of course you will want to add any success messages that you want to display. If anyone sees anything I could've done better please let me know. Otherwise, I hope this helps someone out!
#5

[eluser]rip_pit[/eluser]
seems fine good work !

for a more ready to use audience you should remove references to
Code:
Person_model
  Global_model

also define somewhere
Code:
$this->_photos

not sure about the best practise here but after Run success i prefer to use
Code:
$fieldname = set_value('fieldname','default');
instead of
Code:
$fieldname = $this->input->post('fieldname');

anyway thanks for sharing Wink
#6

[eluser]RMinor[/eluser]
Thanks for the advice. I think another area of improvement would be to create separate methods for the upload and the thumbnail creation. Once I get done with this project I may make some of these improvements.




Theme © iAndrew 2016 - Forum software by © MyBB