0

So I'm trying to insert in the database and upload a file. But when I'm trying to submit, it returns a server error.

The localhost page isn’t working

localhost is currently unable to handle this request.

HTTP ERROR 500

I think the problem is in the $this->upload->do_upload('userfile') because i tried to comment out from this part and display echo the config array and there's no error but when i tried to uncomment up to this part, the error shows.

Controller

public function add_now(){
    $this->load->library('form_validation');
    $this->form_validation->set_rules('Event_Name', 'Event Name', 'trim|strip_tags');
    $this->form_validation->set_rules('Event_Start', 'Start Date', 'trim|strip_tags');
    $this->form_validation->set_rules('Event_End', 'End Date', 'trim|strip_tags');
    $this->form_validation->set_rules('Event_Location', 'Location', 'trim|strip_tags');

    if($this->form_validation->run() == FALSE){
        $this->add();
    }
    else{   
        $query = $this->events_model->do_upload();
        if($query){
            $this->session->set_flashdata('success', 'Successful!');
            $this->index();
        }
        else{
            if(!$this->session->flashdata('upload_error')){
                $this->session->set_flashdata('failed', 'Failed!');
            }
            $this->add();
        }
    }
}

Model

public function do_upload(){
    
    $config['upload_path'] = './resources/images/events_photo/temp/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['file_name'] = uniqid().'.jpeg';
    $config['overwrite'] = TRUE;


    $this->load->library('upload', $config);
    if(!$this->upload->do_upload('userfile')){
        $this->session->set_flashdata('upload_error', $this->upload->display_errors());
    }
}

View

<?php echo form_open_multipart('events/add_now'); ?>
                <div class="panel-body">
                    <div class="row">
                        <div class="form-group">
                            <label class="control-label">Name</label>
                            <input type="text" class="form-control" name="Event_Name" value="<?php echo set_value('Event_Name'); ?>" placeholder="Enter Event Name">
                            <small class="text-danger"><?php echo form_error('Event_Name'); ?></small>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label">Start Date</label>
                                <input type="text" class="form-control" name="Event_Start" value="<?php echo set_value('Event_Start'); ?>" placeholder="Enter Start Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
                                <small class="text-danger"><?php echo form_error('Event_Start'); ?></small>
                            </div>
                        </div>

                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="control-label">End Date</label>
                                <input type="text" class="form-control" name="Event_End" value="<?php echo set_value('Event_End'); ?>" placeholder="Enter End Date" onclick="this.type='datetime-local'" onblur="this.type='text'" min="<?php echo date('Y-m-d'); ?>">
                                <small class="text-danger"><?php echo form_error('Event_End'); ?></small>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-9">
                            <div class="form-group">
                                <label class="control-label">Location</label>
                                <input type="text" class="form-control" name="Event_Location" value="<?php echo set_value('Event_Location'); ?>" placeholder="Enter Event Location">
                                <small class="text-danger"><?php echo form_error('Event_Location'); ?></small>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <textarea placeholder="Event Description" name="Event_Description" rows="10" class="form-control"></textarea>
                    </div>
                    <div class="row">
                        <div class="form-group">
                                <label class="control-label">Image Attachment (Optional)</label>
                                <input type="file" class="form-control" name="userfile" value="<?php echo set_value('userfile'); ?>" placeholder="Upload Image">
                                <?php if($this->session->flashdata('upload_error')): ?>
                                <small class="text-danger"><?php echo $this->session->flashdata('upload_error'); ?></small>
                            <?php endif; ?>
                            </div>
                    </div>
                </div>
                <div class="panel-footer text-right">
                    <button class="btn btn-success" type="submit">Submit</button>
                </div>
            </form>
1

1 Answer 1

0

First load the url helper in your controller...

$this->load->helper('url');

Then set the upload file path as:

$config['upload_path'] = base_url('resources/images/events_photo/temp/');

Try it might works.

And what about these two functions

$this->add();

$this->success();
Sign up to request clarification or add additional context in comments.

2 Comments

I tried doing that base_url but it says in the error that it's not a valid path, so i think the path is not the problem, also the code is working on my other project. The add and success is a function.
did u load URL helper?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.