I tried to create a barcode using zend_barcode library , when I try to run perfectly but when I try to do a project there is a problem , I try to create a barcode based on $id_barcode I input when I insert does not work .
This my Controllers
function insert() {
$barcode = $this->input->post('id_barcode');
$imageResource = Zend_Barcode::factory('code128', 'image', array('text'=>$barcode), array())->draw();
$imageName = $barcode.'.jpg';
$imagePath = './result/';
if (!is_dir($imagePath)) {
mkdir ($imagePath, 777, TRUE);
}
$createbarcode = imagejpeg($imageResource, $imagePath.$imageName);
$this->product_m->insert_produk($createbarcode);
redirect('admin/product');
}
this my Models
function insert_produk($createbarcode){
$data = array(
'id_barcode' => $this->input->post('id_barcode'),
'reseller_produk'=> $this->input->post('reseller_produk'),
'nama_produk' => $this->input->post('nama_produk'),
'jenis_produk' => $this->input->post('jenis_produk'),
'harga_produk' => $this->input->post('harga_produk'),
'stock_produk' => $this->input->post('stock_produk'),
'date' => $this->input->post('date'),
'image' => $createbarcode);
$this->db->insert('tb_produk', $data);
}
Related
I have a problem uploading a file can you help me? when I upload a smaller file it runs smoothly but when I upload a larger file it's problematic, does anyone know the solution? this is the controller part:
$title = $this->request->getPost('title');
$kategori = $this->request->getPost('kategori');
$seo = str_replace(" ", "-", strtolower($kategori));
$deskripsi = $this->request->getPost('deskripsi');
$data=array();
$file = $this->request->getFile('imagefile');
$data=array();
if ($file->getSize() > 0){
$image=$file->getRandomName();
$file->move('./uploads/peta/',$image);
$data = array(
'title' => $title,
'kategori' => $kategori,
'seo' => $seo,
'deskripsi' => $deskripsi,
'file' => $image
);
}else{
$data = array(
'title' => $title,
'kategori' => $kategori,
'seo' => $seo,
'deskripsi' => $deskripsi
);
}
Use $files->hasFile('') to check whether file exist. And then check other file properties as mentioned below.
$files = $this->request->getFiles();
if ($files->hasFile('imagefile'))
{
$file = $files->getFile('imagefile');
// Generate a new secure name
$name = $file->getRandomName();
// Move the file to it's new home
$file->move('/path/to/dir', $name);
echo $file->getSize(); // 1.23
echo $file->getExtension(); // jpg
echo $file->getType(); // image/jpg
}
You need to check if the file is set before trying to call file methods. It seems that the file isn't being sent over your request. As you said, it works with smaller files so please check the php.ini file to see your max_upload_size settings and post_max_size settings. Change the maximum upload file size
Otherwise, to ensure your code won't fail on posts, you can do this:
if (!empty($file) && $file->getSize() > 0){
$image=$file->getRandomName();
$file->move('./uploads/peta/',$image);
$data = array(
'title' => $title,
'kategori' => $kategori,
'seo' => $seo,
'deskripsi' => $deskripsi,
'file' => $image
);
}else{
$data = array(
'title' => $title,
'kategori' => $kategori,
'seo' => $seo,
'deskripsi' => $deskripsi
);
}
This will check if there is a file before trying to use it.
I'm trying to create an Admin Controller with a csv file uploader to process it like an array.
I can't find an efficient way to do it, I tried to use $this-> fields_form, but nothing is showing up.
Then I did a tpl file with an input file, called in initContent, but I don't know how to retrieve my file in the controller.
I need to create multiple object of different classes that I made, thanks to the csv file.
Does somebody have some documentation that could help me, I've already search through prestashop dev doc, stack overflow, ect but I've didn't see anything that could help me (maybe I didn't search the good way ?)
Waiting for your help guys !
Update :
Update :
Just found a way to upload my file, but it's upload as .tmp and can't be processed as a csv file, how can I convert a tmp file to a csv ?
Here is my code :
public function __construct()
{
parent::__construct();
// Base
$this->bootstrap = true; // use Bootstrap CSS
$this->fields_options = array(
'general' => array(
'title' => $this->l('Upload DB'),
'fields' => array(
'DB_BULB_DATA' => array(
'title' => $this->l('Upload DB'),
'type' => 'file',
'name' => 'DB_BULB_DATA'
),
),
'submit' => array('title' => $this->trans('Save', array(), 'Admin.Actions')),
),
);
if(isset($_FILES['DB_BULB_DATA'])){
$headers = fgetcsv(fopen($_FILES['DB_BULB_DATA']['tmp_name'], "r+"));
print_r($headers);
}
}
There is no file type name csvfile , you need to use filed type file and then hadel the csv file after upload, check file extension then process the data.
Just find out how to do it, I feel dummy 😅
I just needed to save my tmp file as a csv to be able to use it then.
Here is the full code :
<?php
class Admin<YourModuleName>Upload<YourThings>DatabaseController extends ModuleAdminController
{
public function __construct()
{
parent::__construct();
// Base
$this->bootstrap = true; // use Bootstrap CSS
$this->name = "Admin<YourModuleName>Upload<YourThings>Database";
$this->fields_options = array(
'general' => array(
'title' => $this->l('Upload DB'),
'fields' => array(
'DB_<YourThings>_DATA' => array(
'title' => $this->l('Upload DB'),
'type' => 'file',
'name' => 'DB_<YourThings>_DATA'
),
),
'submit' => array('title' => $this->l('Save')),
),
);
}
public function initContent()
{
parent::initContent();
unset($_FILES);
}
public function postProcess()
{
$fileName = '<YourThings>Db.csv';
if (!file_exists(_PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName)) {
if (isset($_FILES['DB_<YourThings>_DATA'])) {
$tmpPath = $_FILES['DB_<YourThings>_DATA']["tmp_name"];
move_uploaded_file($tmpPath, _PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName);
$uploadCsv = file(_PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName, FILE_SKIP_EMPTY_LINES);
$Db = array_map("str_getcsv", $uploadCsv, array_fill(0, count($uploadCsv), ';'));
$keys = array_shift($Db);
foreach ($Db as $i => $row) {
$Db[$i] = array_combine($keys, $row);
}
print_r($Db);
}
} else {
$uploadCsv = file(_PS_MODULE_DIR_ . '<YourModuleName>/upload/' . $fileName, FILE_SKIP_EMPTY_LINES);
$Db = array_map("str_getcsv", $uploadCsv, array_fill(0, count($uploadCsv), ';'));
$keys = array_shift($Db);
foreach ($Db as $i => $row) {
$Db[$i] = array_combine($keys, $row);
}
print_r($Db);
}
unset($_FILES['DB_<YourThings>_DATA']);
}
}
I'm running:
- debian-linux-gnu
- codeigniter 2.2.0
- No physical links in db (legacy)
- version: 5.5.41-MariaDB-1~precise-log
Problem:
I'm writing a blog system for a webshop. After filling in the form (with ckeditor for the actual blog), I do a redirect to the reading page/view:
redirect("/blog/read/" . $blog_id, "refresh"); Only no data is found. When I refresh the page manualy imediatly after, the blog shows just fine.
I tried to sleep the process, but this didn't help. So I think it is not a db issue. Since the view is new for this particular post, no caching.
controller:
public function add_do() {
//Image handling
if (!empty($_FILES['blog_file_img'])) {
$path = $_FILES['blog_file_img']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$fileData = $this->upload('blog_file_img', $this->now . '.' . $ext);
$fileNames = $this->resize($fileData);
}
//Create blog
$blog_data = array(
"blog_img_original" => $fileData['file_name'],
"blog_img_small" => $fileNames["small"],
"blog_img_medium" => $fileNames['medium'],
"blog_img_big" => $fileNames['big'],
"blog_date_added" => $this->now,
"blog_date_modified" => $this->now,
"blog_created_by" => $this->user['userid'],
"blog_modified_by" => $this->user['userid']
);
//Create blog description
$blog_description_data = array(
'language_id' => 1,
'blog_title' => $this->input->post('blog_title', true),
'blog_desc' => $this->input->post('blog_desc', true),
'blog_text' => $this->input->post('blog_text', false)
);
$data = array_merge($blog_data, $blog_description_data);
$blog_id = $this->blog_model->blog_new($data);
redirect("/blog/read/" . $blog_id, "refresh");
}
read controller:
public function read($id){
$this->_get_message();
$blog_data = $this->blog_model->blog_entry($id);
$this->load->view('header', $this->data_view);
$this->load->view('blog/blog_entry', array('blog_data' => $blog_data));
$this->load->view('blog/blog_right', array('blog_stats' => $this->blog_model->blog_most_popular()));
$this->load->view('footer');
$this->_debug();
}
model for add
public function blog_new($data) {
$this->db->trans_start();
//Blog data extraction
$blog_data = array(
"blog_img_original" => $data['blog_img_original'],
"blog_img_small" => $data["blog_img_small"],
"blog_img_medium" => $data['blog_img_medium'],
"blog_img_big" => $data['blog_img_big'],
"blog_date_added" => $data['blog_date_added'],
"blog_date_modified" => $data['blog_date_modified'],
"blog_created_by" => $data['blog_created_by'],
"blog_modified_by" => $data['blog_modified_by']
);
$blog_id = $this->new_blog($blog_data);
//Blog description extraction
$blog_description_data = array(
'blog_id' => $blog_id,
'language_id' => 1,
'blog_title' => $data['blog_title'],
'blog_desc' => $data['blog_desc'],
'blog_text' => $data['blog_text']
);
$this->new_blog_description($blog_description_data);
$this->db->trans_complete();
return $blog_id;
}
private function new_blog($data) {
$this->initialise('blog', 'blog_id');
return $blog_id = $this->create($data);
}
private function new_blog_description($data) {
$this->initialise('blog_description', 'blog_description_id');
return $blog_description_id = $this->create($data);
}
model for read
public function blog_entry($id) {
$this->db->select("*");
$this->db->from("blog");
$this->db->join("blog_description", "blog.blog_id = blog_description.blog_id");
$this->db->join("blog_stats", "blog.blog_id = blog_stats.blog_id");
$this->db->where("blog.blog_id", $id);
$query = $this->db->get();
//Add to views stats
$this->blog_add_view($id);
return $query->result_array();
}
How to show the file upload name for jQuery Multiple File Upload Plugin v1.48?
After the users have uploaded the file, is there a way to show the file that they have uploaded under the profile?
1.if ( isset($_FILES['tb-documents']) ) {
$documents = $_FILES['tb-documents'];
foreach ( $documents['name'] as $key => $value ) {
if ( $documents['name'][$key]) {
$document = array(
'name' => $documents['name'][$key],
'type' => $documents['type'][$key],
'tmp_name' => $documents['tmp_name'][$key],
'error' => $documents['error'][$key],
'size' => $documents['size'][$key]
);
$status = wp_handle_upload($document, array('test_form' => false));
if( !isset($status['error']) ) {
$uploads = wp_upload_dir();
add_user_meta($user->ID, 'tb-documents', $uploads['url'].'/'.basename($status['file']));
}
}
}
}
The upload is working well on the user profile. However, I want to pull out the file name that they have uploaded and display all the uploaded file names to them.
Which variable should I use?
1.I tried this, $content .= '.$documents['name']'; but it doesn't work. It shows syntax error.
You have to combine results from this $documents['name'][$key] and show it to user.
For example:
if (isset($_FILES['tb-documents'])) {
$uploadedFiles = array();
$documents = $_FILES['tb-documents'];
foreach ($documents['name'] as $key => $value) {
if ($documents['name'][$key]) {
$document = array(
'name' => $documents['name'][$key],
'type' => $documents['type'][$key],
'tmp_name' => $documents['tmp_name'][$key],
'error' => $documents['error'][$key],
'size' => $documents['size'][$key]
);
$status = wp_handle_upload($document, array('test_form' => false));
if (!isset($status['error'])) {
$uploads = wp_upload_dir();
add_user_meta($user->ID, 'tb-documents', $uploads['url'] . '/' . basename($status['file']));
$uploadedFiles[] = $documents['name'][$key];
}
}
}
var_dump(implode(", ", $uploadedFiles));
}
If you want to show the uploaded files to the uploader, you can handle it easily in JQuery using the fileuploaddone callback provided by jQuery uploader
$('#fileupload')
.bind('fileuploaddone', function (e, data) {
console.log( data['result']['files'][0]['name'] );
console.log( data['result']['files'][0]['url'] );
});
If you have multiple files, you need to loop over data['result']['files'] array.
Ok, that sounds really confusing. What I’m trying to do is this. I’ve got a function that uploads/resizes photos to the server. It stores the paths in the DB. I need to attach the id of the business to the row of photos.
Here’s what I have so far:
function get_bus_id() {
$userid = $this->tank_auth->get_user_id();
$this->db->select('b.id');
$this->db->from ('business AS b');
$this->db->where ('b.userid', $userid);
$query = $this->db->get();
if ($query->num_rows() > 0) {
// RESULT ARRAY RETURN A MULTIDIMENSIONAL ARRAY e.g. ARRAY OF DB RECORDS
// ( ROWS ), SO IT DOENS'T FIT
//return $query->result_array();
// THE CORRECT METHOD IS row_array(), THAT RETURN THE FIRST ROW OF THE
// RECORDSET
$query->row_array();
}
That get’s the id of the business. Then, I have my upload function which is below:
/* Uploads images to the site and adds to the database. */
function do_upload() {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$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();
$upload = $this->upload->data();
$bus_id = $this->get_bus_id();
$data = array(
'userid' => $this->tank_auth->get_user_id(),
'thumb' => $this->gallery_path . '/thumbs/' . $upload['file_name'],
'fullsize' => $upload['full_path'],
'busid'=> $bus_id['id'],
);
echo var_dump($bus_id);
$this->db->insert('photos', $data);
}
The problem I’m getting is the following:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: id
Filename: models/gallery_model.php
Line Number: 48
I’ve tried all sorts of ways to get the value over, but my limited knowledge keeps getting in the way. Any help would be really appreciated.
not sure how to ask you a question without submitting an "answer"...
what's in line 48? which file is gallery_model.php? from the sounds of it, it could be an array key that hasn't been initialized or an issue in querying the database.
The problem is that if the business is not found, your function is not returning anything. As a result, $bus_id has nothing in it (its not even an array). You should probably have your get_bus_id() function return false like so:
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
then after you call get_bus_id() you can check if $bus_id == false and maybe return false to denote an error from do_upload()
Ok, I have it working. This is the complete and working code:
/* Uploads images to the site and adds to the database. */
function do_upload() {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$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();
$upload = $this->upload->data();
$bus_id = $this->get_bus_id();
/*
TABLE STRUCTURE =============
id, the row ID
photoname
thumb
fullsize
busid
userid
*/
$data = array(
'id' => 0 , // I GUESS IS AUTO_INCREMENT
'photoname' => '',
'thumb' => $this->gallery_path . '/thumbs/' . $upload['file_name'],
'fullsize' => $upload['full_path'],
'busid'=> $bus_id['id'],
'userid' => $this->tank_auth->get_user_id(),
);
// CHECK THE DATA CREATED FOR INSERT
$this->db->insert('photos', $data);
}
// Get Business ID from DB
function get_bus_id() {
$userid = $this->tank_auth->get_user_id();
$this->db->select('b.id');
$this->db->from ('business AS b');
$this->db->where ('b.userid', $userid);
$query = $this->db->get();
if ($query->num_rows() > 0) {
// RESULT ARRAY RETURN A MULTIDIMENSIONAL ARRAY e.g. ARRAY OF DB RECORDS
// ( ROWS ), SO IT DOENS'T FIT
//return $query->result_array();
// THE CORRECT METHOD IS row_array(), THAT RETURN THE FIRST ROW OF THE
// RECORDSET
return $query->row_array();
}
}