failed to clone a script to a new server - php

i was trying to copy this script from one of my servers to another one
both of them are ubuntu 18 with php 5.6 on them
but i keep getting internal server error without changing anything
<?php
if(!isset($_POST['importSubmit'])){
header("Location: index.php");
}
date_default_timezone_set('UTC');
header('Content-Type: text/html; charset=utf-8');
$x = fopen($_FILES['file']['tmp_name'], 'r');
require('XLSXReader.php');
$xlsx = new XLSXReader($_FILES['file']['tmp_name']);
$sheetNames = $xlsx->getSheetNames();
foreach($sheetNames as $sheetName) {
$sheet = $xlsx->getSheet($sheetName);
array2Table($sheet->getData());
function array2Table($data) {
foreach($data as $row) {
echo "<tr>";
foreach($row as $cell) {
echo "<td>" . escape($cell) . "</td>";
}
echo "</tr>";
}
}
function escape($string) {
return htmlspecialchars($string, ENT_QUOTES);
}
?>

From what I can tell, you are using: https://github.com/gneustaetter/XLSXReader and this library is not installed in your new server. Install it, and you should probably be good.

Related

how can I download multiple PDF files using php

so I am making a web site that allows user to read from a xlsx file sheet and download all the data each in a separate pdf here is the code
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
// Load Composer's autoloader
require 'vendor/autoload.php';
$file_name="";
//index.php
$message = '';
require_once __DIR__.'/../src/SimpleXLSX.php';
echo '<h1>XLSX to HTML</h1>';
if (isset($_FILES['file'])) {
if ( $xlsx = SimpleXLSX::parse( $_FILES['file']['tmp_name'] ) ) {
$filen=$_FILES['file']['tmp_name'];
echo '<h2>'.$xlsx->sheetName($_POST['sh']-1).'</h2>';
echo '<table border=1>';
$dim = $xlsx->dimension();
$num_cols = $dim[0];
$num_rows = $dim[1];
foreach ( $xlsx->rows($_POST['sh']-1) as $k => $r ) {
// if ($k == 0) continue; // skip first row
echo '<tr>';
if ($k == 0) echo '<td>' .$r[ 0 ]. '</td>';
else
echo '<td>' .substr_replace($r[ 0 ],"",strripos($r[ 0 ]," ")). '</td>';
echo '<td>' .$r[ 1 ]. '</td>';
echo '<td>' .$r[ 2 ]. '</td>';
echo '<td>' .$r[ 4 ]. '</td>';
echo'<td>' . $r[ 5 ]. '</td>';
echo'<td>' . $r[ 7 ]. '</td>';
echo'<td>' .$r[ 8 ] . '</td>';
echo '</tr>';
if ($k != 0) // skip first row
{$date = substr_replace($r[0], "", strripos($r[0], " "));
$factname = $r[1];
$name = $r[2];
$email = $r[4];
$phone = $r[5];
$post = $r[7];
$pack = $r[8];
echo $name;
if ($pack == '90') $garanti = '30 jours';
else if ($pack == '190') $garanti = '6 mois';
else if ($pack == '290') $garanti = '12 mois';
else if ($pack == '390') $garanti = '2 ans';
else if ($pack == '490') $garanti = '3 ans';
else if ($pack == '590') $garanti = '5 ans';
sendmail();
echo'<td>telecharger</td>';}
// echo "telecharger";
}
echo '</table>';
echo '</tr>';
}
echo '</table>';
}
else {
echo SimpleXLSX::parseError();
}
if(isset($_POST['charge'])) {
if (isset($_FILES['file'])) {
if ($xlsx = SimpleXLSX::parse($_FILES['file']['tmp_name'])) {
foreach ($xlsx->rows($_POST['sh']-1) as $k => $r) {
if ($k == 0) continue; // skip first row
$date = substr_replace($r[0], "", strripos($r[0], " "));
$factname = $r[1];
$name = $r[2];
$email = $r[4];
$phone = $r[5];
$post = $r[7];
$pack = $r[8];
if ($pack == '90') $garanti = '30 jours';
else if ($pack == '190') $garanti = '6 mois';
else if ($pack == '290') $garanti = '12 mois';
else if ($pack == '390') $garanti = '2 ans';
else if ($pack == '490') $garanti = '3 ans';
else if ($pack == '590') $garanti = '5 ans';
sendmail();
echo "telecharger";
}
}
echo "telecharger";
}
}
echo '<h2>Upload form</h2>
<form method="post" enctype="multipart/form-data">
*.XLSX <input type="file" name="file" />
<input placeholder="sheet number" name="sh" type="number" required>
<input type="submit" value="Parse" />
</form>';
function sendmail()
{
global $name;
global $file_name;
$file_name="";
echo $file_name;
include('pdf.php');
$pdf = new Pdf();
$file_name = "ORDER-".$name . '.pdf';
$html_code = '<link rel="stylesheet" href="bootstrap.min.css">';
$html_code .= fetch_customer_data();
$pdf->load_html($html_code);
$pdf->render();
$file = $pdf->output();
file_put_contents($file_name, $file);
// $pdf->stream($file_name) ->
}
and this is the pdf.php file
<?php
//pdf.php
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
class Pdf extends Dompdf{
public function __construct(){
parent::__construct();
}
}
?>
I want to download all the pdfs at the same time but it only downloads the first one and shows me this error
( ! ) Fatal error: Cannot declare class Pdf, because the name is already in use in C:\wamp64\www\vucrm\xl\simplexlsx-master\examples\pdf.php on line 0
I tried to add exit() at the end of sendmail function but this only download the first and shows no other data or errors
can anyone help thanks in advance
You need to use require_once at the top of your script, don't use include inside the function.
// Require this at the top of your file
require_once('pdf.php');
The issue is each time you call the function, it includes the PDF class again and it can only be declared once.
Downloadable PDF files in html link!
To Download PDF from HTML link using PHP with the help of header() function in php.
The header() function is used to send a raw HTTP header.
Sometimes it wants the user to be prompted to save the data such as generated PDF.
Syntax:
http response headers to download any application
header("Content-Type: application/octet-stream");
http response headers to set composition and file to download
header('Content-Disposition: attachment; filename="downloaded.pdf"');
The length of the requested file need to download
header("Content-Length: " . filesize("download.pdf"));
Reads a file and writes it to the output buffer.
readfile('original.pdf');
PHP codes
$file = $_GET["file"] .".pdf";
// We will be outputting a PDF
header('Content-Type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="gfgpdf.pdf"');
$imagpdf = file_put_contents($image, file_get_contents($file));
echo $imagepdf;
HTML codes
<!DOCTYPE html>
<html>
<head>
<title>Download PDF using PHP from HTML Link</title>
</head>
<body>
<center>
<h2 style="color:green;">Welcome To GFG</h2>
<p><b>Click below to download PDF</b>
</p>
Download PDF Now</center>
</body>
</html>
Note: Remember that HTTP header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file or from PHP.
Example 1: Save below HTML file as htmllinkpdf.html and save PHP file as downloadpdf.php
Above example to illustrate concept of downloading PDF file using HTML link.
Downloading file appears to be PDF format but without any content which shows error on opening in any application.
See more here
Here is another simple solution in for loop

How to call a function in PHP

I have been following some courses on how to create a session object, which has worked out fine, If I place the complete code onto a PHP file, all works great!
What I would like to do is place this in another module (PHP file) and just use one line (or equivalent) to do this, such as GetSessiondata();
<?php
$SqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");
$UserDataSet = $SqlCon->GetUserList("SELECT * FROM Users");
echo "<br /><br />";
echo "<br /><br />";
if ($UserDataSet)
{
echo "<table>" . "<thead>" ;
echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";
foreach($UserDataSet as $data)
{
echo "<td>" .$data->GetUsrName()."</td>" ;
echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;
}
echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
echo "</tbody>" . "</table>" ;
}
else
echo "Nothing Found in DB!";
?>
You need to "require" your file, where you want to use it.
Here an example
Working with classes:
Whatever.php
class Whatever {
public function __construct() {
// Ever when the code is instantiated, this will be called too!
}
public function myMethod() {
echo 'hello';
}
}
index.php
require_once('./Whatever.php');
$whatever = new Whatever();
$whatever->myMethod();
Without classes:
functions.php:
function whatever(){ echo 'hello'; }
index.php:
require_once('./functions.php');
whatever();
Read more:
Require: http://php.net/manual/es/function.require.php
Require_once: http://php.net/manual/es/function.require-once.php
My advise is to split this refactoring process into 2 steps:
1.Wrap your code into function:
function someFunctionName() {
$SqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");
$UserDataSet = $SqlCon->GetUserList("SELECT * FROM Users");
echo "<br /><br />";
echo "<br /><br />";
if ($UserDataSet)
{
echo "<table>" . "<thead>" ;
echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";
foreach($UserDataSet as $data)
{
echo "<td>" .$data->GetUsrName()."</td>" ;
echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;
}
echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
echo "</tbody>" . "</table>" ;
}
else
echo "Nothing Found in DB!";
}
// and call your function
someFunctionName();
2.Create another file, let's say functions.php, in the same dir and move function into it. Now you can require this file inside your php page:
require_once 'functions.php';
// and call your function
someFunctionName();
I think you are looking for include
Save your file, and then you can include it in another file as:
include 'my_file.php';
You can also use:
include_once
require
require_once
Read the documentation for further explanations or see this question

Putting arrays in a table using PHP

I'm a beginner in PHP and working on an API project which has to get results from a server I connected via API.
I am getting the results, but I would like to have them well arranged in a simple table.
This is what I am getting:
I used the following code:
$api = new SplynxAPI($api_url, $key, $secret);
$locationsApiUrl = "admin/administration/locations";
echo "<pre>";
echo "List locations\n";
$result = $api->api_call_get($locationsApiUrl);
echo "Result: ";
if ($result) {
echo "Ok!\n";
print_r($api->response);
} else {
echo "Fail! Error code: $api->response_code\n";
print_r($api->response);
}
echo "\n-------------------------------------------------\n";
Kindly assist me on this one.
The following code will render your response in a HTML <table>.
$api = new SplynxAPI($api_url, $key, $secret);
$locationsApiUrl = "admin/administration/locations";
$result = $api->api_call_get($locationsApiUrl);
if ($result) {
echo "<table>";
foreach($api->response as $row){
echo sprintf('<tr><td>%s</td><td>%s</td></tr>', $row['id'], $row['name']);
}
echo '</table>';
} else {
echo "Fail! Error code: $api->response_code\n";
print_r($api->response);
}
echo "\n-------------------------------------------------\n";
You are directly getting the response "$api->response" as an array. So simply use a loop and populate it in table as you like.
Ex:
foreach($api->response as $apiData) {
// Your code
}

Call function php uing jquery

Hello i want to call a function using jquery. I tried a lot of ways and I can't get it.
This my principal webpage.
I'am uploading a file csv and pressing 'Crear' button, it uplaod the file while call the function.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<SCRIPT type="text/javascript">
$(function (){
$('#btnxml').click(function (){
alert("aki");
$('#contenidos').load('csv.php');
});
});
</SCRIPT>
</head>
<body>
<form action="index.php" id="filecsv" method="post" enctype="multipart/form-data">
<input type="file" multiple="multiple" id="file" name="up_csv[]"/>
<input type="submit" value="Cargar" name="btnxml" id="btnxml" /><br />
</form>
<?php
global $archivocsv;
//tipos de archivos permitidos
$extensionxml = array('csv','txt');
//destino
$rutaupcsv = './csv/';
//multicargador de archivos
$vt=0;
for($i=0;$i<count($_FILES['up_csv']['size']);$i++){
for ($j=-1; $j<count($extensionxml); $j++) {
if (strripos($_FILES['up_csv']['name'][$i], $extensionxml[$j])!== false) {
$filename = 'lista';
$destino = $rutaupcsv.$_FILES['up_csv']['name'][$i];
$archivocsv = basename($_FILES['up_csv']['name'][$i]);
move_uploaded_file($_FILES['up_csv']['tmp_name'][$i],$destino);
$vt=$vt+1;
break;
}
$ns=1;
}
}
?>
<div id="contenidos"></div>
csv.php
<?php
echo '<html>';
echo '<head>';
echo '<meta content="text/html;charset=utf-8" http-equiv="Content-Type">';
echo '<meta content="utf-8" http-equiv="encoding">';
echo '</head>';
function makecsv() {
global $archivocsv;
$csv = './csv/' . $archivocsv;
$doc = new DOMDocument();
$row = 1;
$handle = fopen($csv, "r");
# Rows Counter
$csvxrow = file($csv);
$csvxrow[0] = chop($csvxrow[0]);
$anzdata = count($csvxrow);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
#Load Predefined XML Template
$xml2 = $xml;
$xmlruta = './Templates/';
$xml = $xmlruta.$data[1].'.xml';
$doc->load($xml);
$xp = new DOMXPath($doc);
for ($c=0; $c < $num; $c++) {
foreach($xp->query('/ROOT/HEADER/#KEY[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[0];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/#AUFNR[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[0];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/#MATNR[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[1];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/#GAMNG[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[2];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1AFFLL/E1FVOL/#MGVRG[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[2];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/#GSTRS[. != ""]') as $attrib)
{
$attrib->nodeValue = $data[3];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/#GLTRS[. != ""]') as $attrib)
{
$fecha = new DateTime($data[3]);
$fecha->add(new DateInterval('P1M'));
$attrib->nodeValue = $fecha->format('Y-m-d');
}
}
$name = $data[0] .'-'. $data[1];
$doc->formatOutput = true;
echo $doc->saveXML();
$rutafinal = './XML/';
$doc->save($rutafinal.$name.'.xml');
}
fclose($handle);
echo $anzdata . " XML Creados" . "<br />";
return $data;
}
makecsv();
echo '</html>';
?>
I can't call the function.
it doesn't do anything when i try to call it.
EDIT: I think the problem is in my function. function edite
Javascript canĀ“t play with php directly because JS is client side (only in browser) and PHP is server side (only in browser). What you can do is a PHP file that has the code you want to invoke, and make JS call that page.
Separate the CVS code from the form and make JS call the new php with the CVS php functionality.
jQuery runs in the client's browser whereas your PHP is running on your web server. If you wish to call a PHP function from your jQuery code, your best option is to do so using AJAX.
You can find documentation for implementing an AJAX call in jQuery here: https://api.jquery.com/jQuery.ajax/
You'll need to print the actual hmtl to do the function.
PHP
<?php
print '<script> makecsv() </script>';
?>

.shtml and .php pages Includes Virtual and Running PHP Problems

I have a strange issue going on. It may be server related, but I don't know what to look for.
I have a .php page with a few Virtual Includes:
<?php virtual ("nav.shtml"); ?>
...throughout it. I have also a parser that is displaying XML data in a table form.
The parser works with the standard:
<?php include ('parser.php'); ?>
...however, if I have the Virtual above the include, the parser doesn't work. Or at least it will not "find the file" however, the file is there and it works ABOVE the virtual, displaying it fine...
For example, this works:
<?php include ('parser.php'); ?>
<?php virtual ('file.shtml'); ?>
This doesn't:
<?php virtual ('file.shtml'); ?>
<?php include ('parser.php'); ?>
Am I missing something here?
Here is the index.shtml page code:
<?php virtual ("nav.shtml"); ?>
<div id="sortabletable">
<table id="myTable" class="tablesorter">
<thead>
<tr>
<th>Subject</th>
<th>Committee</th>
<th>Witness</th>
<th>Date</th>
<th>Bill</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<?php include('parser.php'); ?>
</tbody>
</table>
</div>
<?php virtual ("footer.shtml"); ?>
Here is the parser code:
<?php
$xml_file = "test.xml";
$xml_subject_key = "*TESTIMONIES*CONGRESS*SUBJECT";
$xml_committee_key = "*TESTIMONIES*CONGRESS*COMMITTEE";
$xml_witness_key = "*TESTIMONIES*CONGRESS*WITNESS";
$xml_date_key = "*TESTIMONIES*CONGRESS*DATE";
$xml_bill_key = "*TESTIMONIES*CONGRESS*BILL";
$xml_link_key = "*TESTIMONIES*CONGRESS*LINK";
$congress_array = array();
$counter = 0;
class xml_story{
var $subject, $committee, $witness, $date, $bill, $link;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_subject_key, $xml_committee_key, $xml_witness_key, $xml_date_key, $xml_bill_key, $xml_link_key, $counter, $congress_array;
switch($current_tag){
case $xml_subject_key:
$congress_array[$counter]->subject = $data;
break;
case $xml_committee_key:
$congress_array[$counter]->committee = $data;
break;
case $xml_witness_key:
$congress_array[$counter]->witness = $data;
break;
case $xml_date_key:
$congress_array[$counter]->date = $data;
break;
case $xml_bill_key:
$congress_array[$counter]->bill = $data;
break;
case $xml_link_key:
$congress_array[$counter]->link = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
// A simple for loop that outputs our final data.
//echo sizeof($congress_array);
for($x=0; $x<count($congress_array); $x++){
echo "<tr><td scope='row'>" . $congress_array[$x]->subject . "</td>";
echo "<td>" . $congress_array[$x]->committee . "</td>";
echo "<td>" . $congress_array[$x]->witness . "</td>";
echo "<td>" . $congress_array[$x]->date . "</td>";
echo "<td>" . $congress_array[$x]->bill . "</td>";
echo '<td><img src="download-icon.png" width="20" height="20" alt="' . $congress_array[$x]->subject . '"/></td></tr>';
}
?>
"Will not find the file" ... which file? The XML ? The shtml?
Without seeing your code (cough) I can only guess that your .SHTML includes some executable PHP which is upsetting things ...
But without posted code all that we can do is guess
If you use Virtual and Includes in some cases you will need to split your includes code and run it at the top of your .shtml file. Then have the output includes where you need the output. Worked for my issue.
Thanks!

Categories