unistake Posted November 5, 2010 Share Posted November 5, 2010 Hi all, I am using php to allow users to create a new directory e.g. www.website.com/theirname/ and would like to auto generate an index.php (or index.html) page if possible within this directory. Any tips on how to do it would be appreciated. The code I have for generating the directory is: <?php function handleError() { trigger_error('MY ERROR'); /** usage sample @handleError(); echo $php_errormsg; */ } // detect slash/backslash nomenclature dirname $path = dirname( __FILE__ ); $slash = '/'; (stristr( $path, $slash )) ? '' : $slash = '\\'; define( 'BASE_DIR', $path . $slash ); $folder = $_POST['reg']; // folder name $dirPath = BASE_DIR . $folder; // folder path // print results echo $slash; echo '<hr>'; $rs = @mkdir( $dirPath, 0777 ); @handleError(); if( $rs ) { // print success information echo 'was done!'; echo '<br>folder: <a href="' . $folder . '">' . $folder . '</a>'; echo '<br>dirPath: ' . $dirPath; }else{ // print error information echo "This webpage <b>www.website.com/".$folder."</b> appears to have been already been created. Please try another web name. If the problem persists please report it to us."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/ Share on other sites More sharing options...
PaulRyan Posted November 6, 2010 Share Posted November 6, 2010 Looks like you ain't looked at the manual See here: http://www.php.net/manual/en/function.fwrite.php Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1130908 Share on other sites More sharing options...
trq Posted November 6, 2010 Share Posted November 6, 2010 Please, don't create new pages for each user. PHP is a dynamic language, this means you can use a single page for each user. Hell, you don't even need to actually create the directories. Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1130914 Share on other sites More sharing options...
unistake Posted November 6, 2010 Author Share Posted November 6, 2010 Hi Thorpe, Thanks for the advice, I was thinking the same but for my website I want each user to have their own directory such as www.website.com/mine so that they have a memorable web name to pass on to their friends. I am fully aware that one php can do all but I just wanted this directory for the above use. In question to your comment, is there a way then of just adding an index.php page to the new directory then, and displaying all the information relating to that new directory name? e.g. www.website.com/mine $directoryname = 'mine' from domain above. $sql = "SELECT * FROM table WHERE id='$directoryname'"; How would I get the directory name in to a variable? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131026 Share on other sites More sharing options...
Adam Posted November 6, 2010 Share Posted November 6, 2010 As Thorpe said, you don't need to create a directory. You can use the Apache module mod_rewrite to rewrite the request URL from "example.com/mine" to something like "example.com/profile.php?username=mine". Create a file called ".htaccess" in your web root directory, and add: <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-zA-Z0-9_\.-]+)$ profile.php?username=$1 </IfModule> Create a file called profile.php, and just add the following for now: <?php print_r($_GET); ?> Then go to 'yourserver.com/mine'.. You should see the array printed out containing the username parameter. Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131032 Share on other sites More sharing options...
unistake Posted November 6, 2010 Author Share Posted November 6, 2010 ok, what would this come up as in the web browser? would it show example.com/mine or example.com/profile.php?username=mine? I will use that if it shows example.com/mine. Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131041 Share on other sites More sharing options...
Adam Posted November 6, 2010 Share Posted November 6, 2010 The URL is only rewritten internally, the user still sees whatever they enter into the address bar (example.com/mine). Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131042 Share on other sites More sharing options...
unistake Posted November 6, 2010 Author Share Posted November 6, 2010 Fantastic Thanks Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131142 Share on other sites More sharing options...
MSUK1 Posted November 7, 2010 Share Posted November 7, 2010 this is a great post, question ontop, dont see the need for a new thread, what if in your site you have the neccessary directories /Contact /About etc... is this going to rewrite them to profile.php?user=contact etc etc? or will current directories and future addon directories remain directores Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131223 Share on other sites More sharing options...
Adam Posted November 7, 2010 Share Posted November 7, 2010 The rewrite conditions below check to make sure the requested URL isn't an existing file name or directory: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131229 Share on other sites More sharing options...
MSUK1 Posted November 7, 2010 Share Posted November 7, 2010 this will complete erase the 404 error? is their a way to get this to check against a DB of usernames? Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131238 Share on other sites More sharing options...
trq Posted November 7, 2010 Share Posted November 7, 2010 is their a way to get this to check against a DB of usernames? Of course. Thats' what profile.php should do. Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131243 Share on other sites More sharing options...
MSUK1 Posted November 7, 2010 Share Posted November 7, 2010 but the .hta... will automatically create the impression that /johnsmith exists but then profile.php will say "no johnsmith found" for example? and the 404 error? everything request that doesnt match a folder / file will be treaten as a username? Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131277 Share on other sites More sharing options...
Adam Posted November 7, 2010 Share Posted November 7, 2010 Only requests matching the rewrite rule (a basic regex) will be rewritten: RewriteRule ^([a-zA-Z0-9_\.-]+)$ profile.php?username=$1 That means requests containing only the following characters will be treated as usernames "a-z A-Z _ . -" .. Otherwise the normal 404 error will be thrown. You can customize that to match your own needs; add a prefix like "user/", reduce the characters allowed, or add other rewrite conditions. For usernames that do match the pattern but don't exist, you want to throw a 404 header response code to tell the bwoser/search engines that page isn't found: if (/* username not found */) { header("HTTP/1.0 404 Not Found"); } Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131358 Share on other sites More sharing options...
MSUK1 Posted November 8, 2010 Share Posted November 8, 2010 so <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-zA-Z0-9_\.-]+)$ ClientArea/profile.php?username=$1 </IfModule> would ONLY rewrite request that look like "www.domain.com/ClientArea/BP" not, "www.domain.com/BP" that would get ignored? Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131579 Share on other sites More sharing options...
trq Posted November 8, 2010 Share Posted November 8, 2010 No, that would be.... RewriteRule ^ClientArea\/([a-zA-Z0-9_\.-]+)$ ClientArea/profile.php?username=$1 Quote Link to comment https://forums.phpfreaks.com/topic/217898-auto-generate-a-new-php-page/#findComment-1131595 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.