Cant Get data from explode function - explode

I tried to echo a row from html data but it shows blank page . Please help.
<?php
$url_data = file_get_contents('http://www.nowgoal.com/analysis/1374177.html');
$rows = explode("\n",$url_data);
echo $rows[1];?>

Try this code
$f = fopen ("file.html", "r");
$ln = 0;
while ($line= fgets ($f)) {
if ($line===FALSE) print ("FALSE\n");
else $rows[$ln] = htmlspecialchars($line);
$ln++;
}
print $rows[1];
fclose ($f);

Related

How to display uploaded csv file into tablular format in drupal 7

I have created a form in drupal 7 and it has a field to upload file (csv file only ) now how to display uploaded csv file into table on form submit ?
Not sure about theme function, but you can do it on your own.
I.e. use:
http://php.net/manual/en/function.file-get-contents.php
To read the file and then:
http://php.net/manual/en/function.str-getcsv.php
to parse CSV.
Or, maybe:
http://php.net/manual/en/function.fgetcsv.php
to read row by row. Anyway, you'll end up with looping trough rows so just print values the way you want, add markup around the values...
After go through various code exercises i have come up with solution which is as follows :
`function display_table($filename, $head=false) {
$handle = fopen($filename, "r");
$all_rows = array();
$header = null;
while ($row = fgetcsv($handle)) {
if ($header === null) {
$header = $row;
continue;
}
$all_rows[] = array_combine($header, $row);
}
$table = theme('table', array('header' => $header, 'rows' => $all_rows));
return $table;
}`
Hope it would be helpful to others as well !

htmlspecialchars($_SERVER['PHP_SELF'])

if($_POST) {
$mysql_pass = '';
$connect = #mysql_connect($_POST['dbHost'], $_POST['dbUser'], $mysql_pass);
if($connect) {
if(#mysql_select_db($_POST['dbName'])) {
$dir = $_SERVER['SERVER_NAME']. $_SERVER['REQUEST_URI'];
$redirectUrl = str_replace("configForm.php","site/index", $dir);
print $redirectUrl; exit;
$dbConf = dirname(__FILE__).'/dbConfig/dbconf.php';
$handle = fopen($dbConf, 'w') or die('Cannot open file: '.$dbConf);
$data = '<?php $userName="'.$_POST['dbUser'].'";'."\n";
$data .= '$passWord=" ";'."\n";
$data .= '$dbName="'.$_POST['dbName'].'";'."\n";
$data .= '$host="'.$_POST['dbHost'].'"; ?>';
fwrite($handle, $data);
header('Location: site/index');
}
else {
$error = 'Could not select database.';
}
}
else
$error = 'Not connected';
}
when the code above was executed, my webpage just displayed "localhost/www-edusec/site/index". i do not know where i do it wrong. my form action is like below:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
The action attribute defaults to the current page anyway, so that piece of code is surely redundant.
The problem is PHP_SELF returns the path to the file too. Just get rid of the action line completely and it should work fine.
EDIT:
Also, the reason your script is cutting off half way, you have an exit; after it prints out the directory.

phpExcel reader long numbers to text

When reading a excel file, I have problems with numbers greater length:
I use:
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
for ($row = 1; $row <= $highestRow; $row++){
$obj = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
list($CODE, $NAME) = $obj[0];
echo $CODE;
}
And returns 1.6364698338384E+18
Is it possible to obtain 1636469833838380000 ?
I try with
$CODE = (string) floatval($CODE);
... but nothing...
You can change the cell format to text in the excel file and then try reading the values from it.
Hope this helps.

Perl NetSNMP extension with multiple devices

I have got a machine (Debian based) with some temperature sensors attached to it, and i would like to query them over snmp, from one script. I can work with one sensor ok, but i am struggling when i plug another one in.
What I am trying to do is loop through each device, and give each one an id, then use this ID as part of the OID, then give it a value.
I've never worked with snmp before, and my perl is not great so any help would be much appreciated. Below is my code:
#!/usr/bin/perl
use NetSNMP::agent (':all');
use NetSNMP::ASN qw(ASN_OCTET_STR ASN_INTEGER);
$BASE_OID=".1.3.6.1.4.1.41050";
$dev_id=1;
$string_value;
$integer_value;
sub pimon_handler {
my ($handler, $registration_info, $request_info, $requests) = #_;
my $request;
my $oid_key;
for($request = $requests; $request; $request = $request->next()) {
$oid_key=$BASE_OID . '.' . $dev_id;
my $oid = $request->getOID();
if ($request_info->getMode() == MODE_GET) {
if ($oid == new NetSNMP::OID($oid_key . '.0')) {
$request->setValue(ASN_OCTET_STR, $string_value);
}
elsif ($oid == new NetSNMP::OID($oid_key . '.1')) {
$request->setValue(ASN_INTEGER, $integer_value);
}
} elsif ($request_info->getMode() == MODE_GETNEXT) {
if ($oid == new NetSNMP::OID($oid_key . '.0')) {
$request->setOID($oid_key . '.1');
$request->setValue(ASN_INTEGER, $integer_value);
}
elsif ($oid < new NetSNMP::OID($oid_key . '.0')) {
$request->setOID($oid_key . '.0');
$request->setValue(ASN_OCTET_STR, $string_value);
}
}
}
}
#location of where we are going to find the 1wire devices
#sensors = `cat /sys/bus/w1/devices/w1_bus_master1/w1_master_slaves`;
chomp(#sensors);
#loop through the sensors we find
foreach $line(#sensors) {
#work out the temp we have got. Need to change this for other sensor types
$output = `cat /sys/bus/w1/devices/$line/w1_slave`;
$output =~ /t=(?<temp>\d+)/;
$integer_value = sprintf "%.0f",$+{temp} / 1000;
$string_value = $line;
my $agent = new NetSNMP::agent();
$agent->register("Pimon$looptest", $BASE_OID . '.' . $dev_id,
\&pimon_handler);
print "Dev $dev_id temp $line temp is $integer_value\n";
$dev_id ++;
}
Are you getting any errors or output?
I suspect that your problem lies in and around your reading the data file by shelling-out to cat instead of opening the file and looping over the linewise contents.
Try dumping the value of #sensors. if it is a single entry array, with the only element containing your entire file, then simply switch #sensors to be scalar. then split $sensors into an array and loop over that.
my $sensors = `read something`
chomp $sensors;
my #sensors = split(/\n/, $sensors);
foreach $line (#sensors) {
...

PHP write file... need help

<?php
$title = $_POST['title'];
$filename = $title , ".php";
$fh = fopen($filename, 'w') or die ("can't open file");
$stringData = $title;
fwrite($fh, $stringData);
$stringData = $blog;
fwrite($fh, $stringData);
fclose($fh);
?>
This is only a sample. What is the correct code for that?
You are using the correct code there, what is the point?
Also note that you are using a comma rather than a dot to concatenate strings at:
$filename = $title , ".php";
In your example opening a file using POST is an insecure method, so don't even think about this kinda tricks :P
You can use file read and write in simple method
file_get_contents();
echo $fileData = file_get_contents('filename.txt');
file_put_contents();
$data= 'some data';
// Write the contents back to the file
file_put_contents("filename.txt", $data);