Jump to content

Writing files to a different directories & sub directories


Recommended Posts

I have a problem that I'm sure is a piece of cake for you guys to work out.

 

I have a small index.php script here which creates a php file (with text appended from the index.php script) each time the page is visited. The problem is that I can only get the files to be created in the same folder as the .php file. I want the file to be generated in each folder and sub folder but can't get it to work.

 

Or is there a way when I access index.php then this file index.php will run in each folder and subfolder to act as if index.php is in all of these folders(without copying the index.php to each folder)

 

Here is the code

 

<?php
    
    
    $path = array("./files/","./images/","./txt/","./related/");
    
    $path2= array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/files/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/images/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/txt/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/related/");
    
    $start="";
    
    $Fnm = "./include.php";
    
    $inF = fopen($Fnm,"w");
    
    fwrite($inF,$start."\n");
    
    
    
        $folder = opendir($path[0]);
    
        while( $file = readdir($folder) ) {
    
               if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
    
                    $folder2 = opendir($path[1]);
    
                    $folder3 = opendir($path[2]);
                    $folder4 = opendir($path[3]);
    
                    $imagename ='';
    
                    $txtname ='';
                    $related ='';
    
                    while( $file2 = readdir($folder2) ) {
    
                        if (substr($file2,0,strpos($file2,'.')) == substr($file,0,strpos($file,'.'))){
    
                            $imagename = $file2;
    
                        }
    
                    }
    while( $file4 = readdir($folder4) ) {
    
                        if (substr($file4,0,strpos($file4,'.')) == substr($file,0,strpos($file,'.'))){
    
                            $related = $file4;
    
                        }
    
                    }
    
    
    while( $file3 = readdir($folder3) ) {
    
    
    
    if (substr($file3,0,strpos($file3,'.')) == substr($file,0,strpos($file,'.'))){
    
                       
    
                            $txtname = $file3;
    
                            
    
                            $fh = fopen("/home3/socialb8/public_html/mysite.info/player/txt/$txtname", 'r');
    
                            $theData = fread($fh, filesize("/home3/socialb8/public_html/mysite.info/player/txt/$txtname"));
    
                            fclose($fh);
    
                            
    
                        }
    
    
    
                    }
    
                    closedir($folder2);
    
                    closedir($folder3);
                    closedir($folder4);
    
                $result="{\nlevels: [\n{ file: \"$path2[0]$file\" }\n],\nimage: \"$path2[1]$imagename\",\ntitle: \"$file\",\ndescription: \"$theData\",\n 'related.file':'$path2[3]$related'\n},\n";
    
                fwrite($inF,$result);
    
               }
    
        }
    
        fwrite($inF,"");
    
        closedir($folder);
    
        
    
    fclose($inF);
    
    ?>

Not able to give your script a proper looking over atm, and as such I cannot help with the main question just yet. Though, I would like to point out something very important none the less:

$test = "http://www.test.com/jalla/index.php/../../do_not_overwrite/test.html";
echo dirname ($test);
// Result: http://www.test.com/jalla/index.php/../../do_not_overwrite

This is the reason why you should never trust anything that comes from the client, and always sanitize PHP_SELF with this snippet.

 

Another good question is why are you trying to dynamically create files in those folders, based upon your users visiting them or not? This doesn't seem like a good idea, and I am almost positive it could have been solved much better by utilizing some of Apache's functionality and .htaccess configuration files.

The actual answer to your question:  You appear to only write to one file, which is:

$Fnm = "./include.php";
$inF = fopen($Fnm,"w");

Put the folder definitions in the $Fnm and you should be fine.  You may have to make the directory first.

 

Other comments:  Whatever you're doing here is probably wrong, I bet you can do this much easier another way if you tell us why you need to do this.

Ok let me explain what this script does

It scans the folders "files" "images" "txt" "related" and get the files in each folder and then it writes to the includes.php (in root)

 

If you look at this you will understand more

 $result="{\nlevels: [\n{ file: \"$path2[0]$file\" }\n],\nimage: \"$path2[1]$imagename\",\ntitle: \"$file\",\ndescription: \"$theData\",\n 'related.file':'$path2[3]$related'\n},\n";
    
                fwrite($inF,$result);

 

What I require is the script scans the subfolders too and in each folder write a file include.php in similar fashion

 

For example

/folder/category1/files

/folder/category1/images

/folder/category1/txt

/folder/category1/related

 

then it will create include.php in category1 folder

and

 

/folder/category2/files

/folder/category2/images

/folder/category2/txt

/folder/category2/related

 

then it will create include.php in category2 folder

and

 

/folder/category3/files

/folder/category3/images

/folder/category3/txt

/folder/category3/related

 

then it will create include.php in category3 folder

 

and so on

 

Is there a way I could achive this through recursive or any other

if so can you please explain the code ?

Well the purpose is the create an automatic video playlist from the folders

so include.php contains the format of playlist which is then read by my video player

 

here is the result of include.php which I am getting

 

include.php doesnt contains any php code (it can be a txt file but php extension allows me to include it in my video player)

 

{

levels: [

{ file: "http://mysite.com/files/test video 3.mp4" }

],

image: "http://mysite.com/images/test video 3.gif",

title: "test video 3.mp4",

description: "Test video3 description",

'related.file':http://mysite.com/related/test video 3.xml'

},

{

levels: [

{ file: "http://mysite.com/files/test video.mp4" }

],

image: "http://mysite.com/images/test video.png",

title: "test video.mp4",

description: "Test video description",

'related.file':'http://mysite.com/player/related/'

},

 

folders "files, images, txt, related contains the respective files which I want to scan automatically

It absolutely should not have a .php extension if it's not php code. 

 

You need to simply open a new file handle for each destination.  Your code here, which I've already shown you, opens one copy of the file:

 

$Fnm = "./include.php";
$inF = fopen($Fnm,"w");

Do that exact same thing, but make $Fnm a different path.  Your awfully named $folder2 and $file6 and all those should contain some semblance of the proper directory structure to use in fopen.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.