I'm building a website with MongoDB and Codeigniter where users can create articles (title + text) and after it (but before to submit), upload some pictures (images are not in the text).
I think i will use jquery.upload for it. But my question is how to do link between pictures and article (because img will be uploaded first), how to rename them ?, is there any good way to do this ?
jquery upload would work. and check if it is success then create new hidden input into the form and set its value as you wish to use.
While I've never attempted this feature, this is a hypothetical solution that popped in my head:
Create a unique string on your Create Article page, and insert it into your document as a hidden field. A random string of 6 characters or something would probably be sufficient.
When uploading the images, use that string as an identifier for what future post the images belong to. Use the string in the file name, path, DB entry, or whatever's useful for your scenario.
When the article itself is submitted, the unique string will allow it to be identified with the images, and you can work whatever magic you need to at that stage to fully commit everything together.
Keep in mind that you should have some sort of statistics and/or garbage collection regarding any images that are uploaded and then the related article is abandoned.
Add 'thumbnail' field in your table 'articles_table' . I removed the unnessary parts for simpler need.We will only use 'title' and 'thumbnail' as field.
View | create.php
<?php echo form_open_multipart('articles/insert'); ?>
Title <br>
<?php echo form_input('title','','id="title_input"'); ?><br>
Thumbnail<br>
<input type="file" name="file" size="20" />
<?php echo form_submit('Submit',"Submit"); ?>
<?php echo form_close(); ?>
Controller | articles.php
function insert()
{
//$this->articles_model->insert();
$this->articles_model->save();
redirect('articles/index');
}
Model | articles_model.php
function save($id=0)
{
$title=$this->input->post('title');
//Set the config
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png|PNG';
$config['max_size'] = '1100';
$config['max_width'] = '11024';
$config['max_height'] = '1768';
$config['overwrite'] = FALSE;
//Initialize
$this->upload->initialize($config);
//Upload file
if( ! $this->upload->do_upload("file"))
{
//echo the errors
echo $this->upload->display_errors();
}
//If the upload success
$thumbnail = $this->upload->file_name;
if($id<1)
{
$data=array(
'title'=>$title,
'thumbnail'=>$thumbnail
);
$this->db->insert('articles_table',$data);
}
}
Related
I have created a commenting system for my website. The problem I have is that, the old comments duplicate themselves anytime the webpage is refreshed or reloaded.
How can I stop this and only show original comments?
I have added the entire code here below:
<pre>
require 'data/connect.php';
if(isset($_POST['name'])&& isset($_POST['comment'])){
$name = trim($_POST['name']);
$comment = trim($_POST['comment']);
if(!empty($name) && !empty($comment)){
$insert = $connect->query("INSERT INTO
comments(name,comment)VALUES('$name','$comment')");
if($insert){
echo "Success";
}else{
echo "Sorry";
}
}
}
?>
</pre>
form here
<pre>
<?php
if(isset($_POST['name'])&& isset($_POST['comment'])){
$name = trim($_POST['name']);
$comment = trim($_POST['comment']);
if(!empty($name) && !empty($comment)){
$query = $connect->query("SELECT name,comment FROM comments WHERE name='$name' AND comment='$comment'");
while($row = $query->fetch_object()){
echo "<b>",$row->name,"</b><br/>",$row->comment;
}
}
}
?>
</pre>
Your help will be appreciated.
Send out a unique value (created with php's uniqid() for example) in a hidden field in the form. Every time you send the form to a browser, change the value. If you get the same value twice from the same browser, you know it was a double post.
You could also compute a hash of the posted information and compare that against hashes of what's already in the database, for speed, you could store the hash for each comment in the database.
You may also be able to restrict this at the database level, for example by using replace instead of insert (for mySQL).
i load data in my controller like
$name['name'] = "some text!";
$data['header'] = $this->load->view('header_view', $name, TRUE);
$this->load->view('myView', $data);
and in my view file: myView.php
echo $header;
how can i get data in $name ? thanks
Try:
$data['name'] = "some text!";
$data['header'] = $this->load->view('header_view', $data['name'], TRUE);
$this->load->view('myView', $data);
You can't because when it comes to myView, the header view is allready processed and so data is allready incoroporated into the output string.
The only way is to copy (merge) the contents of $name into $data.
You can get embed data by parsing the content of $data['header'] but it is a really bad solution.
To solve this issue you can generate header and footer along the content inside your controller like that:
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
Or you can use a template engine to include header/footer automaticly with the content template.
Infortunately, there is no buildin way to add an header easily in codeigniter. You'll have to come up with your own functions to do that in a helper or library (but you'll not be able to share data between header and content).
im making currently making my thesis about a record management of our university secretary.. in which all papers inside the office will be scanned and uploaded in the system.. i am using codeigniter..one of the feature in my system is to view the pdf file in other window of the browser. but my problem is, when i click the title. only blank page will be displayed in the other tab.. can you help me solve this one?? here is my code
controller:
function viewMinutesFile(){
if(isset($_GET['id'])){
$id = $_GET['id'];
$file = $this->minutes_model->getFile($id);
$fp= fopen($file->path, "r");
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$file->filename."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' .filesize($file->path));
ob_clean();
flush();
while (!feof($fp)){
$buff = fread($fp,1024);
print $buff;
}
exit;
}
}
code to open the file: this is my syntax to be clicked by the user so that pdf file will be open in the new tab
File
index.php/admin/viewMinutesFile?
id=" target="_tab">
try this one with a static url. no need any extra words for that.
Show My Pdf
New Update
if its work for you then fetch pdf name from database and put the name in the view like
Show My Pdf
now in the controller
$this->load->helper('download');
if($this->uri->segment(3))
{
$data = file_get_contents('./file_path/'.$this->uri->segment(3));
}
$name = $this->uri->segment(3);
force_download($name, $data);
well, you could add a link to file with target="_blank", like
<a href="<?php echo base_url(). 'your_controller/viewMinutesFile'; ?>" target="_blank">
View Pdf
</a>
and in controller function:
function viewMinutesFile(){
....
$file = $this->minutes_model->getFile($id);
$this->output
->set_content_type('application/pdf')
->set_output(file_get_contents($your_pdf_file));
}
you can try this on your view :
Filename
and on your controller, you can try this, because this is works for me :
function viewfile(){
$fname = $this->uri->segment(3);
$tofile= realpath("uploaddir/".$fname);
header('Content-Type: application/pdf');
readfile($tofile);
}
hope this might help you...
Just create a link to a blank page and use this code in your controller:
public function myPdfPage(){
$url = base_url('assets/your.pdf');
$html = '<iframe src="'.$url.'" style="border:none; width: 100%; height: 100%"></iframe>';
echo $html;
}
Enjoy!
There is no any problem with your code you can open easily on next tab, like other pages only difference you have to change header description and it is make sure on your browser pdf reader add-ons are available, otherwise it will give you option to download.
You may just follow this.
<?php echo form_open_multipart('your_controller/your_function','target="_blank"') ;?>
//other input fields
<?php form_close();?>
I have been successful in using the File Uploading Library to upload my files on the server (mostly images).
My problem is when I need to update that specific item, I need to browse for photo again. Is there a way I can just attach the image uploaded already in the server to my upload button? I am thinking to make a media page like that of the wordpress but is there any starting point that anyone knows who can point me to? Thanks so much!
You can use a hidden field...check this example
View
<?php echo form_upload("vLogo", $campaign->vLogo , "id='vLogo'"); ?>
<?php echo form_hidden("vLogo_old", $campaign->vLogo , "id='vLogo_old'"); ?>
Controller
(Edit function)
//The way you do when you click on browse and select a file
if(isset($_FILES['vLogo']['tmp_name']) && !empty($_FILES['vLogo']['tmp_name']))
{
$this->load->library('upload', $config);
// Initialaizing Logo upload
$this->upload->initialize($config);
if ( !$this->upload->do_upload('vLogo')){
$logo_error = array('vLogo' => $this->upload->display_errors());
}
else{
$data1 = array('upload_data' => $this->upload->data());
}
}
else
{
//The browse filed is empty..So hidden input is taken
$data1['upload_data']['file_name'] = $post['vLogo_old'];
}
I'm looking for a simple extension that allows via admin console to load some background pics and display them on the frontend per user session (not on refresh). I don't think anything is out there so some pointers on how to start building one would be great too. Thanks!
Here is a solution for you.
You can upload images through Magento admin into media storage. Lets say you created a subdir "backgrounds" in there and uploaded various images there. Then all you need to do is add the following code into app/design/frontend/[your-interface]/[your-theme]/template/page/html/header.php
<?php
if(!$background = Mage::getSingleton('core/session')->getBackground()){
$img_arr = array();
if($handle = opendir(Mage::getBaseDir('base').'/media/backgrounds/')){
while(false !== ($entry = readdir($handle))){
if(!preg_match('/^\.+$/', $entry)){
$img_arr[] = $entry;
}
}
closedir($handle);
}
if($img_cnt = count($img_arr)){
$background = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK).$img_arr[rand(0, $img_cnt-1)]
Mage::getSingleton('core/session')->setBackground($background);
}
}
?>
<?php if(isset($background) && $background) : ?>
<style> background:url(<?php echo $background; ?>); </style>
<?php endif; ?>
Did not tested it live. Let me know.