whats wrong with my php script - php

hello to all at stackoverflow. (my first post) : )
i wrote this script
$file = glob("*.php"); // list all .php files
$findfile = array_search("index.php", $file); // search array for index.php
unset($file[$findfile]); // remove index.php from array
sort($file, SORT_NUMERIC); // sort array
foreach($file as $file){ include_once($file);} // include files in page
the files are 1.php,2.php,3.php etc
everytime i run it the files get included at the top of the page.
i need the files in the middle,
what am i doing wrong.
Thats the whole page as it looks for now
<?php
if(isset($_POST['ta'])){
$ta = $_POST['ta'];
if($ta != "" && strlen($ta) > 1 ){
$ta = preg_replace('#[^a-z0-9 !?.,]#i', '', $ta);
$usertextinput = '<p class="important">'.$ta.'</p>';
$pho = count(glob('*.php'));
$username_file = ($pho + 1) . ".php";
$createuser = fopen($username_file, 'w');
fwrite($createuser, $usertextinput);
header("location: index.php#bottom");}}
$userpage = '<p class="important"><span>Posts\'s:</span><br />
[username] Has 1 post's To Date.</p>';?>
$file = glob("*.php");
$findfile = array_search("index.php", $file);
unset($file[$findfile]);
sort($file, SORT_NUMERIC);
foreach($file as $file){ include_once($file);}
$userpage .= '<form name="text" method="post" action="">
<textarea name="ta" placeholder=" Enter Your Comment\'s Here ">
</textarea><br />
<a name="bottom"></a>
<p class="sub"><input type="submit" value="Post To Page" /></p>';
?>
im building a post wall (no database)

Give this a go. It will have the form's input on top,
then the Posts's: [username] Has 1 post's To Date. followed by the included files.
It's basically a placement issue.
<?php
if(isset($_POST['ta'])){
$ta = $_POST['ta'];
if($ta != "" && strlen($ta) > 1 ){
$ta = preg_replace('#[^a-z0-9 !?.,]#i', '', $ta);
$usertextinput = '<p class="important">'.$ta.'</p>';
$pho = count(glob('*.php'));
$username_file = ($pho + 1) . ".php";
$createuser = fopen($username_file, 'w');
fwrite($createuser, $usertextinput);
header("location: index.php#bottom");
}
}
$userpage = '<form name="text" method="post" action="">
<textarea name="ta" placeholder=" Enter Your Comment\'s Here ">
</textarea><br />
<a name="bottom"></a>
<p class="sub"><input type="submit" value="Post To Page" /></p>';
$userpage .= '<p class="important"><span>Posts\'s:</span><br />[username] Has 1 post\'s To Date.</p>';
echo $userpage;
$file = glob("*.php");
$findfile = array_search("index.php", $file);
unset($file[$findfile]);
sort($file, SORT_NUMERIC);
foreach($file as $file){
include_once($file);
}
?>

The reason why it isn't working, is because you are terminating the php code here:
[username] Has 1 post's To Date.</p>';?>
Remove the ?> and escape the single quote near post's; so the code block will look like this:
$userpage = '<p class="important"><span>Posts\'s:</span><br />
[username] Has 1 post\'s To Date.</p>';
$file = glob("*.php");
$findfile = array_search("index.php", $file);
unset($file[$findfile]);
sort($file, SORT_NUMERIC);
foreach($file as $file){ include_once($file);}

Related

function php to Plugin Wordpress

i have php code it's for reading excel file (.xlsx) and i want to make plugin for Wordpress ( just a simple plugin ). tested on localhost Wordpress it's work perfectly, but when i uploaded to my site it's not work. when i click submit button just appear blank page.
<form method="post" action="">
Number : <input type="text" name"number" /> </br>
<input type"submit">
</form>
function find(){
if (isset($_POST['number']) {
$number = $_POST["number"];
require_once ( plugin_dir_path(__FILE__). 'includes\classes\PHPExcel.php');
$tmpfname = ( plugin_dir_path(__FILE__). 'number.xlsx');
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
$excel_arr = $worksheet->toArray(null,true,true,true);
for ($row=1;$row <=$lastRow;$row++){
if ($excel_arr[$row]["A"] == $number ) {
echo $excel_arr[$row]["A"];
break;
}
}
}
}
add_shortcode('show_number', 'find');
Insert HTML form code inside "find()" function. So form show with shortcode "show_number" and and get result.
function find(){
if (isset($_POST['number']) {
$number = $_POST["number"];
require_once ( plugin_dir_path(__FILE__). 'includes\classes\PHPExcel.php');
$tmpfname = ( plugin_dir_path(__FILE__). 'number.xlsx');
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
$excel_arr = $worksheet->toArray(null,true,true,true);
for ($row=1;$row <=$lastRow;$row++){
if ($excel_arr[$row]["A"] == $number ) {
echo $excel_arr[$row]["A"];
break;
}
}
}
?>
<form method="post" action="">
Number : <input type="text" name"number" /> </br>
<input type"submit">
</form>
<?php
}
add_shortcode('show_number', 'find');
?>

Don't want to display all RecursiveDirectoryIterator files to user

I have a form which the user inputs a file name into. It iterates through all the directories successfully looking to match the users search input to the relevant pdf file.
When it finds the match it correctly echos 'it matches' and breaks out of the foreach loop. However, it also correctly states 'not a match' for all the files it finds before it matches the correct file. So I get a long list of 'not a match ' followed by 'it matches'.
If I echo " " instead of 'not a match' it works fine and doesn't display anything but I would like to tell the user only once that what they have inputted does not match. I am sure I have overlooked something basic but any assistance would be greatly appreciated on how to achieve this.
Here is the code I have.
<?php
if (isset($_POST['submit']) && !empty($_POST['target'])) {
$searchInput = $_POST['target'];
$it = new RecursiveDirectoryIterator("/myDirectory/");
foreach(new RecursiveIteratorIterator($it) as $file) {
$path_info = pathinfo($file);
$extension = $path_info['extension'];
if (strcmp("pdf", $extension) == 0) {
$lowerInput = strtolower($searchInput);
if (!empty($path_info)) {
$string = strtolower($path_info['filename']);
if(preg_match("~\b" . $lowerInput. "\b~", $string)) {
echo "it matches <br>";
break;
} else {
if (!preg_match("~\b" . $lowerInput . "\b~", $string)) {
echo "not a match <br>";
}
}
}
}
} //end foreach
} // end if submit pressed
?>
<html>
<head>
</head>
<body>
<h3>Search Files</h3>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="searchform">
Type the File You Require:<br><br>
<input id="target" type="text" name="target" required >
<br>
<input id="submit" type="submit" name="submit" value="Search" >
</form>
</body>
</html>
I've left out some of your code, just showing the parts that matter. Basically just set a variable and echo it once when the foreach completes.
$msg="Not a match <br>" ;
foreach(new RecursiveIteratorIterator($it) as $file) {
...
if(preg_match("~\b" . $lowerInput. "\b~", $string)) {
$msg = "it matches <br>" ;
break ;
}
// no need for an else clause, "not a match" is default finding
}// end of foreach
echo $msg ;

PHP - getting a single key from array, matching it with value, printing both

I have an array where the key is a filename and the value is the name of a fish. Right now, this loop prints out text for a randomly chosen fish, then it loops through the img directory and prints every image. How do I "link" it so that the image of the fish corrsponse with the name? Like, if it outputs Nice one! You caught a small-mouth bass! I would then want it to only output smbass.gif.
The php file is in the root, and the images folder is root/img/smbass.gif, e.g.
<p class="fishes">
<form action="fishing.php" method="post">
<?php
$fish = array('catfish.gif' => 'catfish', 'lmbass.gif' => 'large-mouth bass', 'smbass.gif' => 'small-mouth bass', 'shiner.gif' => 'shiner', 'perch.gif' => 'perch', 'pickerel.gif' => 'pickerel', 'minnow.gif' => 'minnow', 'sunfish.gif' => 'sunfish');
$rand = array_rand($fish);
//link file name (key) to value
echo "Nice one! You caught a " . $fish[$rand] . "!";
$files = scandir('img');
if ($files !== false) {
foreach($files as $f) {
if ($f == '..' || $f == '.' || substr($f, -strlen(".DS_Store")) === ".DS_Store") continue;
echo '<li class="fish_pic"><img src="img/'.$f.'" alt="'.$f.'" title="" class="fish"></li>'."\n";
}
}
?>
</p><!-- close class="fishes" -->
<p><input type="submit" class="btn btn-primary" value="Go Fishing!"></p>
</form>
Try something like this:
<form action="fishing.php" method="post">
<?php
$fish = array(
'catfish.gif' => 'catfish',
'lmbass.gif' => 'large-mouth bass',
'smbass.gif' => 'small-mouth bass',
'shiner.gif' => 'shiner',
'perch.gif' => 'perch',
'pickerel.gif' => 'pickerel',
'minnow.gif' => 'minnow',
'sunfish.gif' => 'sunfish'
);
$rand = array_rand($fish);
$sImageOut = "";
//check the image
// - yes, error supression is normally evil
// - and assigning variables in this way isn't exactly pretty
// ... but it works
if(list($iImgX, $iImgY, $iImgType, $sImgAttr) = #getimagesize(__DIR__ . '/img/' . $rand)) {
$sImageOut = "<div><img src=\"img/{$rand}\" alt=\"{$fish[$rand]}\" style=\"width: {$iImgX}px; height: {$iImgY}px; border: 1px solid #cccccc;\" /></div>\n";
}
//output
echo "<p>Nice one! You caught a {$fish[$rand]}!</p>\n{$sImageOut}";
?>
<p><input type="submit" class="btn btn-primary" value="Go Fishing!"></p>
</form>
Since the image name is the key and array_rand() returns the key ... $rand contains the image filename.
I'm using getimagesize() in there so that if, for some reason your image doesn't exist, it won't create "duff" output - not the tidiest code but it works as an example.

Using PHP to read msword and find specific elements e.g. numbering and bullets, underline sentence...?

I tried to use the PHP code from the following link
I tried the below code but it displays only text. How can i find specific element e.g. numbering and bullets, underline sentence...?:
Is there any trick by using chr() function to solve my problem?
<?php
function content($file){
$data_array = explode(chr(0x0D),fread(fopen($file, "r"), filesize($file)));
$data_text = "";
foreach($data_array as $data_line){
if (strpos($data_line, chr(0x00) !== false)||(strlen($data_line)==0))
{} else {if(chr(0) || chr(149)) {
$data_text .="<br /> ";
$data_text .= preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t#\/\_\(\)]/","",$data_line);
}
}
}
$data_text = str_replace('Responsibilities','<strong>'.Responsibilities.': </strong>',$data_text);
$data_text = str_replace('Requirements','<strong>'.Requirements.': </strong>',$data_text);
return $data_text;
}
$destination = str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']);
$destination.= "upload/";
$maxsize = 5120000;
if (isset($_GET['upload'])) {
if($_FILES['userfile']['name'] && $_FILES['userfile']['size'] < $maxsize) {
if(move_uploaded_file($_FILES['userfile']['tmp_name'], "$destination/".$_FILES['userfile']['name'])){
$file = $destination."/".$_FILES['userfile']['name'];
$data = content($file);
echo $data;
}
}
}else{
echo "<form enctype='multipart/form-data' method='post' action='index.php?upload'>
<input name='userfile' type='file'>
<input value='Upload' name='submit' type='submit'>
</form>";
}
?>

PHP or HTML Form Validation

I want to validate an HTML form in either HTML or PHP. By validate, I mean ensuring there is at least 1 character of text entered into each of the 2 textboxes. And either only alpha numeric characters entered, or have something that ensures that any punctuation doesn't end up with "/" before it.
At the moment my form is made up of 2 php pages (copied below), and then posted to a txt file.
I'm either after some (very basic) instructions on how to do it, or suggestions on my script below.
//HTML
<form style="" method="post" action="addtopic2.php">
Topic:<input name="topic" id="topicbox" maxlength="100" type="text"><br>
Outline: <textarea input wrap="nowrap" rows="10" cols="120"
name="outline"></textarea>
<br><input name="submit" value="Submit" type="submit">
</form>
//HTML
<?php
$t = "Topic:";
$o = "Outline:";
$topic = $_POST['topic'];
$outline = $_POST['outline'];
$data = "$t $topic | $o $outline |\n";
$fh = fopen("users.txt", "a");
fwrite($fh, $data);
fclose($fh);
?>
//ATTEMPTED TO USE FOLLOWING, BUT DOESNT SEEM TO WORK WHEN INSERTING INTO EITHER PAGES.
<?php
if($_POST['Submit'] == "submit")
{
$errorMessage = "";
if(empty($_POST['topic']))
{
$errorMessage .= "<li>A topic needs to be entered</li>";
}
if(empty($_POST['outline']))
{
$errorMessage .= "<li>An outline needs to be entered</li>";
}
$vartopic = $_POST['formtopic'];
$varoutline = $_POST['formoutline'];
if(!empty($errorMessage))
{
echo("<p>There was an error with your form:</p>\n");
echo("<ul>" . $errorMessage . "</ul>\n");
}
}
?>
<?php
if (isset($_POST['topic']) && isset($_POST['outline'])) {
$topic = trim($_POST['topic']);
$outline = trim($_POST['outline']);
}
else {
echo '<p>Fill the form</p>';
}
?>
Try
$file = "users.txt";
$errorMessage = array ();
if ($_POST ['Submit'] == "submit") {
if (empty ( $_POST ['topic'] )) {
$errorMessage [] = "A topic needs to be entered<";
}
if (empty ( $_POST ['outline'] )) {
$errorMessage [] = "An outline needs to be entered";
}
if (count ( $errorMessage ) == 0) {
$data = $_POST ['topic'] . "|" . $_POST ['outline'] . "\n";
$fh = fopen ( $file, "a" );
fwrite ( $fh, $data );
fclose ( $fh );
} else {
print ("<p>There was an error with your form:</p>\n") ;
print ("<ul>") ;
foreach ( $errorMessage as $error ) {
print "<li>" . $error . "<li>";
}
print ("</ul>") ;
}
}
// To Read Your File
$content = file ( $file );
print ("<p>File Details</p>\n") ;
foreach ( $content as $info ) {
list ( $topic, $outline ) = explode ( "|", $info );
print ("<ul>") ;
print "<li>Topic: " . $topic . "<li>";
print "<li>Outline:" . $outline . "<li>";
print ("</ul>") ;
}
Try this (single file) solution:
<form style="" method="post">
Topic:<input name="topic" id="topicbox" maxlength="100" type="text" value='<?= $_REQUEST['topic'] ?>><br>
Outline: <textarea input wrap="nowrap" rows="10" cols="120" name="outline"><?= $_REQUEST['outline'] ?></textarea>
<p/>
<input name="submit" value="Submit" type="submit">
</form>
<?php
if ($_REQUEST['submit'] !== 'submit')
{
exit;
}
if ($_REQUEST['topic'] == '')
{
echo "<p>Missing Topic.</p>\n";
exit;
}
if ($_REQUEST['outline'] == '')
{
echo "<p>Missing Outline.</p>\n";
exit;
}
$t = "Topic:";
$o = "Outline:";
$topic = $_POST['topic'];
$outline = $_POST['outline'];
$data = "$t $topic | $o $outline |\n";
$fh = fopen("users.txt", "a");
fwrite($fh, $data);
fclose($fh);
?>

Categories