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);
}
}
}
?>
Related
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 a beginner at codeigniter. since past 2 days i've been trying to build a file upload system using CI's guide. The problem is that the images are not getting uploaded in the upload directory ./uploads/ I suspect that the problem lies with the permissions of the upload folder but i am not sure. I am building the application locally using XAMPP on Windows 7. after pushing the upload button, nothing happens. (it is supposed to either show display errors or redirect to a success page.)
View- upload_form.php
`
<title>Upload Form</title>
</head>
<body>
<form>
<?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>
success page - upload_success.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was uploaded successfully</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|png|jpg|jpeg' ;
/*$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); //show error page
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success' , $data);
}
}
}
?>
routing - routes.php
$route['default_controller'] = 'welcome';
$route['404_override'] = "";
$route['(:any)'] = "upload";
View- upload_form.php
<form> //remove these thing, form_open_multipart('upload/do_upload') will do the job. If you are giving both, then one form tag is unable to find it's closing tag and action path;
In controller, make sure $config['upload_path'] = './uploads/'; path is writable and it exist.
Rest code is looking good and workable.
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');
}
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);
}