for writing i using below code this is very nice and simple work great now i have problem with this we already know that in window 260 character allow for file name problem start from here in code there is option if file not write stop and terminate process (or die("can't open file");) now i want simple if some reason file not write show echo not write and process continue.I try it myslef but it gives me error
my write function
function write($post, $myFile){
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $post);
fclose($fh);
}
If there's a limit on the length of something then you could always check the length of the thing beforehand, and if you don't want your code to exit, then don't use die().
function write($post,$myFile){
if( strlen($myfile) > 260 ) {
echo "Filename too long";
return false;
} else {
if( ! $fh = fopen($myFile, 'a+') ) {
echo "can't open file";
return false;
}
fwrite($fh, $post);
fclose($fh);
return true;
}
}
Try this,
function write($post,$myFile)
{
$fh = #fopen($myFile, 'a+');
if($fh)
{
fwrite($fh, $post);
fclose($fh);
}
else
{
echo "can't open file";
}
}
Related
I'm trying to create a text file-based sequential url rotator by using the following code (original source):
<?php
$linksfile ="urlrotator.txt";
$posfile = "pos.txt";
$links = file($linksfile);
$numlinks = count($linksfile);
$fp = fopen($posfile, 'r+') or die("Failed to open posfile");
flock($fp, LOCK_EX);
$num = fread($fp, 1024);
if($num<$numlinks-1) {
fwrite($fp, $num+1);
} else {
fwrite($fp, 0);
}
flock($fp, LOCK_UN);
fclose($fp);
header("Location: {$links[$num]}");
?>
After my testing, I found that it keeps appending new position number as a string after the existing content of pos.txt and thus the script only works at the first time.
I tried adding the following line of code before the if else statement in order to clear the existing content of pos.txt before updating it.
file_put_contents($posfile, "");
But then error ocurred at the line of redirection saying Undefined index: ...... .
Where could possibly go wrong?
I believe your problem comes from the mode you are using. When you say
After my testing, I found that it keeps appending new position number as a string after the existing content of pos.txt and thus the script only works at the first time.
it points me to the mode usage of fopen.
The mode you should use is w, as it will "replace" the content of what is inside pos.txt.
$fp = fopen($posfile, 'w') or die("Failed to open posfile");
Doing so will prevent your from accessing what was in pos.txt. So you need to have something like this:
$num = file_get_contents($posfile);
$fp = fopen($posfile, 'w') or die("Failed to open posfile");
flock($fp, LOCK_EX);
if($num<$numlinks-1) {
fwrite($fp, $num+1);
} else {
fwrite($fp, 0);
}
flock($fp, LOCK_UN);
fclose($fp);
I have a function to write to a .txt file. my problem is that it writes only on the first line, so erases the old line for the new line. I want to append the new lines to the file.
function logit($log,$filename = ''){
$logfile = "log/".date('Ymd').$filename.".txt";
if ($fh = fopen($logfile, 'w')) {
fwrite($fh, date('H:i:s')." | ".$log."\n");
fclose($fh);
return 1;
} else
return false;
}
You need to search before post your question.. there's many documentation and answers for that problem.. anyway, replace this fopen($logfile, 'w') with fopen($logfile, 'a')
I want to create new .txt files but as this code is always returning false, the ajax success function is not executed.
the all code is:
<?php
$nome = $_POST["nome"];
$datanasc = $_POST["datanasc"];
$genero = $_POST["genero"];
$nat = $_POST["nat"];
$morada = $_POST["morada"];
$mail = $_POST["mail"];
$existe = false;
$myFile = "Users.txt";
$myFile1 = "Current_User.txt";
$fh = fopen($myFile, "r")or die("can't open file");
while (($line_of_text = fgets($fh))) {
$Data = explode(';', $line_of_text);
if($nome == $Data[0] && $datanasc == $Data[1] && $genero == $Data[2] && $nat == $Data[3] && $morada == $Data[4] && $mail == $Data[5]){
$existe = true;
break;
}
}
fclose($fh);
if($existe == true){
$arrayToJs["existe"] = $existe;
}
else{
$arrayToJs["existe"] = $existe;
$fh = fopen($myFile, "a")or die("can't open file");
$stringData = $nome.";".$datanasc.";".$genero.";".$nat.";".$morada.";".$mail.";"."\n";
//print_r($stringData);
fwrite($fh, $stringData);
fclose($fh);
$fh1 = fopen($myFile1, "w")or die("can't open file");
fwrite($fh1, $stringData);
fclose($fh1);
there is the problem in the code cause is returning false and the ajax success function is not executed. . .
if((!file_exists($nome.'_Favoritos.txt')) && (!file_exists($nome.'_Cesto.txt'))) {
$ffav = $nome.'_Favoritos.txt';
$handle = fopen($ffav, 'w') or die('Cannot open file: ');
fclose($ffav);
$fcart = $nome.'_Cesto.txt';
$handle = fopen($fcart, 'w') or die('Cannot open file: ');
fclose($fcart);
}
}
echo json_encode($arrayToJs);
?>
Thank you all guys!
Use the file pointer ($handle) you created with fclose:
if((!file_exists($nome.'_Favoritos.txt')) && (!file_exists($nome.'_Cesto.txt'))) {
$ffav = $nome.'_Favoritos.txt';
$handle = fopen($ffav, 'w') or die('Cannot open file: ');
fclose($handle);
$fcart = $nome.'_Cesto.txt';
$handle = fopen($fcart, 'w') or die('Cannot open file: ');
fclose($handle);
}
Otherwise your file will always return PHP error when those files do not exist
Whether or not your AJAX success function gets called has nothing to do with a PHP code's "return value."
Assuming you're using jQuery or one of the other JavaScript frameworks, it has to do with the HTTP response code. Presumably, you're probably encountering a PHP error which is resulting in a 500 response back to the browser. This would end you up in the error handler instead of the success handler.
Have you tried using something like the network inspector in Chrome (or the Net tab in Firebug) to investigate the actual HTTP response?
This code is not returning any value please pass value (true/false) after file created to ajax response.
If you pass a relative path to file_exists, it will return false unless the path happens to be relative to the current PHP directory.
here's what I've got so far - i really need to ban any tags from being entered as it's like a guestbook, but this doesn't seem to work:
<?php
$txt = $_POST['txt'];
//the data
$data = "
$txt";
//my attempt to implement a filter
var_dump(filter_var($data,FILTER_SANITIZE_SPECIAL_CHARS));
//open the file and choose the mode
$fh = fopen("users.txt", "a");
fwrite($fh, $data);
//close the file
fclose($fh);
header('Location: http://www.google.com/');
?>
You need to assign the returned value of filter_var
$data = filter_var($data,FILTER_SANITIZE_SPECIAL_CHARS);
filter_var can return FALSE if the filter fails. So, to be complete, you really should do something like:
$filtered_data = filter_var($data,FILTER_SANITIZE_SPECIAL_CHARS);
if($filtered_data !== FALSE) {
//write $filtered_data
} else {
//handle error
}
I want to fetch json script and write it to a txt file undecoded, exactly how it was originally. I do have a script that I use that I am modifying but unsure what to commands to use. This script decodes, which is what I want to advoid.
//Get Age
list($bstat,$bage,$bdata) = explode("\t",check_file('./advise/roadsnow.txt',60*2+15));
//Test Age
if ( $bage > $CacheMaxAge ) {
//echo "The if statement evaluated to true so get new file and reset $bage";
$bage="0";
$file = file_get_contents('http://somesite.jsontxt');
$out = (json_decode($file));
$report = wordwrap($out->mainText, 100, "\n");
//$valid = $out->validTo;
//write the data to a text file called roadsnow.txt
$myFile = "./advise/roadsnow.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $report;
fwrite($fh, $stringData);
}
else {
//echo the test evaluated to false; file is not stale so read local cache
//print "we are at the read local cache";
$stringData = file_get_contents("./advise/roadsnow.txt");
}
// if/else is done carry on with processing
//Format file
$data = $stringData
Try this:
// Get JSON Data
$json_data = file_get_contents('http://somesite.jsontxt');
// Write JSON to File
file_put_contents('json_data.txt', $json_data);