PHP Read/Write

8 posts Page 1 of 1
Contributors
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

PHP Read/Write
M1z23R
I started new topic here, in php section, since i am sure i'll figure out somehow about vb ;)

So, i found this code how to read files using php script. But, how can i read file with specified file name ?

My code :
Code: Select all
<?php 
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData . $_POST["getdata"];
exit;
 ?>
This returns the data from the file, but how can i "send" file name which i want to open ? I have CnSs example project on how to post data
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

Re: PHP Read/Write
Codex
Just edit this line:
Code: Select all
$myFile = "testFile.txt";
Replace "testFile.txt" with your file name.
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: PHP Read/Write
M1z23R
Do i look that stupid ? xD How do i send file name - request, i post "myname.txt" and it returns that file xD
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

Re: PHP Read/Write
Codex
You need 2 files, one for submitting the file name and one for reading content.

- Submit file name
Make a html file, inside it, add this:
Code: Select all
<form action="getfile.php" method="get">
File name: <input type="text" name="file" />
<input type="submit" />
</form>
And for reading, you need a PHP file, call it "getfile.php"
Code: Select all
<?php 
$myFile = $_GET["file"];
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData . $_POST["getdata"];
exit;
?>
Using GET, the variables will be visible in the address bar, like http://www.yourlink.com/getfile.php?file=myfile.html

Or you can use the POST method, its similar, just change
(HTML FILE)
This -
Code: Select all
<form action="getfile.php" method="get">
To -
Code: Select all
<form action="getfile.php" method="post">
(PHP FILE)
This -
Code: Select all
$myFile = $_GET["file"];
To -
Code: Select all
$myFile = $_POST["file"];
- CodexVideos
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: PHP Read/Write
M1z23R
I don't need a form :/ I want to get file from a VB.NET app, i have some code that has "Post" comand here it is:
Code: Select all
    Public PostData As New Dictionary(Of String, String)

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Get the data/values we want to send to our page
        PostData.Add(txtID.Text, txtValue.Text)

        'Now send the data to our page and read its response - displayed into the richtextbox
        Try
            Dim myResponseStream As IO.Stream = PostToSite("http://localhost/testscript.php", PostData).GetResponseStream()
            Dim myStreamReader As New IO.StreamReader(myResponseStream)
            RichTextBox1.Text = myStreamReader.ReadToEnd
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    'The main post function
    Public Function PostToSite(ByVal url As String, ByVal values As Dictionary(Of String, String)) As HttpWebResponse
        Dim request As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)
        Dim builder As New StringBuilder
        Dim value As KeyValuePair(Of String, String)
        For Each value In values
            builder.AppendFormat("{0}={1}&", value.Key, HttpUtility.UrlEncode(value.Value))
        Next
        Dim postBytes As Byte() = Encoding.UTF8.GetBytes(builder.ToString(0, (builder.Length - 1)))
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = postBytes.Length
        Using requestStream As Stream = request.GetRequestStream
            requestStream.Write(postBytes, 0, postBytes.Length)
        End Using
        Return TryCast(request.GetResponse, HttpWebResponse)
        PostData.Clear()
    End Function
How do i post to php file name and return content from specified file ?
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: PHP Read/Write
M1z23R
Oh wait, i can just simply put those two scripts, and i can make simple web request, from link like this "http://localhost/getfile.php?file=TestFile.txt" and it will returns the text ;) Thanks a lot man +rep

Now i need a way to create new txt file ;) If it is not much trouble ?
I need to create new txt file with certain info that i post from my app ?
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: PHP Read/Write
M1z23R
I am making something myself, i mostly copied the code for new file, but it seems i can't use link like this :

Code: Select all
http://localhost/makenewfile.php?file=testfile.txt?testtext=TEXT

Would something like this be possible ?
Code: Select all
<?php 
$myFile = $_GET["file"];
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_GET["testtext"];
fwrite($fh, $stringData);
fclose($fh);
echo $theData . $_POST["getdata"];
exit;
?>
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: PHP Read/Write
M1z23R
Sorry for 4th post in a row :D
I finally got it to work :)

Here is my ( 90%yours ) code :

'Write
Code: Select all
<form action="newfile.php" method="get">
File name: <input type="text" name="file" />
<input type="submit" />
</form>
Code: Select all
<?php 
$myFile = $_GET["file"];
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_GET["password"];
fwrite($fh, $stringData);
fclose($fh);
echo $theData . $_POST["getdata"];
exit;
?>
'Read
Code: Select all
<form action="getfile.php" method="get">
File name: <input type="text" name="file" />
<input type="submit" />
</form>
Code: Select all
<?php 
$myFile = $_GET["file"];
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData . $_POST["getdata"];
exit;
?>
VB.NET

'Login
Code: Select all
        Dim theRequest As WebRequest = WebRequest.Create("http://localhost/getfile.php?file=" + txtUserName.Text + ".txt")
        Dim theResponse As WebResponse = CType(theRequest.GetResponse, HttpWebResponse)
        Dim theReader As New IO.StreamReader(theResponse.GetResponseStream())
        Dim theContent As String = theReader.ReadToEnd()
        theReader.Close()
     
        If theContent = txtPassword.Text Then
            MsgBox("Succsess")
        Else
            MsgBox("Wrong password. Try again !")
        End If
'Register
Code: Select all
 
  Dim theRequest As WebRequest = WebRequest.Create("http://localhost/makenewfile.php?file=" + txtNewUser.Text + ".txt&password=" + txtNewPassword.Text)
        Dim theResponse As WebResponse = CType(theRequest.GetResponse, HttpWebResponse)
        Dim theReader As New IO.StreamReader(theResponse.GetResponseStream())
        Dim theContent As String = theReader.ReadToEnd()
        theReader.Close()
8 posts Page 1 of 1
Return to “Help & Support”