I have created a small script to add a string to a text file, but it does not seem to work.
<?php
$email = $_POST["email"];
$name = $_POST["name"];
$city = $_POST["city"];
// Concatenate all values into one string.
$subscription = strtolower(trim($email)) . "," . ucwords(trim($name)) . "," . ucwords(trim($city));
$file = "21_nieuwsbrief.txt";
file_put_contents($file, (string)$subscription, FILE_APPEND);
echo $subscription;
?>
When testing this on the server, the echo $subscription shows the correct output.
But when I go to 21_nieuwsbrief.txt, it does not show any lines of text.
Could someone help me out here?
Thanks in advance.
Related
I have two completely similar codes that one of them works and the other doesnt
$id = 1;
$ext = pathinfo($_FILES['adminProfilePicture']["name"], PATHINFO_EXTENSION);
$address = '../img/admin' . $id . 'image.' . $ext;
move_uploaded_file($_FILES['adminProfilePicture']["tmp_name"],$address);
this code works perfectly and saves the file in the directory root/img/admin1image.png
$id = 1;
$ext = pathinfo($_FILES['teamProfilePicture']["name"], PATHINFO_EXTENSION);
$address = '../img/team' . $id . 'image.' . $ext;
move_uploaded_file($_FILES['teamProfilePicture']["tmp_name"],$address);
but this code for some reason sets the address as the name of the file
and saves a file with this name (../img/team1image.png) in the directory root/(../img/team1image.png)
i really dont know what could be wrong
any help would be appreciated
and one other thing is the code works on my localhost
but iam making a project for someone else and this problem occurs on his host
change the address
$address = '../img/team' . $id . 'image.' . $ext;
to
$address = realpath(__DIR__.'/../img/').'team'.$id.'image.'.$ext;
I'm trying to write a basic error form to a file, but when I submit the file my PHP always crashes. I've tried some basic error fixes such as whether or not the if statement is occurring, if all the variables I'm using are not null, etc...
In my code all the string variables are valid an I am 100% certain they are not causing the error.
I've reduced it down through error testing to be the lines
$destination = fopen($filename,"w");
and
$fclose($destination);
My code:
if(isset($_POST['submit']) && $_POST['name'] != "" && $_POST['email'] != ""){
$filename = "message_" . date('Y-m-d H:i:s') . ".txt";
$issueType = "Type of issue: " . $_POST["issues"] . "\r\n";
$submitter = "Submitted by: " . $_POST["name"];
$email = "Email ID: " . $_POST["email"];
$time = "Submitted at: " . date('Y-m-d H:i:s') . "\r\n";
$message = "Details: " . $_POST["details"];
$destination = fopen($filename,"w");
fwrite($destination,"Issue Alert!");
fwrite($destination,$issueType);
fwrite($destination,$submitter);
fwrite($destination,$email);
fwrite($destination,$time);
fwrite($destination,$message);
$fclose($destination);
}
Any other code I'm implementing is irrelevant as I've cut off those as being points of error.
When I try this with basic, hard-coded strings I don't get any errors. All help is appreciated, Thanks!
Try to change your $filename, as you wrote
$filename = "message_" . date('Y-m-d H:i:s') . ".txt";
because you can't use colon ( ':' from 'H:i:s') in file name.
maybe you can try this:
$filename = "message_" . date('U') . ".txt";
you still have date-time information in your file name
But remember, if you have big traffic on your site, maybe you will get same file name in one second, and the previous existing file will overwrited
I have some PHP on my 'website' that I am using to send some data from a webform into a csv file.
It is working, but the field called 'primary_classification' contains some text which have commas in. With those classifications, it is splitting the text across two cells in the csv.
This is the PHP:
<?php
$keys = array('user_name', 'gr_num', 'primary_classification');
$csv_line = array();
foreach($keys as $key){
array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'test.csv';
$csv_line = implode(',',$csv_line);
if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}
$fcon = fopen($fname,'a');
$fcontent = $csv_line;
fwrite($fcon,$csv_line."\n");
fclose($fcon);
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('The classification has been added!')
window.location.href='http://localhost:8080/classification.html';
</SCRIPT>");
?>
This is the output from the csv when using 'Individual behaviour' (working), and 'Education, learning and skills' (not working).
Does anyone know where I am going wrong?
Thanks
I am pretty sure it is enough if you wrap your fields in quotes, so simply change:
array_push($csv_line,'' . $_GET[$key]);
into
array_push($csv_line, '"' . $_GET[$key] . '"');
Hello guys the following code will add only one line of text to my 'file.txt' whenever i m
going to add more text so its replacing the old one, i want to have the old one with new also
Beside this one i want to separate the words in my 'txt.file' by a comma and space.
Here is my code:
<?php
$Fname = $_POST['fname'];
$email = $_POST['email'];
$Phone = $_POST['number'];
$date = $_POST['date'];
$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
fwrite($handle, $Fname);
fwrite($handle, $email);
fwrite($handle, $Phone);
fwrite($handle, $date);
?>
Because you are using write mode (w). To add lines to the end, you have to append (a):
$handle = fopen($my_file, 'a');
To separate words by comma and space, join it into one string in PHP and than save it:
$finalString = $Fname . ', ' . $email . ', ' . $Phone . ', ' . $date;
fwrite($handle, $finalString );
//or you can use file_put_content to add to end of each line
$my_file = 'file.txt';
file_put_content($my_file, $Fname, PHP_EOL, PHP_APPEND);
file_put_content($my_file, $emai, PHP_EOL, PHP_APPEND);
file_put_content($my_file, $phone, PHP_EOL, PHP_APPEND);
Ok sorry if its a stupid question im a begginer.
Im making a small shoutbox just for practise.
It inserts the shout infos in a txt file.
My problem is that, it lists the text from top to bottom, and i would like to do this reversed.
if(isset($_POST['submit'])) {
$text = $_POST['text'];
if(!empty($text)) {
$text = $_POST['text'];
$name = $_POST['name'];
$time = date("H:i");
$content =
"<div class='text'><em>" . $time . "</em>
<span class='c11'><b>" . "<a href='userinfo_php_willbe_here.php' target='_blank'>" . htmlspecialchars($name) . "</a>" . ":</span></b>
" . htmlspecialchars($text) . "
</div>\n";
file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
}
}
here is my code.
i was googleing around with not much luck maybe i wasnt looking hard enough.
could please someone give me a hint?
thank you
No way to do so with one function call. You need to read the content from your target file, prepend the data in php and rewrite the whole file (see file_get_contents).
$fileContent = file_get_contents($file);
$fileContent = $content . $fileContent;
file_put_contents($file, $fileContent, LOCK_EX);
You can also use the array_reverse like so:
// Data in file separated by new-line
$data = explode("\n",file_get_contents("filename.txt"));
foreach(array_reverse($data) as $value) {
echo $value."\n";
}
You can only prepend to a file by means of reading it and writing afterwards.
file_put_contents($file, $content . file_get_contents($file), LOCK_EX);