how to specify path to save spread sheet file in php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pinocchio123
    New Member
    • Mar 2009
    • 6

    how to specify path to save spread sheet file in php?

    i want to store data ie available in a webpage in a spread sheet...

    i am creating an empty excel file using..
    $excel = new Spreadsheet_Exc el_Writer('hard ware.xls');

    but... i dont know how to specify the path to store the file in a partiucular path...
    i need to have a 'save as' button.. by clicking tat button it should facilitate the user to store the file in tat path..

    friends can u help me??


    thank u...
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, pinocchio123. Welcome to Bytes!

    A path is simply a string that uses '/' or '\' characters to separate folders. All you need to do is provide the User with a text input so that he can specify the path.

    The tricky part is not collecting the path; it is validating the path to make sure that the User did not (accidentally or maliciously) specify an unsafe path.

    For example:

    Code:
    $path = $_POST['path'];
    
    $excel = new Spreadsheet_Excel_Writer($path . '/hardware.xls');
    Most of the time, the results of this code are exactly what one would expect, but you should do some extra validation just to be sure:

    Code:
    $basePath = '/path/to/safe/directory';
    $subPath  = $_POST['path'];
    
    $target = realpath($basePath . '/' . $subPath);
    
    /* Check to make sure the User didn't try to move out of the safe path. */
    if( strpos($target, $basePath) === false )
    {
      throw new Exception("'{$subPath}' is an invalid path!");
    }
    
    $excel = new Spreadsheet_Excel_Writer($target . '/hardware.xls');

    Comment

    • pinocchio123
      New Member
      • Mar 2009
      • 6

      #3
      thank you very much for ur suggestion.. i got it... :)

      Comment

      Working...