By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

PHP file making from directory... Problem.

P: 30
Here is my code and what it does is create a page for every directory with the contents of choice.

Its been working on all letters except "u" I can't seem to figure it out.

Does someone see the flaw that I'm not seeing?

[PHP]<?php
$dir_build = "../data/pages/";
$inside = array("0-9","A","B","C","D","E","F","G","H","I","J","K","L" ,"M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z ");

foreach($inside as $var){
$var = strtolower($var);
build($dir_build.$var."/");
}

//FOR ALPHA build($dir_build);

function build($dir){
if ($handle = opendir($dir)) {
$filecount = "0"; //reset the number of files
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && (!strstr($file,'.txt'))) { // remove the move up directory commands. (. and ..)
$file = strtolower($file); // make all filenames lowercase (looks better. can be removed)
$fileList[] = trim($file); // add the file to an array so it can be sorted easily.
$filecount = $filecount + 1; //count the number of files
}
}
if(is_array($fileList)){
sort ($fileList); // sort the file list in the array
reset ($fileList); // go back to the top of the array
closedir($handle);
}
}

if ($handle2 = opendir($dir)) { // read the current directory. Change the "." to the directory you need.
$filecount2 = "0"; //reset the number of files
while (false !== ($file2 = readdir($handle2))) {
if ($file2 != "." && $file2 != ".." && (strstr($file2,'.txt'))) {
$file2 = strtolower($file2); // make all filenames lowercase (looks better. can be removed)
$fileList2[] = substr(trim($file2),0,-4); // add the file to an array so it can be sorted easily.
$filecount2 = $filecount2 + 1; //count the number of files
}
}
if(is_array($fileList2)){
sort ($fileList2); // sort the file list in the array
reset ($fileList2); // go back to the top of the array
closedir($handle2);
}
}
if(is_array($fileList) && is_array($fileList2)){
$not_made = array_diff($fileList, $fileList2);
}

echo "<br /><br />The following were made in $dir<br />";
if($not_made){
foreach ($not_made as $value) {
$letter2 = $value{0};
if(is_int($letter2) || !$letter2){
$letter2 = "0-9";
}

$fh = fopen($dir.$value.".txt", 'w') or die("can't open file");
$value2 = ucwords(str_replace("."," ", $value));
$stringData = "My Data Here";
fwrite($fh, $stringData);
fclose($fh);
echo $dir.$value."<br />";
unset($value2); // break the reference with the last element
}
}
}
?>[/PHP]
Feb 25 '08 #1
Share this Question
Share on Google+
2 Replies


eragon
100+
P: 431
Is there an error your getting? And may i suggest, if it works fine for other files, then check your permissions. If theres any more info send it.
Mar 7 '08 #2

P: 30
I did check permissions and no errors... its actually happening with x, q, and u. very awkward . Might have to redo whole script. :(
Mar 12 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.