I have to do a php website for college.
I have to create a password and a submit button. After the I have posted the password I should get text from a html file. What is the best to achieve that?
I tried:
foreach($files as $file2) {
if($_POST['submitPassword']){
if($file2 === '.' OR $file2 === '..' OR $file2 === 'thumbs.db' OR !is_dir($folder.'/'.$file2)) {continue;}
if(file_exists($folder.'/'.$file2.'/doubleindemnity.gif') AND file_exists($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm')) {
echo '<div class="Container">';
echo "<div class='image2'><img src='$folder/$file/doubleindemnity.gif'>";
$lines4 = file($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm');
$count = count($lines4);
for($a = 0;$a < $count;$a++) {
echo substr($lines4[$a],strlen($folder),strpos($lines4[$a], '.')-strlen($folder));
}
echo "</div>";
}
echo "</div>";
}
}
?>
Help would be highly appreciated:)
Cheers:)
You can easily include the contents of an html file through include(). For example:
include($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm');
It will call the entire file, and output it (since it's not a PHP file for execution)
Related
How to display all files and folders available in current directory php (FTP)
See Attached image example....
Tree View Image
define('SITE_URL',"http://yourdomain.com");
function listFolderFiles($dir){
$fileFolderList = scandir($dir);
echo '<ul>';
foreach($fileFolderList as $fileFolder){
if($fileFolder != '.' && $fileFolder != '..'){
if(!is_dir($dir.'/'.$fileFolder)){
echo '<li><a target="_blank" href="'.SITE_URL.'/'.ltrim($dir.'/'.$fileFolder,'./').'">'.$fileFolder.'</a>';
} else {
echo '<li>'.$fileFolder;
}
if(is_dir($dir.'/'.$fileFolder)) listFolderFiles($dir.'/'.$fileFolder);
echo '</li>';
}
}
echo '</ul>';
}
listFolderFiles('uploads/'); // function call with directory path (e.g. : upload/)
I am trying to a delete file selected using a dropdown. I am getting an error 'undefined index "file"' and permission denied warning on unlink.
<?php
$dirname = $_SERVER['DOCUMENT_ROOT']."myphp/project/userdir/neeraj/";
$dir = opendir($dirname);
echo '<form action="delete.php" method="get">';
echo '<select name="file2">';
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != ".."))
{
echo "<option value=".$file.">$file</option>";
}
}
echo '</select>';
echo '<input type="submit" value="Delete" class="submit" />';
echo '</form>';?>
<?php
$dirpath = $_SERVER['DOCUMENT_ROOT']."myphp/project/userdir/neeraj/";
$file_to_delete = $_GET['file2'];
if ( unlink ($dirpath.'/'.$file_to_delete) ) {
echo $file_to_delete . " deleted.";
} else {
echo "Error.";
}
?>
Note that your $dirpath ends with / and later you are trying to unlink:
unlink ($dirpath.'/'.$file_to_delete)
so it actualy gets something like this (note double slash):
[...] myphp/project/userdir/neeraj//$file_to_delete [...]
You should check file permissions to be sure that script has a right to unlink your files:
https://en.wikipedia.org/wiki/File_system_permissions
I guess you get the first error because you do not wrap your code to be executed only on form submit. Change the second section of your code to this:
<?php
if(isset($_GET['file2'])){ //i have posted my form
$dirpath = $_SERVER['DOCUMENT_ROOT']."myphp/project/userdir/neeraj/";
$file_to_delete = $_GET['file2'];
if ( unlink ($dirpath.'/'.$file_to_delete) ) {
echo $file_to_delete . " deleted.";
} else {
echo "Error.";
}
}
?>
The other error is related to the apache user to be able or not to access and delete in the directory where you store the files.
With another coder's help, I have PHP code that creates a photo gallery with auto-created thumbnail images of any image files in the directory. (It's open source, so anyone else is free to use and modify as they desire.)
I use it as a stock photo library and the file names include the keywords for that image. For example, one might be businessman-tie-suit-briefcase-office-meeting.jpg.
I've been trying to add a keyword search input that looks at the file names, but cannot figure out how to proceed. I've built keyword searches for a database, but this directory file name search is new to me.
Here's the relevant code of the page:
<?
$gallery = $_GET["gallery"];
$dir = $dir.$gallery."/";
//Put files into an array
// create a handler to the directory
$dirhandler = opendir($dir);
// read all the files from directory
$nofiles=0;
while ($file = readdir($dirhandler)) {
// if $file isn't this directory or its parent
//add to the $files array
if ($file != '.' && $file != '..')
{
$nofiles++;
$files[$nofiles]=$file;
}
}
//close the handler
closedir($dirhandler);
//Back button to appear at the top of each page to go to the previous directory if not on the main page
if ($gallery !="" or $keyword !="")
{
echo "<div><a href='javascript:history.go(-1)'><img src='images/up.png' border='0'></a></div>";
}
// BEGINNING ADD FOR KEYWORD SEARCH
// KEYWORD SEARCH BOX- create text box to search entire directory for that keyword and display page in grid pattern with results
?>
<div style='position:relative; left:10px;'>
<form action='index_search.php?keyword=$keyword' method='get' name='search'>
<input type='text' name='keyword' size='25'>
<input type='submit' value='Search Keyword'>
</form>
</div>
<?
//*************************************************************************//
// PERFORM KEYWORD SEARCH
if ($keyword !="")
{
echo "<div class='keytext'>Keyword search: <b>" . $keyword . "</b><br/></div>";
/*********************************************************************************************************/
/* ***** THIS IS WHERE THE SEARCH OF DIRECTORY FILES NEEDS TO OCCUR AND OUTPUT RESULTS AS $files */
/* get results where $file LIKE %$keyword%; */
/*********************************************************************************************************/
//Show images
foreach ($files as $file)
{
if ($file!="."&&$file!="..")
{
$extention = explode('.', $file);
if ($extention[1] != "")
{
echo "<div class='imgwrapper'>";
echo"<a class='fancybox' rel='group'' href='$dir$file' return false' title='$filename'>";
echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>";
echo"</a><br/>";
$file_name = current(explode('.', $file));
echo substr($file_name,0,21);
echo "</div>";
}
}
}
}
else { // starts the split from keyword or no keyword
//***********************************************************************//
// sort folder names alphabetically, ignore case
natcasesort($files);
//Show the folders
foreach ($files as $file){
if ($file!="."&&$file!="..")
{
sort($files); //Sorts the array (file names) alphabetically -- not the directory/folder names
$extention = explode('.', $file);
if ($extention[1] == "")
{
echo "<div class='imgwrapper'>";
echo "<a href='?gallery=$gallery/$file'>";
echo "<img src='images/folder.jpg' border='0'>";
echo "<div class='folder'>";
echo current(explode('.', $file));
echo "</a>";
echo "</div>";
echo "</div>";
}
}
}
?>
<div style="clear:both"></div>
<?
//Show images
foreach ($files as $file){
if ($file!="."&&$file!="..")
{
$extention = explode('.', $file);
if ($extention[1] != "")
{
echo "<div class='imgwrapper'>";
echo"<a class='fancybox' rel='group'' href='$dir$file' return false' title='$filename'>";
echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>";
echo"</a><br/>";
echo "<div class='title'>";
$file_name = current(explode('.', $file));
echo substr($file_name,0,125);
echo "</div>";
echo "</div>";
}
}
}
}
?>
I have the area commented out where I believe the search string would be executed, as the display code is already there. I've tried a few things that didn't work, so didn't bother listing any of it.
Any ideas if I'm going about this the wrong way?
Thanks in advance.
if ($extention[1] != "")
{
if(stripos($file, $keyword) !== false)
{...
}
}
See (stripos() manual)
I was able to get this resolved from some outside help.
if ($file!="."&&$file!="..")
{
sort($files); //Sorts the array (file names) alphabetically -- not the directory/folder names
$extention = explode('.', $file);
if ($extention[1] == "")
{
$dir = "pics/";
$dir = $dir.$file."/";
$dirhandler = opendir($dir);
// read all the files from directory
$nofiles=0;
$files2=array();
while ($file2 = readdir($dirhandler)) {
if ($file2 != '.' && $file2 != '..')
{
$nofiles++;
$files2[$nofiles]=$file2;
}
}
closedir($dirhandler);
sort($files2); //Sorts the array (file names) alphabetically -- not the directory/folder names
//Show images
foreach ($files2 as $file2){
if ($file2!="."&&$file2!="..")
{
$extention = explode('.', $file2);
if ($extention[1] != "" && stripos($file2,$keyword)!==false)
{
echo "<div class='imgwrapper'>";
echo"<a class='fancybox' rel='group'' href='$dir$file2' return false' title='$filename'>";
echo "<img src='timthumb.php?src=$dir$file2&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>";
echo"</a><br/>";
echo "<div class='title'>";
$file2_name = current(explode('.', $file2));
echo substr($file2_name,0,125);
echo "</div>";
echo "</div>";
}
}
}
}
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>";
?>
I want to include a php file in a div box whenever one generated through a form.php file
I have here some code allready given 12 boxes in two rows, what i want is growing every time one box and include a new generated php file into new generated div box automaticly, here is my code which need to be improved and to be repaired
<?php
$dir=opendir('.') or die ('Cannot open directory');
$file=readdir($dir);
for($j=1; $j < 13; $j++) :
print '<div style="float:left; width:100px">';
if(preg_match("/php$/", $file)){
include($file);
}
print '</div>';
if($j%6==0) print '<div style="clear:both;></div>';
endfor;
?>
thanks in advance for anyhelp
You can use scandir();
here is the code:
<?php
$dir = 'folder/';
$files = scandir($dir);
$count=2;
foreach($files as $file){
$count++;
echo '<div style="float:left; width:100px">';
if(strpos($file,".php")){
include($dir.$file);
}
echo '</div>';
if($count==6){echo'<div style="clear:both;></div>';}
}
?>
Let me know if you still have confusion
or simpler:
foreach(glob(dirname(__FILE__)."/*.php") as $i => $file) {
echo "<div style='float:left; width:100px'>";
include_once($file);
echo "</div>";
if($i%6==0) {
echo "<div style=\"clear:both;\"></div>";
}
);