Couldn't upload file in codeigniter text form - php

I have a text form that works and I have a file uploading form that works using the exact example from the codeigniter docs, but I don't seem to be able to put the two together.
I want a form that allows text and file upload. The text should go to the database and the file to storage with filepath saved in the DB.

The Form you need:
<?php echo form_open_multipart('upload/do_upload');?>
Text: <input type="text" name="usertext" size="20" />
<br /><br />
File: <input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
<?php echo form_close();?>
in upload controller:
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
// upload error area
}
else
{
$data = array('upload_data' => $this->upload->data());
//upload success area
$userfile = $upload_data['file_name']; // the uploaded file name
$usertext = $this->input->post('usertext'); // the text from form
// Here you can insert the value of $usertext to db
}
}

Related

Codeigniter , The upload path does not appear to be valid

I'm have a problem uploading a picture using codeigniter. I followed the online upload tutorial, but can't figure out the problem. I keep getting "The upload path does not appear to be valid". So, here's my form structure:
<?php if (isset($error)) {?>
<div class="col-md-12">
<div class="alert alert-danger" role="alert">
<?= $error ?>
</div>
</div>
<?php } ?>
<?php echo form_open_multipart('pages/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
I also checked for my directory to be a valid path and to be writable with this code:
<?php if(is_writable('./uploads') && is_dir('./uploads')){
echo 'valid';
}
else { echo (base_url('uploads'). ' ' ."is not writable");} ?>
And this returns valid.
The following is my controller structure:
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 10000000000;
$config['max_width'] = 1920;
$config['max_height'] = 1080;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('user/profile/profile', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
In construct i loaded the upload library.
With all this being said, I keep getting the error I mentioned before.
Try Using
$config["upload_path"] = $_SERVER['DOCUMENT_ROOT']."/your/desired/location/";

Uploading multiple files at a time in codeigniter with different names

I aam new to codeigniter, can any one tell me how do we can upload multiple files to the server at a same time with different names in codeigniter.
Create multiple file upload form. Here I refer to upload method in users controller.
<form method="post" action="<?php echo base_url('users/upload/'); ?>" enctype="multipart/form-data">
<label for="upload">Select : </label>
<input type="file" name="upload[]" id="upload" multiple="multiple" />
<input type="submit" name="fsubmit" id="fsubmit" value="Upload" />
</form>
And Write the following code in your upload method.
public function upload()
{
if (isset($_FILES['upload']['name'])) {
// total files //
$count = count($_FILES['upload']['name']);
// all uploads //
$uploads = $_FILES['upload'];
for ($i = 0; $i < $count; $i++) {
if ($uploads['error'][$i] == 0) {
move_uploaded_file($uploads['tmp_name'][$i], 'storage/' . $uploads['name'][$i]);
echo $uploads['name'][$i] . "\n";
}
}
}
}
The directory storage is example. You can move the files in your choice. NOTE It is not pure CodeIgniter method for multiple file uploads. Hope this helps thanks!!
you can upload files with different name in following way
in view
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile1" size="20" />
<input type="file" name="userfile2" size="20" />
in controller just pass field name to do_upload method
function do_upload()
{
$this->upload_file('userfile1');
$this->upload_file('userfile2');
$this->load->view('upload_form', $error);
}
function upload_file($field_name){
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($field_name))
{
return array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
for more information please read CI user guide User guide

Cannot Upload Excel file in CodeIgniter

I would like to upload excel file many times in CodeIgniter, but not successfully
(The filetype you are attempting to upload is not allowed.)
Controller Coding:
$this->load->library('upload');
$this->load->helper('file');
//$config['upload_path'] = XLS_PATH;
$config['upload_path'] = './public/test/files/test/';
$config['allowed_types'] = 'xls|xlsx';
$config['max_size'] = 0;
$this->upload->initialize($config);
echo $this->upload->file_type;
if (!$this->upload->do_upload('userfile'))
{
echo $this->upload->display_errors();
}
else
{
$this->upload->do_upload('userfile');
}
Views:
<form method="post" enctype="multipart/form-data">
<input type="file" id="userfile" name="userfile"/>
<input type="submit" name="submit" id="sumit" value="Test" />
</form>
There is bug in that library maybe this will help

The upload destination folder does not appear to be writable. (Codeigniter)

I have two upload scripts that do very similar functions just with different directories.
function town_upload() {
//Do not create a thumbnail
$create_thumb = false;
//Create the path for the upload
$path = './public/uploads/towns/' . $this->uri->segment(3);
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = $this->image_upload_overwrite;
if ($this->input->post('image_type') == 'main_image') {
$config['file_name'] = 'main.jpg';
$create_thumb = true;
}
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$this->session->set_flashdata('upload_error', array('error' => $this->upload->display_errors()));
} else {
//sucess so now we create the thumbnail
$this->session->set_flashdata('upload_success', $this->upload->data());
Example: If I had a town with the ID of 6, the upload directory would be public/uploads/towns/6. This works perfectly fine as the directory is created when the town is created. I have nearly the exact same function for hotels except the path is public/uploads/hotels/[ID]. I have set the permissions to 777 just to remove that from the equation for now.
I cannot figure this out as they are both very similar functions. Here is the hotel upload code:
function hotel_upload() {
//Create the path for the upload
$path = './public/uploads/hotels/' . $this->uri->segment(3);
chmod($path, 0777);
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg';
$config['max_size'] = '2048'; //kilobytes (2048 = 2mb)
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
if ($this->input->post('image_type') == 'main_image') {
$config['file_name'] = 'main.jpg';
}
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$this->session->set_flashdata('upload_error', array('error' => $this->upload->display_errors() . ' <br/>Path: ' . $path . '<br/>Image Type: ' . $this->input->post('image_type')));
} else {
//sucess so now we create the thumbnail
$this->session->set_flashdata('upload_success', $this->upload->data());
The hotel function gives me the error "The upload destination folder does not appear to be writable.".
Here are the pages HTML forms:
TOWN:
<?php echo form_open_multipart('admin/town_upload/' . $this->uri->segment(3));?>
<input type="file" name="userfile" size="20" />
<br /><br />
<p>
<input type="radio" name="image_type" checked="checked" class="radio" value="main_image" /> <label>Main Image</label>
<input type="radio" name="image_type" class="radio" value="other_image" /> <label>Other Image</label>
</p>
<input type="submit" class="submit short" value="upload" />
<?php echo form_close(); ?>
HOTEL:
<?php echo form_open_multipart('admin/hotel_upload/' . $this->uri->segment(3));?>
<input type="file" name="userfile" size="20" />
<br /><br />
<p>
<input type="radio" name="image_type" checked="checked" class="radio" value="main_image" /> <label>Main Image (shown when visitors search the website)</label>
<input type="radio" name="image_type" class="radio" value="other_image" /> <label>Standard Image</label>
</p>
<input type="submit" class="submit short" value="upload" />
<?php echo form_close(); ?>
Thanks in advance! Been working on this for hours now.. :(
Your code doesnt check the chmod works. For example, by default, apache loves to run as its own user or nobody, not you as a user. So, the chmod in your file may find the user its currently running as doesnt own the file, so the chmod fails.
First thing I would check would be the user PHP thinks it runs as, and the owner of the file.
There is the is_writable function, you could check the directory is writable.
I just ran into the same problem and found a possible reason for this behaviour. In my case, I had created the upload directory a while back. I upgraded php which put in a new php.ini file that had safe_mod = On. And now when I recreated the directory after erasing it, the new php parameters kicked in throwing this error message.
Could it just be possible that you created the town dir tree before a PHP upgrade and your hotel dir tree afterwards?
Okay, just came back to this. I think permissions got messed up somewhere, I destroyed and recreated the folder and it seemed to work.

Codeigniter image upload not working

I can't get my images to upload no matter what i do. Below is the code that is in my model:
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
return $error;
}
else
{
$data = array('upload_data' => $this->upload->data());
return $data;
}
Here is the code that is in my controller:
function do_upload()
{
$this->Upload_model->do_upload();
}
And here is my form:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('upload/do_upload');?>
<?php if(isset($buttons)) : foreach($buttons as $row) : ?>
<h2><?php echo $row->name; ?></h2>
<input type="file" name="userfile" size="20" />
<input type="hidden" name="oldfile" value="<?php echo $row->image_url; ?>" />
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
<br /><br />
<label>Url: </label><input type="text" name="url" value="<?php echo $row->url; ?>" /><br /><br />
<input type="submit" value="submit" />
</form>
<?php endforeach; ?>
<?php endif; ?>
</body>
</html>
Can someone tell me what it is that i'm doing wrong? I'm a complete newbie to codeigniter and it is really leaving me at a dead end. As well as this I want to be able to write the path of the file to my database but i can't figure that out either so if someone has an answer or even point me in the right direction I would be extremely grateful.
Thanks
EDIT -----------
The image i was uploading was too tall/wide. But if someone could point me in the direction of how to write the filepath to the database, that would be great!
Did something similar with a project - basically you just need to turn the path into a string and write it to your db...
$fileup = $this->upload->data();
$myfilepath = $fileup['file_name'];
$databack = array(
'your_field'=> 'http://www.yourserver.com/location/'.$myfilepath.'',
);
$DB->where('your_id_field', $id_value);
$DB->update('your_table', $databack);
The trickiest bit is just tying it to your database!
You can decrease the height and width of image which you are setting 1024 and 768.
$config['max_width'] = '800';
$config['max_height'] = '600';
and secondly, I may suggest, you can store just the imagename say 'abc.jpg', not the entire path of the image. You don't need to store entire path in database because you can get this path by doing same when you were setting the path while uploading an image.

Categories