I successfully uploaded the file based on the "$this->upload->data()" message. However, the file doesn't show up in D:/Drive/webcode/uploads folder. Can anyone help out? Thank you.
***move_uploaded_file() works, but not do_upload
[Codeigniter 3]
View(upload_form.php)
<form method="post" enctype="multipart/form-data">
<input type="file" name="userfile">
<input type="submit" name="submit">
</form>
Controller(Upload)
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function jobapply()
{
//Show view form
$this->load->view('upload_form');
if($this->input->post('submit')){ //Check submit button
//Confirm file is transfered here
echo basename($_FILES["userfile"]["name"]).'<br>';
//Upload Process
$config['upload_path'] = './upload/';
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
var_dump(is_dir($config['upload_path'])); //Check if this path exist
//return the uploaded data after executing do_upload()
echo "<br><pre>";
print_r($this->upload->data());
echo "</pre>";
}
}
Output
1969.png
bool(true)
Array
(
[file_name] => 1969.png
[file_type] => image/png
[file_path] => D:/Drive/webcode/uploads/
[full_path] => D:/Drive/webcode/uploads/1969.png
[raw_name] => 1969
[orig_name] =>
[client_name] => 1969.png
[file_ext] => .png
[file_size] => 1217976
[is_image] => 1
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
Image: Folder is empty
Add $config['allowed_types'] = '*'; and check.
Solved without using CI upload
Just use below in Controller:
$path = './upload/';
$temp = $_FILES['userfile']['tmp_name'];
move_uploaded_file($temp, $path . $name);
echo 'Success';
Thats it, I wouldn't bother if using CI Upload library or not, as long as it works. Cheers.
Related
Firstly can I request this is NOT marked as duplicate. I have read the other posts on SO regarding issues with the CodeIgniter upload library and sadly they do not cover this. I have also extensively read the CI documentation and everything suggests this should work correctly.
I am using a very simple form to grab the file, which is uploaded correctly to the images folder. The full_path of the file is also written successfully to a db table called images. The filename however is blank.
My form:
<?php echo form_open_multipart('image_upload/do_upload');?>
<input type="file" name="userfile" size="20" multiple="true" />
<br /><br />
<input type="submit" value="upload" />
</form>
My Controller function:
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$image_data = $this->upload->data();
echo '<pre>'; print_r($image_data); echo '</pre>';
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}
I am using
echo print_r($image_data); echo;
To display the associated image data but this is all that is returned:
Array
(
[file_name] =>
[file_type] =>
[file_path] => c:/wamp/www/honest/images/
[full_path] => c:/wamp/www/honest/images/
[raw_name] =>
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
I can't work out why it is not grabbing the file name and other details - can someone help spot what is hopefully a simple error?
Many thanks,
DP.
You are requesting your uploaded data before you actually upload anything. That's why your $image_data only contains your configuration values. Move your $image_data = $this->upload->data(); call to after actually performing the do_upload() (into your else block):
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$image_data = $this->upload->data();
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}
I want to upload a file using a registration form. I used File_Upload library of Codeigniter. But the file does not upload to the destination and no errors appears.
This is just part of my code (All of them are really huge)
Controller (seeker_register.php):
public function submit(){
...
$this->load->model('mseeker_register');
$user_id = $this->mseeker_register->register($data);
View (vseeker_register.php):
$attr = array("class" => 'form-horizontal seeker_register','id' => 'form-seeker-register');
echo form_open_multipart('seeker_register/submit',$attr);
...
<div class="col-sm-6 col-sm-offset-3">
<input name="Aks" type="file" class="fileinput" accept=".jpg, .jpeg">
</div>
Model (mseeker_register.php):
...
// Prepare Aks
$config = array(
'upload_path' => './img/users',
'allowed_types' => 'jpg|jpeg|JPG|JPEG',
'max_size' => '200',
'max_width' => '1024',
'max_height' => '768');
$this->upload->initialize($config);
$this->upload->do_upload('Aks');
$this->upload->display_errors();
exit();
...
This is $this->upload->data() output:
Array
(
[file_name] => Clipboard-2.jpg
[file_type] => image/jpeg
[file_path] => D:/khayyamkar.ir/www/img/users/
[full_path] => D:/khayyamkar.ir/www/img/users/Clipboard-2.jpg
[raw_name] => Clipboard-2
[orig_name] =>
[client_name] => Clipboard-2.jpg
[file_ext] => .jpg
[file_size] => 156.42
[is_image] => 1
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
When I scanned your code I realized that you you don't have a way to view the error(s).
To view what the error(s) are/is you need to use var_dump() or print_r() php method to view what are the error(s).
for example:
var_dump($this->upload->display_errors());
in your current code you need to change :
$this->upload->display_errors();
exit();
TO:
var_dump($this->upload->display_errors());
exit();
You need to identify the error first to come up with a solution.
:)
public function register() {
$config = array(
'upload_path' => './img/user',
'allowed_types' => 'jpg|jpeg|JPG|JPEG|png',
'max_size' => '200',
'max_width' => '1024',
'max_height' => '768');
$this->upload->initialize($config);
// you need to make sure that the upload path is existing
// and also check the folder permission to be rwxr-xr-x
// if you installed in a server where permission is required
// for you to create image inside the folder
// create a folder img/user in the root directory of the project
// where application or system located
// this will check
if ($this->upload->do_upload('Aks')) {
echo 'success'; die;
} else {
var_dump($this->upload->display_errors());die;
}
}
see File Upload CodeIgniter
What I am trying to do is after uploading the file parse the cvs file to the screen with print_r so I can do some things with it. I cannot seem to figure out how you access the file name with the $data. I have tried several things like $file_name, $data['file_name], and $this->data->$file_name. The relevant part is on line 37.
Not sure what I am doing wrong. I have looked at the code igniter documentation for the uploader library but it doesn't be sufficient to answer my question. Thanks for any help!
<?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'] = 'csv';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->load->library('getcsv');
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);
echo 'The file name is: '.$file_name;
}
}
}
?>
The filename can be anything you want. This is clearly in the documentation, you might've been reading the wrong page or something:
http://ellislab.com/codeigniter%20/user-guide/libraries/file_uploading.html
file_name: If set CodeIgniter will rename the uploaded file to this name. The extension provided in the file name must also be an allowed file type.
OR:
You can get it from $this->upload->data(), which is an array.
$this->upload->data()
This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
Use the following code :
if($this->upload->do_upload($file)){
//Get uploaded file data here
$allData=$this->upload->data();
$myFile = $allData['file_name'];
}
how to get the new generated file name by codigniter in case if the file already exists?
i am inserting image file into my project (uploads) folder and its name i.e image.png into my database table.
say car.png is the image which exists in my uploads folder. if again i am trying to upload the car.png image, then currently it is saving like car1.png into my uploads folder without replacing the older image. i want to get the new image name i.e. car1.png to save into my db but it actually saving the name which i am sending from my form i.e car.png.
To get the file name i used
$img1=$_FILES['img1']['name'];
//this gives me the name of file which i am uploading from my form i.e car.png
Please help me to solve my problem. Please....
It seems that you're not using file upload class from codeingiter. I advise you to use it because it will solve your problem.
If you a upload a file with the same name and in the options you passed you set overwrite to FALSE, codeigniter will rename your car.png to car1.png (or the next number that's available).
Then if upload was successful it will return an array with all the data related to that file
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
As you can see, this way you will get the name of the file even if it changed.
You can read more about File Upload Class and how to implement it here:
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
EDIT2
YOu have to name your input files on your view userfile1, userfile2, userfile3 and so on
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);
foreach($_FILES as $key => $value){
if ( ! $this->upload->do_upload($key)){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}else{
//This $data is the array I described before
$data = array('upload_data' => $this->upload->data());
//So if you this you will get the file name
$filename = $data['upload_data']['file_name'];
}
}
$this->load->view('upload_success', $data);
}
}
?>
I am using codeIgniter file uploading class example https://www.codeigniter.com/user_guide/libraries/file_uploading.html to upload files. In the example after i uploaded files it will show details like below in the upload success view page.
Array
(
[upload_data] => Array
(
[file_name] => VenkataKrishna10.pdf
[file_type] => application/pdf
[file_path] => C:/xampp/htdocs/upload/application/
[full_path] => C:/xampp/htdocs/upload/application/VenkataKrishna10.pdf
[raw_name] => VenkataKrishna10
[orig_name] => VenkataKrishna.pdf
[client_name] => VenkataKrishna.pdf
[file_ext] => .pdf
[file_size] => 83.27
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
)
Afterwards I tried to see entire uploaded data in an array format. My question is how to get full file path from the above array. I know there is a variable full_path but I am not able to get it. Please help me.
From your controller use
echo $data['upload_data']['full_path'];
From your view use
echo $upload_data['full_path'];
Try like this:
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = $this->upload->data();
// $data will contain full inforation
echo "Full path is:". $data['full_path'];
}
From you post, I am assuming that you have given output from print_r of some variable, say $file_detail.
If you want to get the full_path from $file_detail, you have to use
$file_detail['upload_data']['full_path']
The array is returned upon succesful upload, you just need to retrieve the index key:
Controller:
if ( ! $this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
}
else
{
$data['upload_data'] = $this->upload_data();
}
$this->load->view('myview', $data);
myview.php
<p>Full path: <?php echo $upload_data['full_path'];?></p>
if you want to delete a file from uploads after inserting into database you can remove file from uploads using unlink($filename)