I have a php file where i export data from database to excel using
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;Filename=cel.xls");
Now, i want to delete the first row of the file before it is exported. Can anyone help me on this?
you can try this sample code
if(isset($_POST['Upload'])) {
if(move_uploaded_file($_FILES["file"]["tmp_name"], "files/" . $_FILES["file"]["name"])) {
$file_target="files/" . $_FILES["file"]["name"];
$data->read($file_target);
// $x = 2 indicates second row of the excel sheet //
for($x = 2;$x<=count($data->sheets[0]["cells"]); $x++) {
$first_cell = $data->sheets[0]["cells"][$x][1];
$second_cell = $data->sheets[0]["cells"][$x][2];
$third_cell = $data->sheets[0]["cells"][$x][3];
}
}
}
Related
Part of what's being displayed:
PKd�wL��� ���/images/image_0.jpg��ex\K��1昙�3�133���;ffff����Ԇ�����w�̼߳�cw��딎�$��Z�������&QRTB�|>��E��M5���|�~b��)�o�3�� �>s���o�'���^B�O��V ����#���Q?S#(�� 6���v4���8����vvV�sy}#�_ �O��s�o���L�7Fv.F&�ol��WV.V��?�����g�W!�/w!�������K�oLL�1`���G�p�X���T�>C�#��L��)q���s��3�g�8�p�O�?
I'm trying to download images into zip file, this is what i've got so far:
if (count($headers->images) > 0) {
$counter = 0;
$zip = new ZipArchive();
$tmpFile = "tmpFolder/tmp" . strtotime("now") . ".zip";
$zip->open($tmpFile, ZipArchive::CREATE);
foreach($headers->images as $key => $images) $zip->addFromString("/images/image_" . $counter++ . ".jpg", file_get_contents($images));
$zip->close();
if (file_exists($tmpFile)) {
$fname = basename($_POST["titleZipFile"] . ".zip");
$size = filesize($tmpFile);
header("Content-Type: archive/zip");
header("Content-Disposition: attachment; filename=$fname");
header("Content-Length: " . $size);
ob_end_clean();
readfile($tmpFile);
//unlink($tmpFile);
echo "<div id='reportMessage'><p>You have successfully save images. Please go to your folder " . $_POST["titleZipFile"] . ".zip<p></div>";
}
}
if I remove the unlink($tmpFile); line then the tmp zip is actually generated and has the images inside the zip. However it's not actually showing the zip downloading in browser.
Does any one have any ideas why this could be happening?
Try to change the header for the content type to the following. I think the header is causing the browser problems with interpreting what it is supposed to do. Try the following type.
header('Content-type: application/zip');
I tried to read a .csv file using the PHPExcel library but it is giving me question marks instead of the Hebrew text.
The problem only occur when I tried to upload .csv files created from Windows Operating System. I tried the same for MacOS and Linux and the data seems to be fine. How can I solve the encoding issue from windows
public function actionUpload()
{
$params = $_FILES['uploadFile'];
if($params)
{
$data = array();
$model = new UploadForm();
$model->uploadFile = $_FILES['uploadFile'];
$file = UploadedFile::getInstanceByname('uploadFile');
$inputFileName = $model->getpath($file,$data);
// Read your Excel workbook
try
{
$inputFileType = \PHPExcel_IOFactory::identify($inputFileName['link']);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName['link']);
}
catch(Exception $e)
{
die('Error loading file "'.pathinfo($inputFileName['link'],PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$fileData = array();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++)
{
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
array_push($fileData,$rowData[0]);
// Insert row data array into your database of choice here
}
return $fileData;
}
}
The output array has "???? ???" in places where Hebrew text was present when .csv files created from Windows was uploaded.
So I'm fairly new to PHP and have been working on a web app that allows users to submit designs which are then saved as images (using html2canvas.js) to the server. I have the app working at the moment but it saves over any previous image with the same file name.
What I would like it to do is save with a consecutive number at the end of the file name so that previous designs are not overwritten. Working code below - the idea with the if else statement is that it checks for an existing file, if there isnt one, creates it (this code works) but if there is an existing file ... I dont know..
<?php
session_start();
$customerName = $_SESSION["design_name"];
//Get the base-64 string from data
$filteredData=substr($_POST['img_val_server'], strpos($_POST['img_val_server'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
//Check for previous user images
$fileName = '/customer-submits/design-' . $customerName . '.png';
if (file_exists($filename)) {
//THIS IS WHERE IS WANT THE FILE TO SAVE CONSECUTIVELY
} else {
//Save the image
file_put_contents('customer-submits/design-' . $customerName . '.png', $unencodedData);
$file = 'customer-submits/design-' . $customerName . '.png' ;
$fileName = basename($file);
$fileSize = filesize($file);
set_time_limit(0);
header("Pragma: public");
header("Expires: 0"); // Cache Options
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // Cache Options
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\" " . $fileName ."\"");
header("Content-length: " . $fileSize);
readfile($file);
include 'sendMail.php';
echo send_mail();
}
?>
Would obviously very much appreciate any help with this, as Im struggling to get my head around php!
This should add a new number to the end of the customers name if a similar customers name appears.
$num = 0;
while (file_exists($filename)==TRUE) {
$num++;
$file = 'customer-submits/design-' . $customerName $num . '.png' ;
}
I added ==TRUE into my previous code which makes it a Boolean and should take away the parsing error you got earlier.
Try this after "//Check for previous user images", replacing the if...else:
$indx = 1;
$filename = '/customer-submits/design-' . $customerName . $indx . '.png';
while (file_exists ($filename) {
$indx++;
$filename = '/customer-submits/design-' . $customerName . $indx . '.png';
}
I have a table with large amount of data in my sql database, i have to take a copy of .doc or .pdf format using PHP coding.
I have to download while clicking the Download Button in my Page.
In my table i have Student details with the following format
And i tried to Get in the Form of
But i'm getting the value with out Tabular form and S.no in the .Doc file
My Code in my Php File is,..
<?php
include "../../db_connection/db_connection.php";
include "../../config/config.php";
?>
<?PHP
$result =mysql_query("SELECT Stu_id,Name,Class,Total,Rank FROM Student");
$i = 0;
if (mysql_num_rows($result) > 0) {
for ($i = 0; $i < mysql_num_fields($result); $i++)
echo mysql_field_name($result, $i)." ";
print("\n\n\n");
}
$values = mysql_query("SELECT Stu_id,Name,Class,Total,Rank FROM Student");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]." ";
}
$csv_output .= "\n\n";
}
$filename = "Result"."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".docx");
header( "Content-disposition: filename=".$filename.".docx");
print $csv_output;
exit;
?>
My Output for this code is,..
Here where can i add the values to get S.No and how to form the Tabular form in the .Doc type..
Can i use the same code for generating PDF file for the Same Process?
Anybody suggest Best idea..!
I am currently exporting data from php to excel using the code as below:
include("dbconnect.php");
$query = $_POST['query'];
$result = odbc_exec($conn,$query);
$count = odbc_num_fields($result);
//Define Variable For ODBC
$data = "";
//Field Name Data
for ($i = 1; $i <= $count; $i++)
{
$data .= odbc_field_name($result, $i)."t";
}
$data .= "n";
//Row Data
while(odbc_fetch_row($result))
{
for ($j = 1; $j <= $count; $j++)
{
$data .= odbc_result($result, $j)."t";
}
$data .= "n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=ExcelFile.xls;");
header("Pragma: no-cache");
header("Expires: 0");
echo $data;
odbc_close($conn);
This all works fine, but the generated excel file has a sheetname of: ".xls]ExcelFile(1)" , and when you try to rename the sheet it causes an error in excel (unless you save the file first).
How can I define the sheetname in my php file?
Thanks in advance!
Have a nice weekend:-)
You're not actually creating an xls file, but a tab separated value file. Excel can read this, but it simply populates the data in the first worksheet. Because it's not a true xls file, you can't name the worksheet tabs in any way.
One option would be to change your code to use a library that writes true xls files, such as PHPExcel ( http://www.phpexcel.net )... you would then be able to define a name for the worksheet tab within your script.
By changing the
header("Content-type: application/octet-stream");
To
header("Content-type: application/vnd-ms-excel");
It is downloading without any errors and i am able to save that. the worksheet name is same as excel name.