SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Dynamic folder/file creation: Script help...

    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:

    PHP Code:
    <?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

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Leuven, Belgium
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For debugging purposes only, you could replace
    $fp=fopen('./$company/$qfname.html','wb');
    fwrite($fp,$html);
    fclose($fp);


    with:
    if(!isdir('./'.$company)){
    echo $company .'isn't a directory yet';
    $check = mkdir('./'.$company);
    if(!$check) die '<br />set '.realpath('/.')'. permissions to 777';
    fwrite($fp,$html);
    fclose($fp);


    note: using single quotes (a good idea) forces you to end quotes and concatenate to get variable values = i.e., './'.$company.'/'.$qfanme.html.' instead. I haven't looked this over very carefully, but it's possible that this is the only real thing that's making your script not work.

    Be very, very careful though about how this just lets user input create a directory / file. You really should have some kind of check in place. Also, it would be a good idea to have some convention where spaces are changed to '_' for the filename and vice-versa.

  3. #3
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for your response minck, I really appreciate your time and advice.



    So, thanks to you I am now able to write a dynamic folder/file to my host's server... 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 use nobody to delete the files, but unfortunately, I am not sure what software/how to log-in as nobody to do that... any suggestions?

    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 form fields? I tried "isset" and "empty" with no luck...

    PHP Code:
    <?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! M

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    Madison, WI
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a quick fix for deleting things as php is to write a simple script that will take care of it for you:
    PHP Code:
    <?php
    $deleteme
    ='/path/to/file.ext';
    if(
    is_file($deleteme)) {
       
    unlink($deleteme);
       echo 
    "File $deleteme has been deleted.\n";
    }
    else {
       echo 
    "File $deleteme could not be found.\n";
    }
    ?>
    i've had to use this a lot as i have been developing a wiki system and have had to write/delete a LOT of test files as php

  5. #5
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, thanks for the info on deleting folders via php script... I will be working on a function to list and delete folders in the quote dir.

    This is going to be a script to create personalized quotes/bids for prospective clients...

    Here is the code that will create folders that can then be deleted later via FTP:

    PHP Code:
     // Make directory and open file:
     
    if(!is_dir('./'.$file_name)) {
         
    $oldumask umask(0);
        
    mkdir('./'.$file_name0777); // or even 01777 so you get the sticky bit set
        
    umask($oldumask);
        } 
    using above code, I can now create folders/files and then delete later using ftp... cool!

    Thanks for all your help peeps! Keep the suggestions coming.

    Cheers
    M


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •