fwrite function writes nothing to the file - php

I am trying to write data into a txt file, but It won't work. there is no error. it just won't work.
here is the code
if (isset($_REQUEST['submit']))
{
$hour = $_REQUEST['hour'];
$family = $_REQUEST['family'];
$how = $_REQUEST['how'];
$will = $_REQUEST['will'];
$fun = $_REQUEST['fun'];
$kind = $_REQUEST['kind'];
$device = $_REQUEST['device'];
$study = $_REQUEST['study'];
$agree = $_REQUEST['agree'];
$text = "hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind : " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />" ;
fopen('/1.txt', "r");
fwrite('/1.txt',"hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind : " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");
fclose('/1.txt');
}
do note that there is an "else" that includes all the aforementioned input fields that I used in the code , and that I am executing on localhost.
thanks

You cannot write to it since you are opening it as read-only:
$handle = fopen('/1.txt', "r");
Instead:
$handle = fopen('/1.txt', "w"); // to write only, if you need to read and write use 'w+'
You also need to store fopen in a $handle so you can write to it later.
Documentation: http://www.php.net/manual/en/function.fopen.php
Now the first parameter for fwrite should be $handle:
fwrite($handle, "hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind : " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");
Documentation: http://www.php.net/manual/en/function.fwrite.php
At the end you should also close with $handle:
fclose($handle);
Documentation: http://www.php.net/manual/en/function.fclose.php

You are opening the file as read-only, with the ruse w instead. And you need to pass fwrite the return of fopen as first argument, not the filename:
$f = fopen('/1.txt', "w");
fwrite($f,"hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind : " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");
fclose($f);

You have to pass in a 'w' to write to a file.
$handle = fopen('/1.txt', "w");
Passing in 'r' makes it so you are opening a 'read-only' file.

Much simpler:
file_put_contents('/1.txt', $text);

Related

HTML & PHP Information Table

So i'm hosting servers and trying to make a table displaying the information server name players etc i have a script that displays this but i suck at css, What i'm trying to get is a little like in this website http://echelon.kthx.at/pubbans.php
<?php
include ("BC2Conn.php");
// opens a connection to gameserver
$BC2Conn = new BC2Conn("31.185.143.136", 48888);
if (!$BC2Conn->isConnected()) {
echo "Connection could not be established. " .
"To debug, set '-d' as 3rd parameter to new connection.<br />" .
"<br />" .
"Example: \$BC2Conn = new BC2Conn(\"127.0.0.1\", 48888, \"-d\");";
return 0; // stop executing this script
}
// secure login
// $BC2Conn->loginSecure("password");
// unsecure login (not salted)
// some random serverinformation
echo "Servername: " . $BC2Conn->getServerName() . "<br />";
echo "Players: " . $BC2Conn->getCurrentPlayers() . "/" . $BC2Conn->getMaxPlayers() . "<br />";
echo "Playmode: " . $BC2Conn->getCurrentPlaymodeName() . "<br />";
echo "Current Map: " . $BC2Conn->getCurrentMapName() . "<br /><br /><br /><u>Players:</u><br /><br />";
// playerlist
$playerNames = $BC2Conn->getPlayerlistNames();
foreach ($playerNames as $key => $content) {
if ($BC2Conn->getPlayerClantag($content) != "") {
echo "[" . $BC2Conn->getPlayerClantag($content) . "]";
}
echo " " . $BC2Conn->getPlayername($content) . " - Kills: ";
echo $BC2Conn->getPlayerKills($content) . " | Deaths: ";
echo $BC2Conn->getPlayerDeaths($content) . " | Score: ";
echo $BC2Conn->getPlayerScore($content) . "<br />";
}
// logout
$BC2Conn->logout();
?>

How to configure FFMPEG with Xampp in Ubuntu

Hi i am i trying to merge videos using ffmpeg with Php.Now i installed Xampp in ubuntu.and FFMEPG also i installed in Ubuntu.I searched ffmpeg.dll file but its not dound in the directory.but i add the extension in the php.ini file like below my code
;extension=php_bz2.dll
;extension=php_ffmpeg.dll
;extension=php_curl.dll
;extension=php_dba.dll
;extension=php_exif.dll
;extension=php_fileinfo.dll
This is my test code for ffmpeg.
<?php
extension_loaded('ffmpeg') or die('Error in loading ffmpeg');
$vid = realpath('1.mp4');
$ffmpegInstance = new ffmpeg_movie($vid);
echo "getDuration: " . $ffmpegInstance->getDuration() . "<br />".
"getFrameCount: " . $ffmpegInstance->getFrameCount() . "<br />".
"getFrameRate: " . $ffmpegInstance->getFrameRate() . "<br />".
"getFilename: " . $ffmpegInstance->getFilename() . "<br />".
"getComment: " . $ffmpegInstance->getComment() . "<br />".
"getTitle: " . $ffmpegInstance->getTitle() . "<br />".
"getAuthor: " . $ffmpegInstance->getAuthor() . "<br />".
"getCopyright: " . $ffmpegInstance->getCopyright() . "<br />".
"getArtist: " . $ffmpegInstance->getArtist() . "<br />".
"getGenre: " . $ffmpegInstance->getGenre() . "<br />".
"getTrackNumber: " . $ffmpegInstance->getTrackNumber() . "<br />".
"getYear: " . $ffmpegInstance->getYear() . "<br />".
"getFrameHeight: " . $ffmpegInstance->getFrameHeight() . "<br />".
"getFrameWidth: " . $ffmpegInstance->getFrameWidth() . "<br />".
"getPixelFormat: " . $ffmpegInstance->getPixelFormat() . "<br />".
"getBitRate: " . $ffmpegInstance->getBitRate() . "<br />".
"getVideoBitRate: " . $ffmpegInstance->getVideoBitRate() . "<br />".
"getAudioBitRate: " . $ffmpegInstance->getAudioBitRate() . "<br />".
"getAudioSampleRate: " . $ffmpegInstance->getAudioSampleRate() . "<br />".
"getVideoCodec: " . $ffmpegInstance->getVideoCodec() . "<br />".
"getAudioCodec: " . $ffmpegInstance->getAudioCodec() . "<br />".
"getAudioChannels: " . $ffmpegInstance->getAudioChannels() . "<br />".
"hasAudio: " . $ffmpegInstance->hasAudio();?>
When i run this in xammp the output coming like this
Error in loading ffmpeg
kindly give some solution for me
thanks in advance.

GeoIP not showing all info

so i have this error , i downloaded the lastest
geoipcity.inc | geoipregionvars.inc | GeoLiteCity.dat
so here is my code :
<?php
/**
* Querying against GeoIP/Lite City
* This will fetch country along with city information
*/
include("geoipcity.inc");
include("geoipregionvars.php");
$giCity = geoip_open("GeoLiteCity.dat",GEOIP_STANDARD);
$ip = "99.99.99.99";
$record = geoip_record_by_addr($giCity, $ip);
echo "Getting Country and City detail by IP Address <br /><br />";
echo "IP: " . $ip . "<br /><br />";
echo "Country Code: " . $record->country_code . "<br />" .
"Country Code3: " . $record->country_code . "<br />" .
"Country Name: " . $record->country_name . "<br />" .
"Region Code: " . $record->region . "<br />" .
"City: " . $record->city . "<br />" .
"Postal Code: " . $record->postal_code . "<br />" .
"Latitude: " . $record->latitude . "<br />" .
"Longitude: " . $record->longitude . "<br />" .
"Metro Code: " . $record->metro_code . "<br />" .
"Area Code: " . $record->area_code . "<br />" ;
geoip_close($giCity);
?>
and here is my problem that
not all resulst are shown :
Country Code: SA
Country Code3: SA
Country Name: Saudi Arabia
Region Code:
City:
Postal Code:
Latitude: 25
Longitude: 45
Metro Code:
Area Code:

Showing Array data

I have a foreach below that when print_r or var_dumped it shows the correct data but once I put the $finalMenuDetails into the location I need it shows Array and not the list.
Var_dump
array(3) {
[0]=>
string(6) "Item 1"
[1]=>
string(6) "item 2"
[2]=>
string(6) "item 3"
}
array(3) {
[0]=>
string(2) "23"
[1]=>
string(2) "90"
[2]=>
string(2) "23"
}
PHP
foreach($menuDetailsItem as $key => $value)
{
$finalMenuDetails[] = $value. " ".$amount[$key] . '<br/>';
}
Process:
$menuName = $_POST['menu'];
$menuDetailsItem = $_POST['item'];
$menuDetailsItemAmount = $_POST['amount'];
$menuResult = '';
foreach ($menuDetailsItem as $key => $value) {
$menuResult .= $value." ".$menuDetailsItemAmount[$key]."<br/>";
}
if(empty($errorMessage))
{
$to = "";
$subject = "Booking";
$headers = "From: " . strip_tags($_POST['Email']) . "\r\n" . "Reply-To: ". strip_tags($_POST['Email']) . "\r\n" . "Content-Type: text/html; charset=ISO-8859-1\r\n" . //header("Location: http://redherringcatering.co.nz/Thankyou.html");
$message = "\n" . "<strong>NAME:</strong> ". $varName . "<br />" . "<br />" . "<strong>CONTACT:</strong> " . $varContact . "<br />" . "<br />" . "<strong>EMAIL:</strong> ". $varEmail . "<br />" . "<br />" ."<strong>COMPANY:</strong> ". $varCompany . "<br />" . "<br />" . "<strong>No. ATTENDING:</strong> ".$varAttending . "<br />" . "<br />" ."<strong>FUNCTION DATE:</strong> ". $varFunction . "<br />" . "<br />" ."<strong>FUNCTION DAY:</strong> ". $varFunctionDay . "<br />" . "<br />" ."<strong>DATE ORDERED:</strong> ". $varOrdered . "<br />" . "<br />" ."<strong>TIME REQUIRED:</strong> ". $varTime . "<br />" . "<br />" . "<strong>ONSITE:</strong> ". $varOnsite . ", ". $varOnsite2 . "<br />" . "<br />" ."<strong>INVOICE TO:</strong> ". $varInvoice . "<br />" . "<br />" ."<strong>ADDRESS:</strong>\n". $varAddress . "<br />" . "<br />" ."<strong>SPECIAL NEEDS:</strong> ". $varSpecial . "<br />" . "<br />" . "<strong>ORDER DETAILS:</strong><br/><br/>". $menuName . "<br/>"."<br/>"."<strong>Items:</strong><br/><br/>".print_r($menuResult) . "\n";
mail($to,$subject,$message,$headers);
exit;
}
Try something like this:
$item = array("item 1","item 2","item 3") ;
$amount = array("23","90","23") ;
$result_array = '';
foreach ($item as $key => $value) {
$result_array .= $value." ".$amount[$key]."<br/>";
}
print_r($result_array);
OUTPUT
item 1 23
item 2 90
item 3 23

How to obtain latitude and longitude of a user by IP address or pinpoint location

I want to get the geolocation (latitude and longitude) of a computer user by a php script. I was using this one
<?php
// Get lat and long by address
$address = $dlocation; // Google HQ
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$latitude = $output->results[0]->geometry->location->lat;
$longitude = $output->results[0]->geometry->location->lng;
?>
but the problem is that this uses real adresses and I just want the user to log in, ask him for the permission to track him (allow location) and if he accepts obtain the desired values (longitude and latitude). It could be by an IP adress or another way to pinpoint his location but the thing is I wanted this method to be pratically flawless regarding proxies and fakies. Any idea on how I could do this? Thank you very much!
I think this is what you want.simple and easy to use.Thanks to HTML5 see source here
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
download geoip.inc - http://www.maxmind.com/download/geoip/api/php-20120410/geoip.inc,
geoipcity.inc - http://www.maxmind.com/download/geoip/api/php-20120410/geoipcity.inc,
geoipregionvars.php - http://www.maxmind.com/download/geoip/api/php-20120410/geoipregionvars.php,
GeoLiteCity.dat - http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
please convert GeoLiteCity.dat.gz to GeoLiteCity.dat and
put in geoip named folder
include("application/libraries/geoip/geoipcity.inc");
include("application/libraries/geoip/geoipregionvars.php");
$giCity = geoip_open("application/libraries/geoip/GeoLiteCity.dat", GEOIP_STANDARD);
$ip =$_SERVER['REMOTE_ADDR'];
$record = geoip_record_by_addr($giCity, $ip);
echo "Getting Country and City detail by IP Address <br /><br />";
echo "IP: " . $ip . "<br /><br />";
echo "Country Code: " . $record->country_code . "<br />" .
"Country Code3: " . $record->country_code . "<br />" .
"Country Name: " . $record->country_name . "<br />" .
"Region Code: " . $record->region . "<br />" .
"Region Name: " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "<br />" .
"City: " . $record->city . "<br />" .
"Postal Code: " . $record->postal_code . "<br />" .
"Latitude: " . $record->latitude . "<`enter code here`br />" .
"Longitude: " . $record->longitude . "<br />" .
"Metro Code: " . $record->metro_code . "<br />" .
"Area Code: " . $record->area_code . "<br />" ;

Categories