I want to delete multiple files from my directory, and for this I am using the following code
$x=array(".index.php",".code.html","about.txt");
foreach($x as $a)
unlink($a);
The wired thing with this code is that it sometime works and sometimes doesn't, and no errors.
Is there anything that I am missing?
Add some monitoring to your code, to see what happens:
foreach($x as $a) {
echo "File $a ";
if (file_exists($a)) {
if (is_file($a)) {
echo "is a regular file ";
} else {
echo "is not a regular file ";
}
if (is_link($a)) {
echo "is a symbolic link ";
}
if (is_readable($a)) {
echo " readable";
} else {
echo " NOT readable";
}
if (is_writeable($a)) {
echo " and writeable ";
} else {
echo " and NOT writeable ";
}
echo "owned by ";
echo posix_getpwuid(fileowner($a)) ['name'];
if (unlink($a)) {
echo "- was removed<br />\n";
} else {
echo "- was NOT removed<br />\n";
}
} else {
echo "doesn't exist<br />\n";
}
}
Also read this comment on the PHP manual page about unlinking files.
If you have to use a path for your file, convert it to a real path with the function realpath() - see https://php.net/manual/en/function.realpath.php
Related
I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>
I want to show only one time the heading of parts of speeches for example when user enter the word "Spell" all the parts of speeches and its meaning come every time .
The headings are repeating multiple times . i want to print only one time
There should be a heading Of Noun,Verb,Adjective etc..
like this
Noun.
Verb.
adjective.
this is my code
while($row=mysql_fetch_array($sql)){
echo "<div id='wordid' style='display:none'>".$row["wordid"]."</div>";
echo $count++." : ".$row['definition']. " ";
if($row['pos']=="n"){
echo "(Noun) <br/>";
}
if($row['pos']=="s"){
echo "(Subject) <br/>";
}
if($row['pos']=="p"){
echo "(Proverb) <br/>";
}
if($row['pos']=="a"){
echo "(Adjective) <br/>";
}
if($row['pos']=="v"){
echo "(Verb) <br/>";
}
}
You can keep track of what the last wordid was and only echo the header if it changed.
// initialize to avoid notices
$lastWordId = '';
while($row=mysql_fetch_array($sql)){
if ($lastWordId != $row['wordid']) {
echo "<div id='wordid' style='display:none'>".
$row["wordid"].
"</div>";
$lastWordId = $row['wordid'];
}
echo $count++." : ".$row['definition']. " ";
if($row['pos']=="n"){
echo "(Noun) <br/>";
}
if($row['pos']=="s"){
echo "(Subject) <br/>";
}
if($row['pos']=="p"){
echo "(Proverb) <br/>";
}
if($row['pos']=="a"){
echo "(Adjective) <br/>";
}
if($row['pos']=="v"){
echo "(Verb) <br/>";
}
}
I'm currently making a droplist but in the droplist let's say I only want to include only .txt extension files so any other extensions like .php .jpg or any other extensions will not be in in the droplist. How can I do that as simple as possible?
Another question is I want to make a warning IF the folder does not have any .txt extension files an error message will show. So even if there are other .jpg .php or any other files inside as long as there's no .txt file in the folder a warning will show.
Anyone able to give me a hand?
This is what I have done but it only shows a drop-list with no .txt at the end but it will still show other random files in the drop-list though.
if(!(is_dir("./aaa")))
{
die("Must create a folder first, sorry");
}
$lists = scandir("./aaa");
echo "<form action=\"./page2.php\" method=\"get\">";
echo "<select>";
foreach($lists as $list)
{
if(($list == ".") || ($list == ".."))
{
continue;
}
echo "<option value=\"";
echo basename($list,".txt");
echo "\">";
echo basename($list,".txt");
echo "</option>";
}
echo "</select>";
echo "</form>";
editted added the substr with $hasTxt
<?php
if(!(is_dir("./aaa")))
{
die("Must create a <strong>aaa</strong> folder first, sorry");
}
echo "<form action=\"./page2.php\" method=\"get\">";
echo "<select name=\"aaa\">";
$aaa_files = scandir("./aaa");
$hastxt = false;
foreach($aaa_files as $file_list)
{
if(($file_list == ".") || ($file_list == ".."))
{
continue;
}
if(strlen($file_list)>4 && strtolower(substr($file_list, -4))!='.txt')
{
continue;
}
else
{
$hastxt = true;
echo "<option value=\"";
echo basename($file_list,".txt");
echo "\">";
echo basename($file_list,".txt");
echo "</option>";
}
}
echo "</select>";
echo "<br/><input type=\"submit\">";
echo "</form>";
if($hastxt == false)
{
echo "Must create text files first, sorry";
die();
}
?>
This is what happens for the script that I have now if the folder does not have any txt files.
This is what I actually want if there's no txt file but of course without the arrow
For the first part, just like you continue on directories . and .., you can continue on non-text files:
if(strlen($list)>4 && strtolower(substr($list, -4))!='.txt') continue;
For the warning part, put a flag before the foreach
$hasTxt = false;
And set it to true whenever you get input you don't ignore (ie. after the if(unwanted) continue;)
$hasTxt = true;
Finally, after the foreach check the value of $hasTxt and use it as you prefer.
You could use PHP's substr() function to test the filenames:
if(substr($filename, -3) == 'txt') {
// show file
}
See here: http://php.net/manual/en/function.substr.php
Try this , Hope it will work you
<?php
if(!(is_dir("./aaa")))
{
die("Must create a folder first, sorry");
}
$lists = scandir("./aaa");
$i =0;
foreach($lists as $list)
{
if (strstr($list, '.txt')) {
$i++;
}
}
if($i == 0){
die("the folder does not have any .txt extension files");
}
echo "<form action=\"./page2.php\" method=\"get\">";
echo "<select>";
foreach($lists as $list)
{
if (strstr($list, '.txt')) {
echo "<option value=\"".substr($list,0, -4)."\">".substr($list, 0,-4)." </option>";
}
}
echo "</select>";
echo "</form>";
?>
this is by Far the weirdest thing i have ever seen and i am completely confused. please someone help me with this.
$variable=array();
$count=0;
// now im am going to loop through a resource that i made
while(!feof($job))
{
$data=fgets($job);
// i am search for different things below. search for name, date, employer
// i am using regex to search btw
// presume object in class works fine, and they do.
if(search for eg name in $data, storing in $variable[$count].first($match))
// the problem is at this point i will have access to
// $variable[$count].getFirst(returns value set by first) which was set above;
if(search for eg Employer in $data, storing in variable[$count].next($match))
// i will have access here as well
// $variable[$count].getFirst(returns value set by first) which was set above
if(search for 3rd search in $data, storing in variable[$count].name($match))
// down here after the second if i am not able to see any of my variables set more than 2 if statements ago????
// $variable[$count].getFirst(does not returns the value set by first()) which was set above
if(search for 4th search in $data, storing in variable[$count].foo($match))
// check if everything is set then count++;
}
Now each one of these methods are completely dependent from the next but after 2 if statements. I am just not able to access $variable[count]->getfirst()
the answer is null;
edited
this is the actual code
require "functions/decodeEncodedUrl.php";
require "objects/jobObject.php";
$url=decodeEncodedUrl();
$profile=array();
$companies=0;
$url_search='http://www.jobbank.gc.ca/';
$startReading=0;
$job=fopen($url['url'], 'r')or die("JobBanks is failing to respond.<br>Please Try again Later");
while(!feof($job))
{
set_time_limit(500);
$profile[$companies]= new jobProfile();
$trash=fgets($job);
if(!$startReading)
{
if(preg_match('~RepeaterSearchResults_hypJobItem_[0-9]+~',$trash,$matches))
{
$startReading=true;
}
}
if($startReading)
{
$data=$trash;
if(preg_match("~href=\".*\"~",$data,$matches))
{
$temp=preg_replace("~href=~",'',$matches[0]);
$temp=preg_replace("~\"~",'',$temp);
$profile[$companies]->setLink($url_search.$temp);
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~>[A-Za-z-, ]+\(~",$data,$matches))
{
$temp=preg_replace("~>|\(~",'',$matches[0]);
$profile[$companies]->setPosition(ucfirst($temp));
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~# *[0-9]+~",$data,$matches))
{
$profile[$companies]->setOrderNum(preg_replace("~#| ~",'',$matches[0]));
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~Employer:</strong>.*~",$data,$matches))
{
$temp=preg_replace("~Employer:</strong> ~",'',$matches[0]);
$temp=preg_replace("~<br.*~",'',$temp);
$temp=ucfirst($temp);
$profile[$companies]->setEmployer($temp);
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~[$][0-9]+.*~",$data,$matches))
{
$temp=preg_replace("~/.*~",'',$matches[0]);
$profile[$companies]->setSalary(preg_replace("~[$]~","$ ",$temp));
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~[$][0-9]+.*~",$data,$matches))
{
$temp=preg_replace('~[$A-Za-z0-9. ]*[/] ?~','',$matches[0]);
$profile[$companies]->setRate(preg_replace('~<.*~','',$temp));
var_dump($profile[$companies]);
echo "<br>";
echo "<br>";
}
if(preg_match("~Location:.*~",$data,$matches))
{
$temp=preg_replace('~.*;~','',$matches[0]);
$temp=preg_replace('~^ |,~','',$temp);
$profile[$companies]->setCity(ucfirst($temp));
//echo ucfirst($temp)."<br>";
}
if(preg_match("~Location[:<>/\,A-Za-z ]*~",$data,$matches))
{
$profile[$companies]->setProvince($matches[0]);
//echo " ".$matches[0]."<br>\n";
//echo $profile[$companies]->getLocation()."\n<br>";
}
if(preg_match("~[0-9]{4}/[0-9]{2}/[0-9]{1,2}~",$data,$matches))
{
echo $profile[$companies]->displayHTML();
$profile[$companies]->setDate($matches[0]);
if($profile[$companies]->allDataSet())
{
//echo "data was set"."<br>";
$startReading=false;
$companies++;
}
else
{
$startReading=false;
$companies++;
echo "Data was Not set";
}
}
}
}
fclose($job);
everything works except the $profile[number] doesn't store anything in it at all after the 3 rd if statement when the variable is stored.
If
{
//Profile[number] info stored
}
if
{
//Profile[number] info available
}
if
{
//profile[number] info available
}
if
{
//profile[number] info is gone
}
variable[$count].next($match)
the .next() moves the internal pointer to the next element in the array.
Currently, I have this code:
<?php
if (isset($_GET['s'])) {
$itemid = $_GET['s'];
$search = "$itemid";
$query = ucwords($search);
echo "<title>Results for $query</title>";
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if ($itemid == "") {
echo "Please fill out the form.";
} else {
echo '<div id="content">';
$string = explode('<br>', $string);
foreach ($string as $row) {
preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
if (preg_match("/$query/i", "$matches[1]")) {
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
echo '</div>';
}
} else {
echo "Item does not exist!";
}
?>
If "$matches[1]" has nothing in it, I want my code it to echo "Item does not exist!" How do I do this though? Please help!
I've tried something like if ($matches[1]=="") { echo "Item does not exist!"; } before, but it didn't work. This is what I got:
http://img685.imageshack.us/img685/998/28990b2c12d0423292d3574.png
See works fine right? Look what happens if $matches[1] DOES exist:
http://img528.imageshack.us/img528/3690/71472c9de6ec49118ee8d48.png
It still comes up! How can I make my code so it only echos the error if there is nothing for $matches[1]? PLEASE HELP ME!
If you are wondering, this is my code when I added the if ($matches[1]=="") { echo "Item does not exist!"; } in:
<?php
if (isset($_GET['s'])) {
$itemid = $_GET['s'];
$search = "$itemid";
$query = ucwords($search);
echo "<title>Results for $query</title>";
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if ($itemid == "") {
echo "Please fill out the form.";
} else {
echo '<div id="content">';
$string = explode('<br>', $string);
foreach ($string as $row) {
preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
if (preg_match("/$query/i", "$matches[1]")) {
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
echo '</div>';
if ($matches[1] == "") {
echo "Item does not exist!";
}
}
} else {
echo "Item does not exist!";
}
?>
Any help to my question would be VERY HIGHLY APPRECIATED!
Look at PHP's comparison operators
if (empty($matches[1])) { echo "Item does not exist!"; }
else {
echo "Item does not exist!";
}
This "else" is in regards to isset($_GET['s']). Is there a variable called s in your query string? If there isn't, then it's normal that you get the message.
Maybe you put that else block in the wrong place?