Page 1 of 1

Dynamic folder/file creation? Script help...

Posted: Thu Nov 11, 2004 4:52 am
by mhulse
I can't seem to figure out how to create a dynamically-named folder/file on my server... I get a "failed to open stream: No such file or directory" warning... I kinda assumed that would happen. Does anyone have any suggestions? I am also curious to hear peoples thoughts on making the below script secure:

Code: Select all

<?php
echo "<form action='".$_SERVER['PHP_SELF']."?action=create_quote' method='post' name='create_quote_now'>"."\n";

print <<< PRINT_PAGE

 Company: <input name="company" type="text" value="Company" size="15" maxlength="40">
 <input name="submit" type="submit" value="Submit">
 </form>
 
PRINT_PAGE;

if ($_GET['action'] == "create_quote") { // If user donated money:
 
 $html="<head><title>Quote for $company</title></head>\n 
 This is a quote for $company";
 
 //generate file name
 $qfname = $company . '_quote';

 //open file
 $fp=fopen('./$company/$qfname.html','wb');
 fwrite($fp,$html);
 fclose($fp);
 
 echo "your file has been created, <a href='./$company/$qfname.html'>click here</a> to scope it!";
 
}
 ?>
Any help would be greatly appreciated.

Thanks!
Cheers
m

Posted: Thu Nov 11, 2004 5:13 am
by Wayne
try this

Code: Select all

$filename = "./" . $company ."/" . $qfname . ".html";
$fp=fopen($filename,'wb');
....
you might want to note the differences between using ' and "

Posted: Thu Nov 11, 2004 9:06 am
by josh
Make sure everything you will be writeing to, or folders you will be working with/editing/deleteing are chmod'd

http://php.net/chmod

Posted: Thu Nov 11, 2004 10:28 am
by mikeb
I've just been through the mill with this stuff and have had some success. I now have a site which writes itself to the server. The main problem I had to begin with was file permissions on the hosting server. Locally my site was sitting on XP/IIS while remotely the site was on Unix/Apache. I'm not good on IIS file permissions so my site was doing exactly as you mention locally. I changed the permissions on the live site 'though and this worked a treat. Use your ftp package to 'chmod' the directory where the file will be written.

Only problem is, securing the permissions after. I've just submitted help on this myself in this forum! The trick will be to get the script to 'open up' the permissions, get the script to write the file(s), and finally get the script to 'close up' the permissions again.

Posted: Thu Nov 11, 2004 6:01 pm
by mhulse
Hi,

Thanks all for your responses! I really appreciate the help.

I am now able to write a dynamic folder/file to my host's server (using below script)...

...But, now I am having big probs with being able to delete the same folder/files from server!

I think it is because PHP runs as the user "nobody", which means that I need to login as "nobody" to delete the files, but unfortunately, I am not sure what software/how to log-in as "nobody" to do that... any suggestions (me = mac user)?

Or, how can I write the below code to make files/folders with proper perms? My host, webhostfreaks, only allows a max chmod of 666 :( .

Also, anyone have suggestions when it comes to error-checking the two form fields? I tried "isset" and "empty" with no luck...

Code: Select all

<?php

error_reporting(E_ALL ^ E_NOTICE); // Turn off error "Notice" reporting.
//error_reporting(0); // Turn off error reporting.

if ($_GET['action'] == 'create_quote') { // Generate quote/bid:

 $en_form_num = number_format($quote); // Add commas to the quote number.
 
 // HTML to be written:
 $html='<head><title>Quote for '.$company.'</title></head>';
 $html.='This is a $'.$en_form_num.' quote for '.$company.'.';

 $file_name = strtolower($company); // Make company name lower-case for making file. 
 $file_name = htmlspecialchars(stripslashes($file_name)); // Convert to special chars and strip slashes.
 $urls = array(' ', '.', '?', '=', '+', ':', '%', '$', ','); // Create array and pop with items to be stripped from var.
 $file_name = str_replace($urls,"_",$file_name); // Now remove above array items from company name.
 $qfname = $file_name.'_quote';

 // Make directory and open file:
 if(!is_dir('./'.$file_name)) { mkdir('./'.$file_name); }
 $fp = fopen('./'.$file_name.'/'.$qfname.'.html','w'); // Open for writing only; If the file does not exist, attempt to create it.
 fwrite($fp,$html);
 fclose($fp);
 
 echo "your file has been created, <a href='./$file_name/$qfname.html'>click here</a> to scope it!";
 
 exit();
}

echo "<form action='".$_SERVER['PHP_SELF']."?action=create_quote' method='post' name='create_quote_now'>"."\n";

print <<< PRINT_PAGE

 Company: <input name="company" type="text" size="15" maxlength="40">
 Quote: $<input name="quote" type="text" size="15" maxlength="40">
 <input name="submit" type="submit" value="Submit">
 </form>
 
PRINT_PAGE;

?>
Also, how could I avoid using exit() while still hiding other parts of the script? Meaning, on the above script, when the form gets submitted, it goes to a "congrats page"... without the exit(), I would still see the form... any tips/suggestions? I am guessing the best thing to do is to break up my script into multiple PHP pages.

Any (more) help would rock!

Thanks! Cheers! :D M

Posted: Thu Nov 11, 2004 10:15 pm
by mhulse
Howdy,

Well, this fixes my file permission problems:

Code: Select all

// Make directory and open file: 
if(!is_dir('./'.$file_name)) { 
    $oldumask = umask(0); 
    mkdir('./'.$file_name, 0777); // or even 01777 so you get the sticky bit set 
    umask($oldumask); 
    }
I am still curious about exit() function... is there a better way to display specific blocks of code? Or, should I just create multiple pages?

Anyway, keep the suggestions rolling.. I really appreciate any advice... I love php!

Cheers,
Micky

Posted: Fri Nov 12, 2004 6:01 am
by josh
I am still curious about exit() function... is there a better way to display specific blocks of code? Or, should I just create multiple pages?
Use a switch?

Code: Select all

switch ($var) {
case 'page1':
   do_stuff();
break;
case '2':
   do_stuff();
break;
case NULL:
   do_stuff();
break;
}