(Hopefully) simple conversion needed

Last post 12-06-2006 12:52 AM by pickyh3d. 4 replies.

Sort Posts:

  • (Hopefully) simple conversion needed

    10-30-2006, 7:39 AM
    • Contributor
      2,030 point Contributor
    • SamTanner
    • Member since 02-08-2004, 7:04 PM
    • US Gulf Coast
    • Posts 406

    Can someone please do a simple conversion from PHP for me?

    It just writes to and saves a file.

    <code>
    <?php


    $filename = $HTTP_POST_VARS["filename"];
    $content = stripslashes($HTTP_POST_VARS["content"]);

    // In our example we're opening $filename in write mode.
    // The file pointer is at the begining of the file
    if (!$fp = fopen($filename, 'w+')) {
            echo("flag=file is readonly");
            exit;
    }
    // Write $content to our opened file.
    if (!fwrite($fp, $content)) {
           echo("flag=Cannot write to file".($filename));
           exit;
    }
    echo("flag=File is saved");

    fclose($fp);


    ?>
    </code>

    Thanks in advance,

    Sam Tanner

  • Re: (Hopefully) simple conversion needed

    10-30-2006, 7:55 AM

    You can use something like this

    Dim myfile As IO.StreamWriter = IO.File.CreateText("c:\test.txt")
            myfile.Write("Hello World!!!")
            myfile.Close()

     

    This example will create a file and will write Hello world in that file.

    For more info, check the MSDN about the System.IO namespace 

     

    HTH
    Regards

  • Re: (Hopefully) simple conversion needed

    10-30-2006, 10:22 AM
    • Contributor
      2,030 point Contributor
    • SamTanner
    • Member since 02-08-2004, 7:04 PM
    • US Gulf Coast
    • Posts 406
    Well, no... the php code writes my "content" (from a Flash ui) to an existing file and saves it. Your code creates a new file.
    Thanks, though.
  • Re: (Hopefully) simple conversion needed

    12-06-2006, 12:45 AM
    • Star
      9,676 point Star
    • pickyh3d
    • Member since 09-17-2002, 6:19 AM
    • Virginia
    • Posts 1,955

    It's pretty bad idea to trust a post variable for a filename, but I'll let that go for the sake of the example.

    Also, it might be worth noting that stripslashes is not a good function to use in PHP, especially if magic quotes are off.

    // filename for the file (should ensure that it's only writing to
    //    where you want it to write the file too...
    string strFilename = txtFilename.Text;
    // text for the file
    string strContent = txtContent.Text;
    
    // used to write the new file
    System.IO.StreamWriter swFile = null;
    
    // catch any errors
    try
    {
        // overwrite the existing file
        swFile = new System.IO.StreamWriter(strFilename, false);
    
        // write the data to the file
        swFile.Write(strContent);
    }
    catch ( Exception ex )
    {
        // do stuff with the exception--you may also want to check
        //   the IOException object
    }
    finally
    {
        // close the file if it was succesfully opened
        if ( swFile != null )
        {
            swFile.Close();
        }
    }
    Picky
  • Re: (Hopefully) simple conversion needed

    12-06-2006, 12:52 AM
    • Star
      9,676 point Star
    • pickyh3d
    • Member since 09-17-2002, 6:19 AM
    • Virginia
    • Posts 1,955

    I just noticed his reply was in VB.NET, so if you want that, then here:

      

    ' filename for the file (should ensure that it's only writing to
    '    where you want it to write the file too...
    Dim strFilename As String = txtFilename.Text
    ' text for the file
    Dim strContent As String = txtContent.Text
    
    ' used to write the new file
    Dim swFile As System.IO.StreamWriter = Nothing
    
    ' catch any errors
    Try
    
        ' overwrite the existing file
        swFile = new System.IO.StreamWriter(strFilename, false);
    
        ' write the data to the file
        swFile.Write(strContent)
    
    Catch ex As Exception
    
        ' do stuff with the exception--you may also want to check
        '   the IOException object
    
    Finally
    
        ' close the file if it was succesfully opened
        If ( Not swFile Is Nothing )
    
            swFile.Close()
    
        End If
    
    End Try

    Picky
Page 1 of 1 (5 items)