Save formatted xml - simplexml

Hi Can someone please tell me why this inst saving to my XML file? it doesn't return any errors but doesn't write to the file either? It might be silly so I apologize in advance
<?php
$pastor = $_POST["Speaker"];
$title = $_POST["Title"];
$link = $_POST["podcasturl"];
$download = $_POST["Download"];
$podcastid = $_POST["Podcastid"];
$pubdate = $_POST ["Date"];
$filename = 'podcast.xml';
$xml = simplexml_load_file($filename);
$xml->addChild('title');
$xml->addChild('pastor', $pastor);
$xml->addChild('title', $title);
function saveXMLFormatted($xml, $filename) {
$savexml = new DOMDocument('1.0');
$savexml->preserveWhiteSpace = FALSE;
$savexml->loadXML($xml->asXML());
$savexml->formatOutput = TRUE;
file_put_contents($filename, $savexml->saveXML());
}

Related

force_download not working in codeigniter after writing

tried to auto download file after file created but its not working. file creating and appending data perfectly but download functionality not working
public function getexportfile(){
$this->load->dbutil();
// $this->load->database('DB_utility','dbutil');
//$this->load->helper('download_helper','download');
$this->load->helper('download_helper');
$this->load->helper('file');
error_reporting(0);
ini_set('display_errors', 0);
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
## Condition Start
$sWhere = " where condition"; // where conditon
$date = date('Y-m-d');
$filename = 'app/logs/othermarketplace_'.$date.'.csv';
$data = $this->PMP->export($sWhere); // calling model
$result = $this->dbutil->csv_from_result($data);
write_file($filename,$result); // file creating write data perfectly
$data = file_get_contents($filename);
//$path = file_get_contents(base_url().$filename);
//$name = "othermarketplace_'.$date.'.csv";
//force_download($nme, $pth);
force_download($filename,$data);
}
anything i missed?
From docs:
// Contents of photo.jpg will be automatically read
force_download('/path/to/photo.jpg', NULL);
//$data = file_get_contents($filename);
//$path = file_get_contents(base_url().$filename);
//$name = "othermarketplace_'.$date.'.csv";
//force_download($nme, $pth);
if (!is_file($filename)) {
exit('No file found.');
}
force_download($filename, NULL);
Note:
These lines might be doing something weird. Perhaps remove them as they aren't being used:
error_reporting(0);
ini_set('display_errors', 0);
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);

Laravel 5.2 File Upload always (/tmp) saved in DB

I Have my code below, it successfully move the file in the desired path. My problem is that in db it always saved like (/tmp/phpI9zJ1F) with random characters after tmp/. How do I solve this problem?
$quiz = Quiz::findOrFail($id);
$tmp = '';
if( $request->hasFile('survey_image') )
{
$extension = $request->file('survey_image')->getClientOriginalExtension();
do{
$rand = rand(11111,99999);
$imageName = $rand.'.'.$extension;
$tmp = $imageName;
$is_duplicate = Quiz::where('survey_image', $imageName)->count();
}
while( $is_duplicate!=0 );
$request['survey_image'] = (string)$tmp;
$quiz->survey_image_path = 'images/surveys/';
}
$request->file('survey_image')->move(
'images/surveys/' , $tmp);
$quiz->update($request->all());
You would need to use replace() or merge() in order to override the value in $request.
$request->replace(array('survey_image' => (string)$tmp));
You hadn't explained much about it, but I believe that this is a method to only update a image.
So, I would do something like that:
$quiz = Quiz::findOrFail($id);
$tmp = '';
if( $request->hasFile('survey_image') )
{
$extension = $request->file('survey_image')->getClientOriginalExtension();
do{
$rand = rand(11111,99999);
$imageName = $rand.'.'.$extension;
$tmp = $imageName;
$is_duplicate = Quiz::where('survey_image', $imageName)->count();
}
while( $is_duplicate!=0 );
$request['survey_image'] = (string)$tmp;
$quiz->survey_image_path = 'images/surveys/';
$request->file('survey_image')->move(
$quiz->survey_image_path , $tmp);
$quiz->update([
'survey_image' => '/'.$quiz->survey_image_path.$imageName,
]);
}
If you want to reuse your code, you can do something like: (Ps. Code not tested!)
/**
* Handle File Upload
* #param UploadedFile $file [File from request]
* #param string $path [Path where to save the file]
* #param string|boolean $current_file [An old file that you want to delete, before save the new one]
* #return string
*/
public function updateImage(UploadedFile $file, $path, $current_file = false)
{
//Set a random name to the file
$extension = $file->getClientOriginalExtension();
$fileName = str_random(32) . '.' . $extension;
//Delete an old file first
if ($current_file !== false)
if (File::exists($current_file))
File::delete($current_file);
//Move new file to the directory
$file->move($path, $fileName);
return '/'.$path.$imageName;
}
And where you want to save on database:
if( !$request->hasFile('survey_image') ) return false; //Or what you want
$quiz = Quiz::findOrFail($id);
$quiz->update([
'survey_image' => $this->updateImage(request('survey_image'), 'images/surveys/');
]);

Creating a Dynamic PDF and Saving it in a folder using Loop with MYSQL and Codeigniter

This is the public function with looping (Edited)
public function generate_salary_slip(){
$this->load->helper('file');
$this->load->library('m_pdf');
$data = [];
$slip = $this->Admin_Login_Model->generate_salary_slip();
//$html = array();
$html='';
if(!empty($slip)){
foreach($slip as $slip_list){
$id = $slip_list->emp_id;
$data['attendance'] = $this->Admin_Login_Model->get_slip_attendance($id);
$data['emp_profile'] = $this->Admin_Login_Model->get_emp_details($id);
$data['late_day'] = $this->Admin_Login_Model->get_late_days($id);
$data['half_days'] = $this->Admin_Login_Model->get_half_days($id);
$data['salary'] = $this->Admin_Login_Model->get_salary_slip($id);
$data['second_half'] = $this->Admin_Login_Model->get_half_Second_days($id);
//$html=$this->load->view('admin/payslip',$data);
$pdfFilePath = date("F_Y")."salary_slip.pdf";
$html .= $this->load->view('admin/payslip', $data, true);
}
$htmlval = $html;
//echo $html;exit;
$this->m_pdf->pdf->WriteHTML($htmlval);
$this->m_pdf->pdf->Output('salary_slips/'.$pdfFilePath, "F");
$this->session->set_flashdata('success','Salary slips saved in directory!');
redirect('admin_attendance_list');
}else{
$this->session->set_flashdata('message','Salary slips not available for this month!');
redirect('admin_attendance_list');
}
}
File is getting saved in the folder, but Only with the First Record. Also, Repeated loop is getting generated, but with same record.
How to create a HTML by loop.
PS: Using MPDF to generate PDF File
Any hep will be highly appreciated.
You need to take the render, writeHtml and Output calls outside the forech loop, there is you problem.
You could save all the data in a var and then render the HTML as you are doing now and then generate the PDF.
EDIT: Your code should look something like this (please read comments below):
$htmlData = array();
if(!empty($slip)){
foreach ($slip as $slip_list) {
//
$htmlData[] = $data;
}
$pdfFilePath = date("F_Y")."salary_slip.pdf";
$html = $this->load->view('admin/payslip', $htmlData, true);
$this->m_pdf->pdf->WriteHTML($html);
$this->m_pdf->pdf->Output('salary_slips/'.$pdfFilePath, "F");

how to export a table to csv format and save it on specified folder

i want to export a table to a csv file. that file should save to a folder in assets. here i am using force download.How can set path to the downloading csv file? And is there any other method to save the table as csv file to a specified folder.? pls help me ..
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$filename = "filename.csv";
$query = "SELECT * FROM cyber_details";
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
force_download($filename, $data);
I assume that you want to save CSV file on server end.
you can do as following
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$filename = "filename.csv";
$query = "SELECT * FROM cyber_details";
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
if ( ! write_file('./path/to/file/file_name.csv', $data))
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}
make sure the path you choose has enough permission to perform write operation.
try this, worked for me:
function createcsv(){
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$filename = "filename.csv";
$query = "SELECT * FROM YourTable";
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
force_download($filename, $data);
}
I also had come through the same issue.
Below code worked good enough for me.
$arry_to_csv[] = $data;
$output_file_name = 'path/to/Rejected.csv';
$temp_memory = fopen($output_file_name, 'w');
$csv_headers = array_keys($arry_to_csv[0]);
$delimiter = ",";
fputcsv($temp_memory, $csv_headers, $delimiter);
$csv = '';
foreach ($arry_to_csv as $line) {
$csv.= $line['first_name'] . ',' . $line['last_name'] . "\n";
}
fwrite($temp_memory, $csv);
fclose($temp_memory);

Upload image on laravel

add an image I can not load because it creates a new folder outside the public folder
$input = Input::all();
$polje = array('naslov' => 'required');
$provjera = Validator::make($input, $polje);
if($provjera->passes()){
$file = Input::file('file');
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('file')->move(base_path().'/images/vijesti', $filename);
$vijest = new Vijesti();
$vijest->naslov = $input['naslov'];
$vijest->slika = $uploadSuccess;
$vijest->tekst = $input['tekst'];
$vijest->tag = $input['tag'];
$vijest->kategorija_id = Input::get('kategorija_id');
$vijest->save();
return Redirect::to('admin/vijesti/dodaj')->withInput()->with('ok', 'Vijest je uspjesno dodata.');
}else {
return Redirect::to('admin/vijesti/dodaj')->with('no', 'Greska: morate pokusati ponovo, polje naslov i slika su obavezna polja.');
}
You are using the wrong function. Use public_path() instead of base_path().
$uploadSuccess = Input::file('file')->move(public_path().'/images/vijesti', $filename);
Hope that helps :)

Resources