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...
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());
}
}
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().
im trying to write a code where i can submit a form enter the contents in a database, at the same time perform a file upload and have it stored in a folder inside my server
the folder location is called uploads which is located at the root of my site
here is my code
controller (site.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
$this->load->model("site_model");
$this->load->helper(array('form', 'url'));
}
public function cmain($type,$page)
{
$data = $this->_initialize_data(); //this is just a bunch of variables that im calling in another function
$data['type'] = $type;
$data['page'] = $page;
$this->load->vars($data);
$this->load->view('site/cmain');
}
public function m1()
{
$this->load->library('upload', $config);
if(isset($_POST['m1']))
{
$suffix = $this->input->post("suffix");
$fn = $this->input->post("fn");
$mn = $this->input->post("mn");
$ln = $this->input->post("ln");
$newdata = array('suffix'=>$suffix,
'fn'=>$fn,
'mn'=>$mn,
'ln'=>$ln,
);
//this code is for the file upload
$config['upload_path'] = 'uploads';
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
$data = array('upload_data' => $this->upload->data());
//end of file upload codes
$this->db->insert('myself', $newdata);
redirect(base_url() . "site/complaint");
}
}
view (cmain.php)
<form action="<?php echo base_url();?>site/m1" name="details" id="details" method="post" enctype="multipart/form-data">
<table class='table' width="100%">
<tr>
<td colspan='2'>
<b><font color="#3B608C">Personal Information</font></b>
</td>
</tr>
<tr>
<td>
Suffix (e.g. Jr., III)
</td>
<td>
<input type="text" name="suffix" id="suffix" value="">
</td>
</tr>
<tr>
<td>
First Name*
</td>
<td>
<input type="text" name="fn" id="fn" value="">
</td>
</tr>
<tr>
<td>
Middle Name*
</td>
<td>
<input type="text" name="mn" id="mn" value="">
</td>
</tr>
<tr>
<td>
Last Name*
</td>
<td>
<input type="text" name="ln" id="ln" value="">
</td>
</tr>
</table>
<table>
<tr>
<td width="50%">
Please attach documents pertinent to these complaints. <br>
(Attach a zip file if more than one document)<br>
</td>
<td align="center">
<input name = "userfile" type="file" class="input-xlarge" id = "userfile" />
</td></tr>
</table>
<input type="submit" value="Submit Form" class="pull-right" id="submit" name="m1"/>
</form>
the form posts properly in the database like the suffix,fn,ln and mn
however the file upload isnt working
i tried my best to follow what the codeigniter docs samples and only got the lines i think i needed
am i doing something wrong?
thanks
finally got it working
i used this for the file upload part
//start of file upload code
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
//end of file upload code
and changed the
<form action="<?php echo base_url();?>site/m1" name="details" id="details" method="post" enctype="multipart/form-data">
into
<?php echo form_open_multipart('site/c_upload');?>
I think you must have to set file upload preference
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
Form Helper
$this->load->helper(array('form', 'url'));
For further detail have a look at my blog post
Did you set writing permissions for 'uploads' folder? (example: php.net/manual/en/function.chmod.php)
Always do test driven development. I got below code block from this url
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
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.
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.