I want to upload an image with CodeIgniter. I followed the tutorial in the documentation, but it's not working. Does anyone know the solution?
How do I have to send the image to the database?
View in Views/mod/mod.php
<form action="" method="post">
<?php if(isset($error)): ?>
<?php echo $error ?>
<?php endif; ?>
<?php echo form_open_multipart('upload/do_upload');?>
<label for="image">Afbeelding</label>
<input type="file" id="image" name="image" />
<input type="submit" value="upload" />
</form>
Upload succes in Views/mod/upload_succes.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload succes</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
Upload.php in controllers/upload.php
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('mod/mod', array('error' => ' ' ));
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '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());
$this->load->view('mod/mod', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('mod/upload_success', $data);
}
}
}
?>
Try this out.Hope this helps you-----------
$file_name = "";
if ($_FILES['image']['error']!= 4) {
$fileParts = pathinfo($_FILES['image']['name']);
$file_name = time() . '.' . $fileParts['extension'];
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['file_name'] = $file_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload('image');
}
Related
I'm having trouble to upload image on my local server.
I just started learning Codeigniter referencing https://www.phptpoint.com/Codeigniter-upload-file-image/ and Codeigniter documentation.
I followed each step but when I click upload button then, I can't open the page. safari says "Failed to open the page" error.
There should be some mistake I've made in the code.
Does anyone find it, please?
PS: I even got error when not attaching image. maybe the error is not about uploading image?
<?php
class ImageUpload_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
//load Helper for Form
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('imageupload_form');
}
public function upload()
{
$new_name = date('ymd') . time();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '1500';
$config['max_height'] = '1500';
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
if (! $this->upload->do_upload('profile_pic'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('imageupload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('imageupload_success', $data);
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload Image in Codeigniter</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
// I'm not sure but referenced website used "#" right before $error so I kept using. when omitted it I got errors
<?php echo #$error; ?>
<?php echo form_open_multipart('imageupload_controller/upload');?>
<?php echo "<input type='file' name='profile_pic' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>";?>
</body>
</html>
Remove these lines
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
I don't see any use case of it.
I updated few lines in your code. Please run a command given below to make your directory with writable permission.
sudo chown www-data:www-data /var/www/[new directory]
Add a variable name $path = '/var/www/html/ur_project_directory/uploads;
on the first line after function start. And change ur_project_directory with your directory name.
Here is your working code after testing.
This is the view file.
<!DOCTYPE html>
<html>
<head>
<title>Upload Image in Codeigniter</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php echo #$error; ?>
<?php echo form_open_multipart('test/upload');?>
<?php echo "<input type='file' name='profile_pic' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>";?>
<?php if ($this->session->flashdata('status')) { ?>
<label><?php echo $this->session->flashdata('status'); ?></label>
<?php } ?>
</body>
</html>
Here is your controller file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends MX_Controller {
function __construct()
{
parent::__construct();
//load Helper for Form
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('test-view');
}
public function upload()
{
$new_name = date('ymd') . time();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '1500';
$config['max_height'] = '1500';
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
if (! $this->upload->do_upload('profile_pic'))
{
$data = $this->upload->display_errors();
$this->session->set_flashdata('status', $data);
$this->load->view('test-view', $data);
}
else
{
$data = 'Your file uploaded sucessfully.';
$this->session->set_flashdata('status', $data);
$this->load->view('test-view', $data);
}
}
}
?>
i'm trying to upload a file in databse using codeigniter, the image is getting stored in the folder but i'm having issue in storing the data in databse, i don know where
im going wrong. please can any one guide me what im doing wrong ? im new to codeigniter concept.
upload.php(controller)
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ' ' ));
}
public 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('filename')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
$data = array('upload_data' => $this->upload->data('filename'),$this->input->post());
$this->Upload_model->saverecords($data);
//$this->load->view('upload_success', $data);
}
}
}
?>
Upload_model.php(model)
<?php
class Upload_model extends CI_Model
{
//Insert
function saverecords($data)
{
//saving records
$this->db->insert('latest_news', $data);
}
}
?>
Upload_form.php(view)
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<form action = "" method = "POST">
<input type = "file" name = "filename" size = "20" />
<br /><br />
<input type = "submit" value = "upload" />
</form>
</body>
</html>
You declare <form> twice! Remove <form action = "" method = "POST"> completely and just use the above form_open_multipart:
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="filename" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
Also there is no need for spaces like type = "submit"
Then in your code:
if ( ! $this->upload->do_upload('filename')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else {
//$data = array('upload_data' => $this->upload->data('filename'),$this->input->post());
$data['upload_data'] = $this->upload->data('file_name');
$this->load->model('upload_model');
$this->upload_model->saverecords($data);
//$this->load->view('upload_success', $data);
}
For future:
Do not just do $this->input->post() to assign the key value pairs to those in db. It is not very controlled and can lead to issues. Instead just define it. So if you add other data it would look something like:
$data = array(
'example1_fieldname' => $this->input->post('example1_fieldname');
'upload_data' => $this->upload->data('file_name');
);
I want to upload file in folder, following is my code for upload file in folder, the code is successfully working in my local system and also file moved to folder but on server side i got 'The upload path does not appear to be valid'. when i used $config['upload_path'] = './uploads/'; but when i used $config['upload_path'] = './assets/images/store/category'; i got success message but uploaded file is not showing in category folder.
Following is my code.
Controller
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
echo "Tst";
$this->load->view('Upload_form', array('error' => ' ' ));
}
public function Test_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());
$this->load->view('Upload_form', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
echo '<pre>';
print_r($data);
// $this->load->view('upload_success', $data);
}
}
}
?>
View ( form)
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/Test_do_upload');?>
<input type = "file" name = "userfile" size = "20" />
<br /><br />
<input type = "submit" value = "upload" />
<?= form_close(); ?>
</body>
</html>
View of success upload
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?phpforeach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?phpendforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
Code is working in my local system but on server side file not move to folder. I also given permission.
I can see there are two form tag could fire this problem. So revise it
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<form action ="<?php echo site_url('upload/Test_do_upload');?>" method="post" enctype="multipart/form-data">
<input type = "file" name = "userfile" size = "20" />
<br /><br />
<input type = "submit" value = "upload" />
</form>
</body>
</html>
May be try this code and try to create directory if its not created. Else print $error variable to find out what kind of upload error. May be this will be helpful. And remove form tag from your view its repeated.
$dir_path= 'assets/admin/uploads/course_images/';
if (!is_dir($dir_path)) {
mkdir($dir_path, 0777, TRUE);
}
$new_name = time().$this->input->post('Course_code');
//move_uploaded_file($_FILES['file']['tmp_name'], $dir_path);
$config['upload_path'] = $dir_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = FALSE;
$config['max_size'] = '1000';
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('file'))
{
// no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
/*Print Error*/
}else{
/*Insert Code*/
}
I am trying to upload photo using codeigniter but that is not working. I don't get file name it display only error.The helper files also available in my system folder but I do not upload photo.
Controller: upload.php
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url','file'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
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());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
view: upload_form.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
upload_success.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
I always specify the input field name when calling do_upload() method. This is a fragment from working example, follow the pattern and it should work.
$field_name = 'userfile';
if (isset($_FILES[$field_name]) && is_uploaded_file($_FILES[$field_name]['tmp_name'])) {
$this->load->library('upload', $config);
if (!$this->upload->do_upload($field_name)) {
//upload failed
} else {
//upload passed
}
}
Also, make sure your upload directory is writable.
I am trying to upload an image using Codeigniter, following the CI user guide - but I'm getting an error due to the url redirection.
This is my view upload_form.php:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
View 2 upload_success.php:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
Controller upload.php:
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
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());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
I added the folder "uploads" under the "project_name" folder.
I am using this url for the upload form:
localhost/project_name/index.php/upload/
After submitting I get a "page not found" error and the url becomes:
http://localhost/project_name/index.php/upload/localhost/project_name/index.php/upload/do_upload
Based on comments under question:
$config['base_url'] = 'http://localhost/codeigniter/';
$config['base_url'] = 'http://localhost/set root folder name here/';
and use this variable <?php echo
Change the variable name of upload object anything other than $config.
eg:
$this->**config2** = array(
'upload_path' => "/root/user_name/public_html/*/files",
'upload_url' => "/root/user_name/public_html/*/files",
'allowed_types' => "pdf|doc",
'overwrite' => TRUE
);
$this->load->library('upload', $this->config2);
if(!$this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}