I am trying to open a sub folder when user clicked on main directory url link. It does not either show any errors or content.
Here is my php code to open main folder which is working fine.
$loc = 'C:/Apache24/htdocs/www.test.in/stRec';
if (is_dir($loc)){
if ($dh = opendir($loc)){
while (($file = readdir($dh)) !== false){
echo''.$file.'';
}
}
}
Code to open sub directory which shows no content.
if(isset($_GET['dir'])) {
$dir = $_GET['dir'];
$loc = 'C:/Apache24/htdocs/www.test.in/stRec'.$dir;
if (is_dir($loc)){
if ($dh = opendir($loc)){
while (($file = readdir($dh)) !== false){
echo $file;
}
}
}
}
C:/Apache24/htdocs/www.test.in/stRec'.$dir
path separator on windows is "\"
edit:
To get more details:
<?php
if (!isset($_GET['dir'])) {
die('dir not set');
}
$dir = $_GET['dir'];
$loc = 'C:\whatever\path' . $dir;
if (!is_dir($loc)) {
die('not a dir');
}
if ($handle = opendir($loc)) {
while (false !== ($entry = readdir($handle))) {
echo "$entry\n";
}
closedir($handle);
}
Related
I'm currently trying to merge several .csv files with the following code:
<?php $csvdir = get_template_directory() . '/exports';
$csvcontent = '';
if (is_dir($csvdir)) {
if ($handle = opendir($csvdir)) {
while (($file = readdir($handle)) !== false) {
if (substr($file, -4) === ".csv") {
$csvcontent .= file_get_contents($csvdir . $file);
}
}
closedir($handle);
}
}
$result = fopen('app/merge.csv', 'w');
fwrite($result, $csvcontent);
fclose($result); ?>
It's outputting a blank csv file at the moment with no errors. Is there anything obvious wrong with the code?
The template to generate this is in the same directory as the exports folder.
As I have mentioned in the comment, you are missing / after the exports directory name. Hence, the file name is going wrong while reading contents from it.
Also, check the directory is a valid one by echo $csvdir and echo is_dir($csvdir) before the processing starts.
Here is the working demo: https://repl.it/#fiveelements/MergeCSVContents
And here is your modified code:
<?php $csvdir = './exports/';
$csvcontent = '';
if (is_dir($csvdir)) {
if ($handle = opendir($csvdir)) {
while (($file = readdir($handle)) !== false) {
if (substr($file, -4) === ".csv") {
$csvcontent .= file_get_contents($csvdir . $file);
}
}
closedir($handle);
}
}
echo $csvcontent;
$result = fopen('exports/merge.csv', 'w');
fwrite($result, $csvcontent);
fclose($result); ?>
Hello stackoverflow community;
I'm trying to display a few files in a directory using php and coming unstuck:
In my file ('salad') I have three recipe files ('recipe1.txt', recipe2.txt, 'recipe3.txt') and I want to display them so I'm writing the following:
$script = opendir('salad');
while(false !==($file = readdir($script))) {
if (is_file($file)) {
echo "<p>$file</p>";
}
}
Unfortunately this only echos to the screen .DS_store, what am i doing wrong?
You could use this:
<?php
$dir = "/tmp"; // put what suits you here
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
sort($files);
print_r($files);
rsort($files);
echo"$files";
?>
source:
http://php.net/manual/en/function.scandir.php
I want to create a system which will include any .php file from a folder, similar to wordpress\plugins folder. Preferably drag and drop.
Any ideas how to do it?
Question is not clear...
read the folder files
scroll through the files found
if PHP - include it
<?php
function scandir2($dir)
{
$out = array();
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if ($file == '.' or $file == '..') continue;
if (!is_dir($dir . '/'. $file))
{
$out[] = $dir . '/' . $file;
}
}
closedir($dh);
}
}
sort($out);
return $out;
}
$i=scandir2(".");
foreach($i as $name)
{
if(strstr($name ,'.php'))
include($name);
}
?>
this code scan directory and include php files ...
Here is the code to do that:
foreach (glob("/path/*.php") as $filename) {
include $file;
}
The glob() function returns an array of all .php files on the specified path.
Here is how I would do it...
<?php
$dirPath = '/path/to/files';
if ($handle = opendir($dirPath)) {
/* loop over files in directory */
while (false !== ($entry = readdir($handle))) {
/* find extension */
$ext = substr($entry,-3);
$ext = strtolower($ext);
/* include file if extension is PHP */
if ($ext === 'php') {
include_once($dirPath .'/'. $entry);
} //if
} //if
closedir($handle);
} //if
Note that there is several security concerns and possibly performance concerns.
Right now there is an upload system on the site I am working on where users can upload some documents to a particular file. Later on I will need to make these documents downloadable. Is there an easy way to iterate through all the files in a particular directory and create download links for the files?
Something like:
foreach($file){
echo 'somefilename';
}
Many thanks in advance.
if($dh = opendir('path/to/directory')) {
while(($file = readdir($dh)) !== false) {
if($file == "." || $file == "..") { continue; }
echo '' . $file . '';
}
closedir($dh);
}
You should see opendir.
Example from that page adapted to question:
$dir = "/etc/php5/";
$path = "/webpath";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "$file";
}
closedir($dh);
}
}
I'm looking for a solution to zip a folder's content, including all subfolders that are in the folder, but not the main folder itself.
I started from this function, that adds a whole folder to a zip archive
function addFolderToZip($dir, $zipArchive){
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
//Add the directory
$zipArchive->addEmptyDir($dir);
// Loop through all the files
while (($file = readdir($dh)) !== false) {
//If it's a folder, run the function again!
if(!is_file($dir . $file)){
// Skip parent and root directories
if( ($file !== ".") && ($file !== "..")){
addFolderToZip($dir . $file . "/", $zipArchive);
}
}else{
// Add the files
$zipArchive->addFile($dir . $file);
}
}
}
}
}
but I don't see how to change the function to (maybe using a third argument ?)
$z = new ZipArchive();
$z->open("zips/new.zip", ZIPARCHIVE::CREATE);
addFolderToZip("unzipped/",$z);
$z->close();
I've got it, I have created an argument $localpath, set to "":
function add_folder_in_path($folder_path,$local_path,$z)
{
$dh=opendir($folder_path);
while (($file = readdir($dh)) !== false)
{
if( ($file !== ".") && ($file !== ".."))
{
if (is_file($folder_path.$file))
{
$z->addFile($folder_path.$file,$local_path.$file);
}
else
{
add_folder_in_path($folder_path.$file."/",$local_path.$file."/",$z);
}
}
}
}
Here is how I call it
$z = new ZipArchive();
$z->open("zipped/new.zip", ZIPARCHIVE::CREATE);
echo "<br>";
add_folder_in_path("myzips/","",$z);
$z->close();