I am working on a gallery which is supposed to say "Please upload an image" if there are no images in the folder, but next to that also show the image uploaded but these things arent working correctly.
no errors are being shown.
Uploading images and changing them into thumbnails work and they go in the correct folder, but pulling them out is a different story, I have checked these values and they go to the correct destination:
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url() . 'images/';
and this is my autoload:
$autoload['helper'] = array('url', 'form', 'file');
this is the view where I am suspecting $data is coming out empty?:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Gallery</title>
</head>
<body>
<div id="gallery">
<?php if (isset($data) && count($data)) : ;?>
<?php foreach($images as $image): ?>
<div class="thumb">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['thumb_url']; ?>" />
</div>
<?php endforeach; else: ?>
<div id="blank_gallery">Please upload an image</div>
<?php endif; ?>
</div>
<div id="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>
</body>
</html>
this is my controller:
<?php
class Gallery extends CI_Controller
{
function index()
{
$this->load->model('Gallery_model');
if($this->input->post('upload'))
{
//handle upload
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$this->load->view('gallery', $data);
}
}
?>
this is the model:
<?php
class Gallery_model extends CI_Model{
var $gallery_path;
var $gallery_path_url;
function __construct()
{
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url() . 'images/';
}
function do_upload()
{
//handle userfile
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
die($this->upload->display_errors());
}
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ratio' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
if(!$this->image_lib->resize())
{
die($this->image_lib->display_errors());
}
}
function get_images(){
$files = scandir($this->gallery_path);
$files = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($files as $file){
$images[] = array(
'url' => $this->gallery_path_url . $file,
'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file,
);
}
}
}
?>
Any help much appreciated!
function get_images(){
/* all the stuff you already have... */
/* return the array! */
return $images;
}
Related
I am trying to upload single image using codeigniter. But getting error
"Undefined property: Main::$upload
Filename: controllers/Main.php
Line Number: 14"
This is my controller file "controller/Main.php"
<?php
class Main extends CI_Controller
{
public function index(){
$this->load->view('main_view', array('error' => ''));
}
public function upload(){
$config['upload_path'] = "./images/";
$config['allowed_types'] = 'jpg|png|gif';
$this->load->initialize('upload', $config);
if(!$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('main_view', $error);
}
else {
$file_data = $this->upload->data();
$data['img'] = base_url().'/images/'.$file_data['file_name'];
$this->load->view('success_msg', $data);
}
}
}
?>
and this is my view file "views/main_view.php"
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<?php echo $error; ?>
<?php echo form_open_multipart('main/upload');?>
<input type="file" name="userfile" />
<input type="submit" name="Submit" value="upload image"/>
</form>
</body>
</html>
when image upload is successful I'm trying to show the image by this "view/success_msg.php"
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<h1>File has been uploaded.</h1>
<img src="<?php $img?>" width="300" height="300">
</body>
</html>
my image folder name is "images" and it is in the root directory. Please help my why I am having this error ?
Maybe the upload library isn't loaded. Change the first few lines ofpublic function upload() to the following
public function upload(){
$config['upload_path'] = "./images/";
$config['allowed_types'] = 'jpg|png|gif';
$this->load->library('upload', $config);
By sending $config to load->library you do not need to use initialize().
Controller
public function upload(){
$cc = $_FILES['userfile']['name'];
$extension = pathinfo($cc, PATHINFO_EXTENSION);
$newfile = preg_replace('/[^A-Za-z0-9]/', "", $_FILES["userfile"]['name']);
$config['file_name'] = time() . $newfile;
$ss = ".";
$picture1 = time() . $newfile . $ss . $extension;
$config['upload_path'] = "./images/";
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
echo $this->upload->display_errors();
} else {
$this->load->view('success_msg', $picture1 );
}
}
View page
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<?php echo $error; ?>
<?php echo form_open_multipart('main/upload');?>
<input type="file" name="userfile" />
<input type="submit" name="Submit" value="upload image"/>
</form>
</body>
</html>
view/success_msg.php
<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
<h1>File has been uploaded.</h1>
<img src="<?php $picture1 ?>" width="300" height="300">
</body>
</html>
This image shows what I need to do with codeigniter. I have one page with several div tags. I need to upload images and show them at the same place. But there should be 3 different images and 3 different file locations for these images to save. I tried number of ways. Please anyone with idea help me.
My controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Upload_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index(){
$this->load->view('file_view', array(
'error' => ' '
));
}
public function file_view()
{
$this->load->view('file_view', array(
'error' => ' '
));
}
public function do_upload()
{
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data = array(
'upload_data' => $this->upload->data()
);
$this->load->view('file_view', $data);
} else {
$error = array(
'error' => $this->upload->display_errors()
);
$this->load->view('file_view', $error);
}
}
public function do_upload2()
{
$config = array(
'upload_path' => "./uploads/index2/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data = array(
'upload_data' => $this->upload->data()
);
$this->load->view('file_view', $data);
} else {
$error = array(
'error1' => $this->upload->display_errors()
);
$this->load->view('file_view', $error);
}
}
}
?>
My View
<div id="1">
<?php echo form_open_multipart( 'upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</div>
<div id="2">
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<li>
</li>
<img alt="Your uploaded image" src="<?=base_url(). 'uploads/' . $upload_data['file_name'];?>">
</ul>
</div>
<div id="3">
<?php echo form_open_multipart( 'upload_controller/do_upload2');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</div>
<div id="4">
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<li>
</li>
<img alt="Your uploaded image" src="<?=base_url(). 'uploads/index2/' . $upload_data['file_name'];?>">
</ul>
<p>
<?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?>
</p>
</div>
Try This Code:
<form method="post" action="uploader/go" enctype="multipart/form-data">
<input type="file" name="image1" /><br />
<input type="file" name="image2" /><br />
<input type="file" name="image3" /><br />
<input type="file" name="image4" /><br />
<input type="file" name="image5" /><br />
<input type="submit" name="go" value="Upload!!!" />
</form>
Here Controller code:
class Uploader extends Controller {
function go() {
if(isset($_POST['go'])) {
/* Create the config for upload library */
/* (pretty self-explanatory) */
$config['upload_path'] = './assets/upload/'; /* NB! create this dir! */
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
/* Load the upload library */
$this->load->library('upload', $config);
/* Create the config for image library */
/* (pretty self-explanatory) */
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE;
/* Set the height and width or thumbs */
/* Do not worry - CI is pretty smart in resizing */
/* It will create the largest thumb that can fit in those dimensions */
/* Thumbs will be saved in same upload dir but with a _thumb suffix */
/* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */
$configThumb['width'] = 140;
$configThumb['height'] = 210;
/* Load the image library */
$this->load->library('image_lib');
/* We have 5 files to upload
* If you want more - change the 6 below as needed
*/
for($i = 1; $i < 6; $i++) {
/* Handle the file upload */
$upload = $this->upload->do_upload('image'.$i);
/* File failed to upload - continue */
if($upload === FALSE) continue;
/* Get the data about the file */
$data = $this->upload->data();
$uploadedFiles[$i] = $data;
/* If the file is an image - create a thumbnail */
if($data['is_image'] == 1) {
$configThumb['source_image'] = $data['full_path'];
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
}
}
}
/* And display the form again */
$this->load->view('upload_form');
}
}
I have a controller that shows modules for my each position
Array
(
[0] => Array
(
[layout_module_id] => 1
[layout_id] => 1
[module_id] => 1
[position] => column_left
[sort_order] => 1
)
[1] => Array
(
[layout_module_id] => 2
[layout_id] => 1
[module_id] => 2
[position] => column_left
[sort_order] => 2
)
)
Above currently I have only two modules set and the are in the position of column left.
Because the position views are out side of the foreach loop they are picking up that module even though not set for that position? As shown in image.
Question: How can I make sure that the module will only display in its set position view.
public function index() {
$layout_id = $this->getlayoutID($this->router->class);
$modules = $this->getlayoutsmodule($layout_id);
echo '<pre>';
print_r($modules);
echo "</pre>";
$data['modules'] = array();
foreach ($modules as $module) {
$this->load->library('module/question_help');
$data['modules'][] = $this->load->view('module/question_help', $this->question_help->set(), TRUE);
}
// Position Views
$data['column_left'] = $this->load->view('column_left', $data, TRUE);
$data['column_right'] = $this->load->view('column_right', $data, TRUE);
$data['content_top'] = $this->load->view('content_top', $data, TRUE);
$data['content_bottom'] = $this->load->view('content_bottom', $data, TRUE);
// Main view
$this->load->view('welcome_message', $data);
}
public function getlayoutsmodule($layout_id) {
$this->db->select('*');
$this->db->from('layouts_module');
$this->db->where('layout_id', $layout_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
}
}
Each of the position views have the same foreach loop
<?php if ($modules) { ?>
<?php foreach ($modules as $module) { ?>
<?php echo $module;?>
<?php } ?>
<?php }?>
main view
<div class="container">
<div class="row">
<?php echo $column_left; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>">
<?php echo $content_top; ?>
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/Welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
<?php echo $content_bottom; ?>
</div>
<?php echo $column_right; ?></div>
</div>
Got it working I have had to use a foreach switch statement
<?php
class Welcome extends CI_Controller {
public function index() {
$layout_id = $this->getlayoutID($this->router->class);
$column_left = '';
$column_right = '';
$content_top = '';
$content_bottom = '';
$position = array('column_left', 'column_right', 'content_top', 'content_bottom');
foreach ($position as $key) {
switch ($key) {
case 'column_left':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'column_left');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$column_left = $this->load->view('column_left', $data, TRUE);
break;
case 'column_right':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'column_right');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$column_right = $this->load->view('column_right', $data, TRUE);
break;
case 'content_top':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'content_top');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$content_top = $this->load->view('content_top', $data, TRUE);
break;
case 'content_bottom':
$data['modules'] = array();
$layout_module_results = $this->getlayoutsmodule($layout_id, 'content_bottom');
if (!empty($layout_module_results)) {
foreach ($layout_module_results as $layout_module_result) {
$data['modules'][] = array(
'position' => $layout_module_result['position']
);
}
}
$content_bottom = $this->load->view('content_bottom', $data, TRUE);
break;
}
}
$data['column_left'] = $column_left;
$data['column_right'] = $column_right;
$data['content_top'] = $content_top;
$data['content_bottom'] = $content_bottom;
$this->load->view('welcome_message', $data);
}
}
i just learning codeigniter, and i find some tutorials for uploading and getting images from folder,and Function do_upload () works and creates directories, but the function get_images () does not work
This is my model:
var $gallery_path;
var $gallery_path_url;
function Gallery_model()
{
$this->gallery_path = realpath(APPPATH .'../images');
$this->gallery_path_url = base_url().'images/';
}
function get_images(){
$files = scandir($this->gallery_path.'thumbs');
$files = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($files as $file){
$images[] = array(
'url' => $this->gallery_path_url . $file,
'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
);
}
}
Controller:
function index()
{
$this->load->model('gallery_model');
if($this->input->post('upload')){
$this->gallery_model->do_upload();
}
$data['images'] = $this->gallery_model->get_images();
$this->load->view("gallery", $data);
}
View:
<div id="gallery">
<?php if(isset($images) && count($images)):
foreach ($images as $image):
?>
<div class="thumb">
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['thumb_url']; ?>" />
</a>
</div>
<?php endforeach; else: ?>
<div id="blank_gallery">Please upload an image</div>
<?php endif; ?>
</div>
<div id="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>
I don't get any error, nothing, but image isn't being uploaded, like controller isn't called at all, when I add in contruct function log_message I do not see anything in log file, not sure what is the problem. By the way I'm using this library(http://jeromejaglale.com/doc/php/codeigniter_i18n) to manage languages, so I have in url http://getit.mysite/en
Here is my controller function
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class FileUpload extends CI_Controller {
public $view_data = array();
private $upload_config;
function __construct() {
parent::__construct();
}
public function do_upload() {
$this->load->library('upload');
$image_upload_folder = base_url() . 'uploads/';
if (!file_exists($image_upload_folder)) {
mkdir($image_upload_folder, DIR_WRITE_MODE, true);
}
$this->upload_config = array(
'upload_path' => $image_upload_folder,
'allowed_types' => 'png|jpg|jpeg|bmp|tiff',
'max_size' => 2048,
'remove_space' => TRUE,
'encrypt_name' => TRUE,
);
$this->upload->initialize($this->upload_config);
if (!$this->upload->do_upload()) {
$upload_error = $this->upload->display_errors();
echo json_encode($upload_error);
} else {
$file_info = $this->upload->data();
echo json_encode($file_info);
}
}
}
?>
and js
jQuery(document).ready(function($) {
var base_url = 'http://getit.mycom/';
$('#upload-file').click(function(e) {
e.preventDefault();
$('#userfile').uploadify('upload', '*');
});
$('#userfile').uploadify({
'auto': false,
'swf': base_url + 'js/uploadify/uploadify.swf',
'cancelImg': base_url + 'js/uploadify/uploadify-cancel.png',
'uploader': base_url + 'fileupload/do_upload',
'fileTypeExts': '*.jpg;*.bmp;*.png;*.tif',
'fileTypeDesc': 'Image Files (.jpg,.bmp,.png,.tif)',
'fileSizeLimit': '8MB',
'fileObjName': 'userfile',
'buttonText': 'Select Photo(s)',
'multi': true,
'removeCompleted': false
});
});
and view file(add user) :
<?php echo form_open_multipart(); ?>
<ul class="unstyled">
<li>
<?php echo form_upload('userfile', '', 'id="userfile"'); ?>
<?php echo (isset($error)) ? $error : ''; ?>
</li>
<li>
<?php echo form_button(array('content' => 'Upload', 'id' => 'upload-file', 'class' => 'btn btn-large btn-primary')); ?>
</li>
</ul>
<?php echo form_close(); ?>
At the controller code, change if (!$this->upload->do_upload()) to if (!$this->upload->do_upload('Filedata')). Just try.