I am new to CodeIgniter. I've tried uploading my files using as follows:
(createAlbum.php)
<form method="post" action="createAlbum_db.php" enctype="multipart/form-data">
<table>
<tr>
<td>Album Name: </td><td><input type="text" name="album_name"></td>
</tr>
<tr>
<td>Photos for album: </td><td><input type = "file" name="photos[]" multiple="true"></td>
</tr>
<tr>
<td><input type="submit" value="Create Album"></td>
</tr>
</table>
</form>
(createAlbum_db.php)
for ($i=0; $i < count($_FILES['photos']['name']);$i++) {
if (move_uploaded_file(base_url().'assets/images/'.$_FILES['photos']['name'][$i],$_FILES['photos']['tmp_name'][$i])) {
echo '<br>success';
} else {
echo '<br>Failure';
echo $_FILES['photos']['error'][$i];
}
}
My assets directory lies in the same directory where application and system files reside.
What I am not being able to figure out is what should I keep in
move_uploaded_file(**DESTINATION????**,$_FILE['photos']['tmp_name']);
I have gone through most of the post and I found CodeIgniter's native uploader class difficult to use.
Codi. provides upload function for that
<?php
$config['upload_path'] = // Here Goes Path;
$config['allowed_types'] = // Here Goes allowed file types;
$config['max_size'] = // you can validate max. size;
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
echo $this->upload->display_errors();
}
else
{
$img = $this->upload->data();
$img_name = $img['file_name'];
}
?>
Just Like this, you can upload single file....
Since you need path on server use realpath('assets/images/') instead of base_url().
Related
I have tried everything but still i cant figure out why the uploaded file is not saving anywhere.....
My HTML CODE:
<form enctype='multipart/form-data' action='generate_xml.php' method='POST'>
<table>
<tr>
<td>Enter Remote ID :</td> <td> <input type='text' name='remote' required /></td><br /> </tr><tr>
<td>Enter Alter_id : </td><td> <input type='text' name='alter' required/></td> <br /> </tr><tr>
<td>Enter Master ID : </td><td><input type='text' name='master' required/> </td><br /></tr><tr>
<td>Enter Vch ID : </td><td> <input type='text' name='vch' required/> </td><br /></tr><tr>
<td>Enter Date Account : </td><td><input type='text' name='date' required/> </td><br /></tr><tr>
<td>Choose a file to upload: <input name='uploadedfile' type='file' /></td><br /></tr>
<tr><td>
<input type='submit' value='submit' /></td>
</tr>
</table>
</form>
Here's my Code
if(! empty($_FILES['uploadedfile']['name']))
{
$this->config->load('je_settings',TRUE);
$tally_folder_path = $this->config->item('tally_folder_path');
$template_file_path = FCPATH;
$tally_folder_path="/home/torrez/Public/";
$file_type = $_FILES['uploadedfile']['type'];
$allowed = array('text/csv','text/comma-separated-values');
if( ! empty($_FILES['uploadedfile']['name']) && in_array($file_type, $allowed))
{
$tally_src_file = $tally_folder_path . basename( $_FILES['uploadedfile'] ['name']);
$name = basename( $_FILES['file']['name']);
move_uploaded_file($name, $tally_folder_path);
}
else
{
die("No file specified or Format not supported");
}
Please help!!!
Include src file path instead of file file path....
$tally_src_file = $tally_folder_path . basename( $_FILES['uploadedfile']['name']);
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $tally_src_file);
Change name to tmp_name and $_FILES['uploadedfile']
$name = $_FILES['uploadedfile']['tmp_name'];
you can try this this might help you
your code is:
$file_type = $_FILES['uploadedfile']['type'];
$allowed = array('text/csv','text/comma-separated-values');
there will be problem with allowed type array
you define it like
$allowed = array('csv','txt','jpg');
Remember to give 777 permission to the folder to which you uploading files.
I think then it will work.
as Its recommended to not use $_FILES['file']['type']
in place of that you can use
$ext = filetype ( string $filename );
it return extension of file.
can refer: http://php.net/manual/en/function.filetype.php
I hope it will help you!
I would recommend you to used the codeigniter uploading library.
https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
-> Make sure that the path you are uploading is real and writable
-> Dump your $_FILES and see if it displays the right value and array index
Here is my sample code when uploading:
if (isset($_FILES['userfile']) && is_uploaded_file($_FILES['userfile']['tmp_name'])) {
$config['upload_path'] = $path = 'Your Path'; //MAKES SURE THIS PATH IS WRITABLE AND A REAL PATH
$config['allowed_types'] = 'csv';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
//error in upload
var_dump($this->upload->display_errors());
}
else
{
//success upload
var_dump($this->upload->data());
}
}
anyone can help me.. its work only if at least 2 files uploaded and then the first file didn't move to upload folder.. anyone can help.. here's my code
// view
<?php echo form_open_multipart($this->uri->uri_string()); ?>
<p>
<?php echo form_label('Image') ?>
<input type="file" name="userfile[]" size="20" class="multi" accept="gif|jpg|png"/>
</p>
// controller
function album($id){
if (isset($_POST['submit']))
{
$config['upload_path'] = './assets/gallery/'; // server directory
$config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image
$config['max_size'] = '1000'; // in kb
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->load->library('Multi_upload');
$files = $this->multi_upload->go_upload();
if ( ! $files )
{
$data['error'] = $this->upload->display_errors();
}
else
{
$data = array('upload_data' => $files);
}
}
$id = $this->uri->segment(3);
$data['query'] = $this->administrator_model->getAllPhoto($id);
$data['current'] = 'home';
$data['side'] = 'gallery';
$data['attr'] = 'view_album';
$data['content'] = 'backend/administrator_manage';
$data['sidebar'] = 'backend/home_sidebar';
$this->load->view("backend/index", $data);
}
// multi upload library
i use from here
with little modification
It is very simple to use pure PHP multi file Upload. Look into this script
View
<form method="post" action="<?php echo base_url('users/upload/'); ?>" enctype="multipart/form-data">
<label for="upload">Select : </label>
<input type="file" name="userfile[]" id="userfile" multiple="multiple" />
<input type="submit" name="fsubmit" id="fsubmit" value="Upload" />
</form>
Controller
public function album($id)
{
if (isset($_FILES['userfile']['name'])) {
// total files //
$count = count($_FILES['userfile']['name']);
// all uploads //
$uploads = $_FILES['userfile'];
for ($i = 0; $i < $count; $i++) {
if ($uploads['error'][$i] == 0) {
// FCPATH = root directory project
move_uploaded_file($uploads['tmp_name'][$i], FCPATH . 'assets/gallery/' . $uploads['name'][$i]);
echo $uploads['name'][$i] . "\n";
}
}
}
}
Hope this helps. Thanks!!
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
Hi I wanted to upload a 100 x 100 pixel image as a logo on my website for each user. It works on my localhost but doesn't work online (image is not uploaded). Well the concept is simple I just upload it and rename the image base on their ID's then display it on their page for them to view it. Another thing, the localhost version changes the image on the specified path but doesn't display it after upload somethimes but sometimes it works just fine. Well that not a big case here I just think that it might be a clue for solving this problem. Here's my code so far:
this is declared on top:
$data['base'] = $this->config->base_url();
$data['check_error'] = false;
$data['error_message'] = array();
$data['id'] = $this->session->userdata('id');
This is the code:
if (isset($_FILES['userfile']) && is_uploaded_file($_FILES['userfile']['tmp_name'])){
$new_name = $data['id'];
$config['upload_path'] = './Logos/';
$config['allowed_types'] = 'jpg|png|gif';
$config['encrypt_name'] = true;
$config['max_size'] = '100';
$config['max_width'] = '100';
$config['max_height'] = '100';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
array_push($data['error_message'], "You have the following errors in your entry:\n");
array_push($data['error_message'], "- Logo must be 100x100 pixels in size not exceeding 100KB with JPG,PNG or GIF format");
array_push($data['error_message'], "\nLogo upload failed.");
$data['check_error'] = true;
}
else
{
$data = array('upload_data' => $this->upload->data());
//DO UPDATE PART HERE
$file = $data['upload_data']['file_name'];
rename($config['upload_path'] . $file, $config['upload_path'] .$new_name.'.jpg');
//GO TO SETTINGS
$this->load->helper('url');
redirect($data['base'].'settings');
}
}
HTML:
<tr>
<td><p class="titles">Logo</p></td>
<td>
<div>
<input type="text" id="fileName" class="file_input_textbox" readonly="readonly">
<div class="file_input_div">
<input type="button" value="Browse" class="file_input_button" />
<input type="file" class="file_input_hidden" onchange="javascript: document.getElementById('fileName').value = this.value" id="upload" name="userfile" />
</div>
</div>
</td>
</tr>
<tr>
<td>
</td>
<td>
<p class="titles">100px x 100px jpg, png or gif only.</p>
</td>
</tr>
<tr>
<td><p class="titles">Current Logo</p></td>
<td>
<img src="<?php if(is_array(#getimagesize($base."Logos/".$id.".jpg"))){echo $base."Logos/".$id.".jpg";}else{echo $base."Logos/default.jpg";} ?>" style="margin:0px 0px 0px 90px;"/>
</td>
</tr>
I also use CodeIgniter_2.1.3 and 5.4.3 in PHP locally and 5.2.17 online. Going nuts haha!
You should look into the file name part. Firstly you have set the condition encrypt_name to true. While you anyway rename your file to the id this is not neccesary.
You set the new filename to be equal to
$data['id'].".jpg"
this will never work if uploaded file is a GIF or PNG image...
I am working on my second CI application. I have created some simple CRUD methods in my controller. My client has requested that each record includes an image. I have searched the forums and other resources for help but haven’t had much luck.
I have managed to upload files into a directory using the File Uploading Class, my problem though is how to associate the uploaded file/s with the relevant record.
Here are the relevant parts of my MVC.., any help/point in the right direction would be appreciated.
View - admin/locationEdit.php
<form method="post" action="<?php echo $action; ?>">
<div class="data">
<table>
<tr>
<td width="30%">ID</td>
<td><input type="text" name="id" disabled="disable" class="text" value="<?php echo $this->validation->id; ?>"/></td>
<input type="hidden" name="id" value="<?php echo $this->validation->id; ?>"/>
</tr>
<tr>
<td valign="top">Name<span style="color:red;">*</span></td>
<td><input type="text" name="name" class="text" value="<?php echo $this->validation->name; ?>"/>
<?php echo $this->validation->name_error; ?></td>
</tr>
<tr>
<td valign="top">Address<span style="color:red;">*</span></td>
<td><input type="text" name="address" class="text" value="<?php echo $this->validation->address; ?>"/>
<?php echo $this->validation->address_error; ?></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save"/></td>
</tr>
</table>
</div>
</form>
Controller - location.php
function add(){
// set validation properties
$this->_set_fields();
// set common properties
$data['title'] = 'Add new location';
$data['message'] = '';
$data['action'] = site_url('admin/location/addLocation');
$data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=>'back'));
// Write to $title
$this->template->write('title', 'Admin - Add New Location!');
// Write to $sidebar
$this->template->write_view('content', 'admin/locationEdit', $data);
// Render the template
$this->template->render();
}
function addLocation(){
// set common properties
$data['title'] = 'Add new location';
$data['action'] = site_url('admin/location/addLocation');
$data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=>'back'));
// set validation properties
$this->_set_fields();
$this->_set_rules();
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$path_to_uploads='./uploads';
$config['upload_path'] = $path_to_uploads;
$this->load->library('upload', $config);
$upload_data=$this->upload->data();
$file_name=$upload_data['file_name'];
$full_file_path = $path_to_uploads.'/'.$file_name;
// run validation
if ($this->validation->run() == FALSE){
$data['message'] = '';
}else{
// save data
$location = array('name' => $this->input->post('name'),
'address' => $this->input->post('address'),
'image_url' => $full_file_path);
$id = $this->locationModel->save($location);
// set form input name="id"
$this->validation->id = $id;
// set user message
$data['message'] = '<div class="success">add new location success</div>';
}
// Write to $title
$this->template->write('title', 'Admin - Add New Location!');
// Write to $sidebar
$this->template->write_view('content', 'admin/locationEdit', $data);
// Render the template
$this->template->render();
}
Within your upload function, get the path of the uploaded file:
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$path_to_uploads='./uploads';
$config['upload_path'] = $path_to_uploads;
$this->load->library('upload', $config);
//add this
$this->upload->initialize($config);
if (!$this->upload->do_upload()){
$error = $this->upload->display_errors();
echo "<script>alert($error);</script>";
}else{
$upload_data=$this->upload->data();
$file_name=$upload_data['file_name'];
$full_file_path = $path_to_uploads.'/'.$file_name;
}
then you can return $full_file_path back to the method that called it and insert it into the db, or just insert directly.