PHP Read/Write
8 posts
Page 1 of 1
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 :
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
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<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData . $_POST["getdata"];
exit;
?>
Just edit this line:
Code: Select all
Replace "testFile.txt" with your file name.$myFile = "testFile.txt";
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
Do i look that stupid ? xD How do i send file name - request, i post "myname.txt" and it returns that file xD
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:
Or you can use the POST method, its similar, just change
(HTML FILE)
This -
This -
- Submit file name
Make a html file, inside it, add this:
Code: Select all
And for reading, you need a PHP file, call it "getfile.php"<form action="getfile.php" method="get">
File name: <input type="text" name="file" />
<input type="submit" />
</form>
Code: Select all
Using GET, the variables will be visible in the address bar, like http://www.yourlink.com/getfile.php?file=myfile.html<?php
$myFile = $_GET["file"];
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData . $_POST["getdata"];
exit;
?>
Or you can use the POST method, its similar, just change
(HTML FILE)
This -
Code: Select all
To - <form action="getfile.php" method="get">
Code: Select all
(PHP FILE)<form action="getfile.php" method="post">
This -
Code: Select all
To - $myFile = $_GET["file"];
Code: Select all
- CodexVideos$myFile = $_POST["file"];
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
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
How do i post to php file name and return content from specified file ? 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
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 ?
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 ?
I am making something myself, i mostly copied the code for new file, but it seems i can't use link like this :
Would something like this be possible ?
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;
?>
Sorry for 4th post in a row
I finally got it to work
Here is my ( 90%yours ) code :
'Write
'Login
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
'Read<?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;
?>
Code: Select all
<form action="getfile.php" method="get">
File name: <input type="text" name="file" />
<input type="submit" />
</form>
Code: Select all
VB.NET<?php
$myFile = $_GET["file"];
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData . $_POST["getdata"];
exit;
?>
'Login
Code: Select all
'Register 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
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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023