I want to replace/overwrite my image upload, which works. But the file have copied end up with another extension.
This is my controller
function add(){
if (isset($_POST['submit'])) {
$id = $this->input->post('nisn', TRUE);
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 1024;
$config['file_name'] = $id;
$config['overwrite'] = TRUE;
$this->load->library('upload', $
$this->upload->do_upload('userfile');
$upload = $this->upload->data();
print_r($upload);
die;
}
Related
I try to save the uploaded file in 2 paths, one inside a folder and contain the id and one outside the folder
Here's my code :
private function _uploadImage()
{
$config['upload_path'] = './upload/'.$this->product_id;
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
$config['upload_path'] = './upload/product';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
if (!is_dir('upload/'.$this->product_id)) {
mkdir('./upload/' . $this->product_id, 0777, TRUE);
}
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
When I run that code, the result is stucked
Can you help me what's wrong?
You set the upload_path key twice, which just overwrites itself. You'll need to process the upload wholly twice, with your two directories different.
Since you're using a private function, you could for example add a parameter that distinguishes the path
private function _uploadImage($path)
{
$config['upload_path'] = $path . '/' . $this->product_id;
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['overwrite'] = true;
$config['max_size'] = 1024; // 1MB
if (!is_dir($path . $this->product_id)) {
mkdir($path . $this->product_id, 0777, TRUE);
}
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')) {
return $this->upload->data("file_name");
}
return "default.jpg";
}
Now when you call your function, just include the paths:
$this->_uploadImage('./upload');
$this->_uploadImage('./upload/product');
This is my code:
config['upload_path'] = './media/resumes/';
$config['allowed_types'] = 'pdf|doc|docx';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->do_upload("myfile");
// enabling this code will result in duplication of uploaded file
$file = $this->upload->data();
$filename=$file['file_name'];
The problem is, I tried .jpg and even .php, and the files were successfully uploaded! no error!
Why is that and How can I solve it?
There is no any errors in the code, add this line $this->upload->display_errors(); and check its working
$config['upload_path'] = './media/resumes/';
$config['allowed_types'] = 'pdf|doc|docx';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->do_upload("myfile");
$this->upload->display_errors(); //add this line too
$file = $this->upload->data();
$filename = $file['file_name'];
$this->load->library('upload', $config);
After this try to add
$this->upload->initialize($config);
may be it help you.
$config['upload_path'] = './media/resumes/';
$config['allowed_types'] = 'pdf|doc|docx';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
if(!$this->upload->do_upload("myfile")){
$this->upload->display_errors();
}
else{
$file = $this->upload->data();
$filename = $file['file_name'];
}
Try this condition it should work..
public function makeUpload($field, $place) {
$config['upload_path'] = './uploads/' . $place;
$config['allowed_types'] = 'png';
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
$config['max_size'] = 30003072;
$this->load->library('upload', $config);
$this->upload->do_upload($field);
$errors = $this->upload->display_errors();
if ($errors) {
$this->error(array($errors));
} else {
$this->success(array($this->upload->data()));
}
}
public function success($data){
header('Content-Type: application/json');
http_response_code(200);
die(json_encode($data));
}
public function error($data){
http_response_code(404);
header('Content-Type: application/json');
die(json_encode($data));
}
And not forget the validation in html input, using
accept="image/*" or accept="application/pdf" more types in here
I want to upload two images with my given name
$config['upload_path'] = '/path/to/file';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['file_name'] ='logo1_'.$_POST['name'];
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image1'))
{...}
else
{
$data = $this->upload->data();
}
$config2['upload_path'] = '/path/to/file';
$config2['allowed_types'] = 'gif|jpg|png';
$config2['max_size'] = '0';
$config2['file_name'] = 'logo2_'.$_POST['name'];
$config2['max_width'] = '0';
$config2['max_height'] = '0';
$this->load->library('upload', $config2);
if ( ! $this->upload->do_upload('image2'))
{...}
else
{
$data = $this->upload->data();
}
My first image correctly saved in my folder with the name logo1_myname but my second image dosen't save as logo2_myname it save as logo1_myname1 What is wrong with my code ?
Change
$this->load->library('upload', $config2);
to
$this->upload->initialize($config2);
Since the class has already been loaded codeigniter won't load it again.
Hope this helps.
I have a problem with my multiple upload with codeigniter,
using image
another using pdf
When I uploaded the file uploaded twice and how to call different path to uploaded to database. this my code
Controller
public function upload(){
$catalog='catalog';
$userfile='userfile';
//for cover
$config['upload_path'] = './file/book/'; //Use relative or absolute path
$config['allowed_types'] = 'gif|jpg|png|';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$c=$this->upload->do_upload($userfile);
//for catalog
$config['upload_path'] = './file/book/pdf/'; //Use relative or absolute path
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$cat=$this->upload->do_upload($catalog);
if(!$this->upload->do_upload($catalog) && $this->upload->do_upload($userfile)){
$error = array('error' => $this->upload->display_errors());
$this->load->view('uploadds', $error);
}else{
$this->load->model("book_model");
$this->book_model->addnewbook();
redirect('book/book');
}
}
This model
function addnewbook(){
$fcat=array('upload_data' => $this->upload->data($userfile));
$fcatalog=array('upload_dataa' => $this->upload->data($catalog));
}
You need to handle multiple uploads independently. For this, you have to create separate custom objects for both uploads while loading the upload library. (See the code comments)
public function upload() {
// Cover upload
$config = array();
$config['upload_path'] = './file/book/';
$config['allowed_types'] = 'gif|jpg|png|';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config, 'coverupload'); // Create custom object for cover upload
$this->coverupload->initialize($config);
$upload_cover = $this->coverupload->do_upload('cover');
// Catalog upload
$config = array();
$config['upload_path'] = './file/book/pdf/';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->load->library('upload', $config, 'catalogupload'); // Create custom object for catalog upload
$this->catalogupload->initialize($config);
$upload_catalog = $this->catalogupload->do_upload('catalog');
// Check uploads success
if ($upload_cover && $upload_catalog) {
// Both Upload Success
// Data of your cover file
$cover_data = $this->coverupload->data();
print_r($cover_data);
// Data of your catalog file
$catlog_data = $this->catalogupload->data();
print_r($catlog_data);
} else {
// Error Occured in one of the uploads
echo 'Cover upload Error : ' . $this->coverupload->display_errors() . '<br/>';
echo 'Catlog upload Error : ' . $this->catalogupload->display_errors() . '<br/>';
}
}
Use the data on $cover_data['full_path'] and $catlog_data['full_path'] to update your database
$config['upload_path'] = 'frontend_assets/images/hospital';
$config['allowed_types'] = 'gif|jpg|png|jpeg|JPEG||JPG|PNG';
$this->load->library('upload', $config);
if($_FILES['logo']['name']){
$config['file_name'] = time() . $_FILES["logo"]['name'];
if (!$this->upload->do_upload('logo')) {
$this->upload->display_errors();
} else {
$upload = $this->upload->data();
$insert_data['logo'] = $config['upload_path'] . '/' . $upload['file_name'];
}
}
if($_FILES['bulding_photo']['name']){
$config['file_name'] = time() . $_FILES["bulding_photo"]['name'];
if (!$this->upload->do_upload('bulding_photo')) {
$this->upload->display_errors());
} else {
$upload = $this->upload->data();
$insert_data['bulding_photo'] = $config['upload_path'] . '/' . $upload['file_name'];
$this->image_size_fix($insert_data['bulding_photo'], $width = 200, $height = 200);
}
}
I'm trying to add time as the prefix of the image name along with the original name when uploading, But I couldn't figure it out. Please help me with the following code to add a prefix to my original file name when uploading.
<?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'] = 'Public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$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);
}
}
}
?>
You can encrypt file name with use of CI native option:
$config['encrypt_name'] = TRUE;
OR
You can do it with your own code:
$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
For some reasons, consecutive calls to the do_upload function doesn't work. It sticks to the first filename set by the first function call
$small_photo_url = $this->upload_photo('picture_small', $this->next_id.'_small ');
$medium_photo_url = $this->upload_photo('picture_medium', $this->next_id.'_medium');
$large_photo_url = $this->upload_photo('picture_large', $this->next_id.'_large ');
The filenames will all be "00001_small", "00001_small1", "00001_small2"
given the following configurations
function upload_photo($field_name, $filename)
{
$config['upload_path'] = 'Public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = $filename;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())...
I think it's because this line doesn't work the second time you call it. It does not set the configurations again
$this->load->library('upload', $config);
==========================================================================
Solution to the problem faced during consecutive do_upload function calls:
// re-initialize upload library
$this->upload->initialize($config);
/* Conf */
$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '';
$config['max_width'] = '';
$config['max_height'] = '';
$config['file_ext_tolower'] = TRUE;
/* Load the upload library */
$this->load->library('upload', $config);
For what you are exactly looking for, this worked for me:
$path = $_FILES['image']['name'];
$newName = "<Whatever name>".".".pathinfo($path, PATHINFO_EXTENSION);
when you use do_upload() function its rename file name if already exists and upload file
System->libraries->Upload.php
default function of do_upload() return true or false
replace
return $this->upload_path.$this->file_name;
and in another file
<input type='file' name='userfile'/>
<?php
$upload_file_name = $this->upload->do_upload('userfile');
$finalname;
if (!$upload_file_name) {
$finalname = 'default.png';
} else {
$split_data_upload_file_name = explode("/", $upload_file_name);
foreach ($split_data_upload_file_name as $i => $value) {
$finalname = $value;
}
}
?>
$config['file_name'] = $new_name;
Just add it align with the config codes.
This should be after the upload.
Process the filename you want here:
$a=$this->upload->data();
rename($a['full_path'],$a['file_path'].'file_name_you_want');
Try This:
$config['file_name'] = "yourCustomFileName";
If your $config is with this code: $config['encrypt_name'] = TRUE;
the name of your file will be forced to rename with a encrypt name, just remove the code.
Solution 1: Upload file with new customized and specific name (Note: this answer is applicable when you are submitting a unique name of a record, for example student name is a unique value in a record then you will use $_POST['student_name'])
$config['file_name'] = str_replace(' ', '_', $_POST['anyUniqueValue']);
//any Other unique value that you are using to submit with this file
$config['upload_path'] = './folderName/';
$config['encrypt_name'] = FALSE;
Solution 2: Upload file with new unique name
$config['file_name'] = time();
$config['upload_path'] = './folderName/';
$config['encrypt_name'] = FALSE;
Solution 3: Upload file with new unique name using codeigniter's default option
$config['encrypt_name'] = TRUE;
if you do multiple file upload, maybe you can try update library Upload.php
on system/libraries/Upload.php. On function do_upload()
$this->file_name = $this->_prep_filename($_file['name']);
to
$this->file_name = time().'_'.$this->_prep_filename($_file['name']);