PHP Auto Directory Creator Based on KWs

Status
Not open for further replies.

plepco

New member
May 24, 2007
275
11
0
New Orleans
www.massindexer.com
I need to create over 100 directories. Each directory will be named according to a keyword on a list. I figured it would take me all day to do this, so I wrote a PHP script to do this. My attempt has had two unfortunate outcomes.
1) I took all day to write this script when I could have been making the directories
2) The script doesn't work

It creates only one directory which is the last keyword in the file.

So can someone take a look and see what's wrong with the code?
PHP:
<?php
    $dirs=file('keywords.txt');
    $num = count($dirs);

for ($i = 0; $i < $num; $i++)
{
    $conn = ftp_connect("ftp.example.net") or die("Could not connect");
    ftp_login($conn,"username","pass");
    ftp_mkdir($conn,"/public_html/test/uploads/$dirs[$i]");
    ftp_close($conn);
}

?>
 


Wouldn't it be easier to run mkdir locally instead of via FTP?

Code:
[FONT=Courier New][COLOR=#0000bb]<?php
    $dirs[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]file[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]'keywords.txt'[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]);
    [/COLOR][COLOR=#0000bb]$num [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]count[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$dirs[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]);

for ([/COLOR][COLOR=#0000bb]$i [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]0[/COLOR][COLOR=#007700]; [/COLOR][COLOR=#0000bb]$i [/COLOR][COLOR=#007700]< [/COLOR][COLOR=#0000bb]$num[/COLOR][COLOR=#007700]; [/COLOR][COLOR=#0000bb]$i[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]++)
{[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]
[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]   shell_exec(`mkdir [/COLOR][COLOR=#dd0000]"/public_html/test/uploads/`.$dirs[$i].`"`);[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]);[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]
}
[/COLOR][COLOR=#0000bb]?>[/COLOR][COLOR=#000000] [/COLOR][/FONT]
[FONT=Courier New][COLOR=#000000]
[/COLOR][/FONT]
 
You can make a lot of dirs with one mkdir command. mkdir dir1 dir2 will make two dirs. Extra credit if you can guess what the dir's names are.
 
well there's a lot wrong with this and some right.

1. you php has a built in mkdir() function. No need for risky shell calls which a lot of times don't work on shared hosting.
2. you should be uploading your file and the script to the server in the dir you want the stuff. And your script should look like this.
<?
$dirs = file('dirs.txt');
foreach($dirs as $dir){
mkdir($dir);
}
?>
of course that's with one dir name per line.
3. if you HAVE to use ftp, WHY are you connecting for each directory creation? You should connect 1 time. then run the mkdir through the loop.
<?php
$dirs=file('keywords.txt');
$num = count($dirs);
$conn = ftp_connect("ftp.example.net") or die("Could not connect");
ftp_login($conn,"username","pass");

for ($i = 0; $i < $num; $i++)
{
ftp_mkdir($conn,"/public_html/test/uploads/$dirs[$i]");
}
ftp_close($conn);
?>
4. verify that your uploads dir is writable
5. in your code you're using a for statement which is fine. However it's much cleaner to user a foreach when you're iterating through a whole array. As there's no counter vars to keep track of.

Hope this helps yah brotha. 2 is your best answer.
 
Now I'm back but this has now become a really old thread but hopefully you guys are still out there...

Okay, well I don't need to do this via FTP. I can do this from the server like you suggested smaxor by uploading a script and my kw text file and running it. So of course you were correct - 2 is the best answer and now I see that makes a whole lot more sense.

However I am still having a problem: the script only creates a directory from the last keyword in the file. If I put fifty keywords in the file, three keywords in the file...whatever. It only creates the last one.

I get an error like this:
Warning: mkdir(testkeyword1 ) [function.mkdir]: File exists in /home/testaccount/public_html/uploads/dirlist.php on line 16

Any ideas why?
 
Last edited:
In Smaxor's answer, the following line:
ftp_mkdir($conn,"/public_html/test/uploads/$dirs[$i]");
should be:
ftp_mkdir($conn,"/public_html/test/uploads/{$dirs[$i]}");
OR
ftp_mkdir($conn,"/public_html/test/uploads/" . $dirs[$i]);

You cannot get the value of an array variable INSIDE a quoted string unless you either 1) 'escape it' (example1) OR break out of it(example2).

Other than that, print_r() is your friend. Look it up, love it.

My fav fast and simple debugging:

print "<br><pre>-----<br>";
print_r();
print "<br>-----<br></pre>";
//exit();

Have fun!
 
I usually don't give up until I have a particular problem figured out. This script never seemed to work.

However, I discovered the problem. It wasn't the script at all. My script has been creating directories just fine. I just couldn't SEE them.

This is what happened:
1) I wrote the script and uploaded it via FTP to my shared hosting server
2) I ran the script (via my browser)
3) It created a bajillion directories just like I told it to unknown to me
4) I then looked (via my FTP program) at the server to verify the directories were created - but they weren't there from what I could see
5) I ran the script again and again, each time getting errors that didn't make sense to me

So the way I discovered this is just now I finally used the cPanel file manager to look at my directories and I saw that every single directory was there!

So apparently the script created the directories as root or something, thus not allowing me to see and/or use the directories.

I attempted to use the chown function to change myself to the owner, but alas it doesn't work.

Oh well. Just thought I'd share. Let's consider this a Teachable Moment.
 
Status
Not open for further replies.