When I run:
$url='foldername';
$dir = opendir($url);
//List files in images directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "<br />";
}
closedir($dir);
...it outputs:
filename: a.gif
filename: file.html
filename: g.gif
filename: gg.html
I would like to see all the files and folders on another server from the URL:
$url="http ://example.com"
How do I find the files and folder names from example.com?
It is possible. Just got to go outside the box. Only flaw with this is index output
<?
$matches = array();
preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text('http://www.website.com/ images/'), $matches);
foreach($matches[2] as $match)
{
echo $match . '<br>';
}
function get_text($filename)
{
$fp_load = fopen("$filename", "rb");
if ( $fp_load )
{
while ( !feof($fp_load) )
{
$content .= fgets($fp_load, 8192);
}
fclose($fp_load);
return $content;
}
}
?>
http:// does not support directory listing. What you're trying to do is impossible.
It's impossible of course, otherwise websites would be much more vulnerable since anyone could explore their directories tree!
If you have another way of accessing that website (e.g. if it's yours), like FTP or SSH, it becomes possible.
Maybe with php is impossible.. but there r programs that can do what u r trying to do.
"intellitamper" is one of them. http://www.softpedia.com/get/Internet/Other-Internet-Related/IntelliTamper.shtml <-- the link.
its doesnt work on windows 7
u cannot get server sides files like php.
u can only see folders, html, js or image files.
Otherwise it would be heaven... anyone could hack any website.
This should work fine in some servers
function get_text($filename) {
$fp_load = fopen("$filename", "rb");
if ( $fp_load ) {
while ( !feof($fp_load) ) {
$content .= fgets($fp_load, 8192);
}
fclose($fp_load);
return $content;
}
}
$matches = array();
preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text('https://www.example.com/'), $matches);
foreach($matches[2] as $match) {
echo $match . '<br>';
}
Related
I'm beginner in html and php. I trying to create a script that will open the directory, display all text files, open each of them for editing and save the changes to a file (all the script operation will be transferred to the html form). Unfortunately, after opening the directory and viewing the files, I have trouble reading their contents. Could someone tell me what I doing wrong ?
Thank for help
<?php
$path = "books/";
$books = opendir($path);
while (($book = readdir($books)) !== false)
{
echo $book;
foreach (glob("*.txt") as $readfile)
{
$readFile = fopen($book, "r") or die("Permission error");
echo fread($readFile, filesize($book));
fclose($readFile);
}
}
closedir();
?>
Server response:
...Atlas_chmur.txtDiuna.txt
I used the loop so that only .txt files were opened. Currently I have this:
<?php
$path = "books/";
$books = opendir($path);
while (($book = readdir($books)) !== false)
{
echo $book;
$readFile = fopen($book, "r") or die("Permission error");
echo fread($readFile, filesize($book));
fclose($readFile);
}
closedir();
?>
Now I'm getting an error when trying to read the files.
Server response:
. Warning: fopen (.): Failed to open stream: Permission denied in C: \ xampp \ htdocs \ BooksEditorForm \ index.php on line 9 Permission error
Try this code
$path = "blog/";
$books = opendir($path);
while (($book = readdir($books)) !== false)
{
if( substr($book, -4) === ".txt" )
{
$filePath = $path.$book;
$readFile = fopen($filePath, "r") or die("Permission error");
echo fread($readFile, filesize($filePath));
fclose($readFile);
}
}
closedir();
Let me explain it...
First, occasions where you need loop inside a loop are quite rare so if your code has them, analyze it because there's a big possibility that problem can be solved differently and more efficient.
Code: value of $book is string that contains filename so simple use of substr() function to check last 4 characters will tell us is it of "*.txt" format.
Other thing that is changed is filepath; $book contains it's name but your script is looking for a file from its own perspective so file path should be containing folder + filename.
And there's no need for closing PHP tags at the end unless you have something else following it that is not PHP (like HTML).
I have used the below code. It's working well.try this code
$files = scandir('books/');
if(count($files) > 0) {
foreach ($files as $key => $value) {
echo '<pre>'; print_r($value); echo '</pre>';
// to read files data
$readFileVar = fopen ($value, "r");
while ($filedata = fgets($readFileVar)) {
print_r($filedata);
}
}
}
Im trying to make a Php file that receives nothing and checks every file on the folder, searching for a string inside them. it echos a array of filenames that have the string inside. Any way to do it, possibly with low memory usage?
Thank you a lot.
To achieve something like this, I recommend you read about the DirectoryIterator class, file_get_contents, and about strings in PHP.
Here is an example of how you can read the contents of a a given directory ($dir) and use strstr to search for a specific string occurrence in each file's contents ($contents):
<?php
$dir = '.';
if (substr($dir, -1) !== '/') {
$dir .= '/';
}
$matchedFiles = [];
$dirIterator = new \DirectoryIterator($dir);
foreach ($dirIterator as $item) {
if ($item->isDot() || $item->isDir()) {
continue;
}
$file = realpath($dir . $item->getFilename());
// Skip this PHP file.
if ($file === __FILE__) {
continue;
}
$contents = file_get_contents($file);
// Seach $contents for what you're looking for.
if (strstr($contents, 'this is what I am looking for')) {
echo 'Found something in ' . $file . PHP_EOL;
$matchedFiles[] = $file;
}
}
var_dump($matchedFiles);
There is some extra code in this example (adding a trailing slash to $dir, skipping dot files and directories, skipping itself, etc.) that I encourage you to read and learn about.
<?php
$folderPath = '/htdocs/stock/tae';
$searchString = 'php';
$cmd = "grep -r '$searchString' $folderPath";
$output = array();
$files = array();
$res = exec($cmd, $output);
foreach ($output as $line) {
$files[] = substr($line, 0, strpos($line, ':'));
}
print_r($files);
I'm trying to make my PHP script open more than 1 text document and to read them.
My current script is as follows:
<?php
//$searchthis = "ignore this";
$matches = array();
$FileW = fopen('result.txt', 'w');
$handle = #fopen("textfile1.txt", "r");
ini_set('memory_limit', '-1');
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
if(stripos($buffer, $_POST["search"]) !== FALSE)
$matches[] = $buffer;
}
fwrite($FileW, print_r($matches, TRUE));
fclose($handle);
}
?>
I'm trying to fopen like a bunch of files, maybe like 8 of them or less.
How would I open, and read all these files?
Any help is GREATLY appreciated!
Program defensively, check the return's from functions to ensure you are not making incorrect assumptions about your code.
There is a function in PHP to read the file and buffer it:
enter link description here
I don't know why you would want to open a lot of files, it surely will use a lot of memory, anyway, you could use the file_get_contents function with a foreach:
$files = array("textfile1.txt", "textfile2.txt", "textfile3.txt");
$data = "";
foreach ($files as $file) {
$data .= #file_get_contents($file);
}
echo $data;
There is a function in php called file which reads entire file into an array.
<?php
// "file" function creates array with each line being 1 value to an array
$fileOne = file('fileOne.txt');
$fileTwo = file('fileTwo.txt');
// Print an array or do all array magic with $fileOne and $fileTwo
foreach($fileOne as $fo) {
echo $fo;
}
foreach($fileTwo as $ft) {
$echo $ft;
}
?>
Read more about : file function ion php
I would like to point out before I get into this that I am a PHP newb, and I have been struggling for a while with this before finally deciding that I don't know what I'm doing with
function extract_bin($Filename, $output){
global $template;
global $BinColumnDelimiter;
global $BinLineEnd;
$decodedfile = fopen($output,'w+');
$decodedfilecontents = "";
$handle = fopen($Filename, "r");
if ($handle) {
foreach ( $template as $ColName=>$Endians)
$decodedfilecontents .= $ColName.$BinColumnDelimiter;
$decodedfilecontents .= $BinLineEnd;
while (!feof($handle)) {
$lastLine = '';
foreach ($template as $ColName => $Endians){
$hex = bin2hex(fread($handle,$Endians));
if ( $Endians <= 4) {
$val = hexdec(decodehex($hex));
if ( $val == 4294967295 ) $val = -1;
$lastLine .= $val.$BinColumnDelimiter;
} else {
$lastLine .= str_replace("\t"," ",hex2str($hex)).$BinColumnDelimiter;
}
}
if ( ! feof($handle))
$decodedfilecontents .= $lastLine.$BinLineEnd;
}
fclose($handle);
fwrite($decodedfile,$decodedfilecontents);
fclose($decodedfile);
}
echo '<h1>'.$output.'</h1>';
echo '<hr />';
print_r($decodedfilecontents);
}
And I keep getting this error:
Warning: fopen(): Filename cannot be empty in C:\xampp\htdocs\rh-bin\func.php on line 8
My questions are, what am I doing wrong in my attempt to pass on the uploaded file as an attachment? And, why does $FileName seem to be empty? Also, not everyone will be uploading files, so how could I allow the submit?
print_r($Filename) to check if it prints anything
check $Filename is empty or not before passing to fopen(). // it is good practice.
According to your question, your saying that not every one is uploading file, then your calling this function every time with empty value while not uploading the file also check this and make sure.
I made some kind of PHP file to organize my Images from an old website. The admin of that old site didn't use subfolders so I got thousands of Images in one dir.
I got a csv File with filenames and categories, now I want to just copy this filenames line by line and let PHP copy them to a destenie directory.
This is on XAMPP on my local machine, maybe this is the problem?
Here is what I have already:
<?php
$source = "";
$destination = "Kompaniefest05/";
$handle = fopen($source."names.txt", "r");
$buffers[] = "";
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
array_push($buffers, $buffer);
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
};
foreach ($buffers as $buffer){
echo "Copy ".$source.$buffer." to ".$destination.$buffer;
if(copy($source.$buffer, $destination.$buffer)){
echo "</br>True</br>";
}else{
echo "</br>False</br>";
};
};
?>
The code runs through as expected, and I get list with all filenames inside the .txt file, but always with "False", and the destination directory stays empty.
Is this a configuration problem with XAMPP or do I have some mistakes in my code?
// Edit: Sample of the .txt file
_1_20081008_2089220027.jpg
_2_20081008_1467211531.jpg
_3_20081008_1388589383.jpg
_4_20081008_1327719202.jpg
_5_20081008_1691023977.jpg
_6_20081008_1494910005.jpg
_7_20081008_1101725852.jpg
_8_20081008_1123823020.jpg
_9_20081008_1092805517.jpg
_10_20081008_1752481220.jpg
_11_20081008_1420860420.jpg
_12_20081008_1803761675.jpg
_13_20081008_1199173697.jpg
_14_20081008_1877520136.jpg
_15_20081008_1344646088.jpg
_16_20081008_1918096785.jpg
_17_20081008_1895142423.jpg
_18_20081008_1841060330.jpg
_19_20081008_1438833482.jpg
_20_20081008_1871628956.jpg
_21_20081008_1141581267.jpg
_22_20081008_1416565613.jpg
_23_20081008_1868584205.jpg
_24_20081008_1265588276.jpg
_25_20081008_2023422375.jpg
_26_20081008_1410663038.jpg
_27_20081008_1398533505.jpg
_28_20081008_1413417063.jpg
_29_20081008_1827605061.jpg
_30_20081008_1883399407.jpg
_31_20081008_1468535188.jpg
_32_20081008_1742608861.jpg
_33_20081008_1818421650.jpg
_34_20081008_1682119571.jpg
_35_20081008_1066121950.jpg
Try this, i checked it on localhost with some sample files and its working.
You can also use your own code with a small change.
Just change array_push($buffers, $buffer); to array_push( $buffers, trim( $buffer ) );
Because your file name contains a new line at the end, that stopping you from copying the files.
And always turn on error_reporting while coding :)
<?php
$source = "";
$destination = "Kompaniefest05/";
if ( !file_exists( $destination ) ) {
mkdir( $destination, 0755 );
}
$buffers = file( $source."names.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
foreach( $buffers as $buffer ) {
$fcopy = $source.trim( $buffer );
$tcopy = $destination.trim( $buffer );
echo "Copying ".$fcopy." to ".$tcopy." - ";
if ( file_exists( $fcopy ) ) {
if ( copy( $fcopy, $tcopy ) ) {
echo "SUCCESS";
}
else {
echo "FAILED";
}
}
else {
echo "FILE MISSING";
}
echo "<br />";
}
?>