Forum Moderators: coopster
For example, say you want to remove line 5 and you are passing the info via a query string:
index.php?id=5
//read the contents of the file into an array
$file = file('myfile.txt');
//Loop through reading all lines except the one you want to delete
for ($i=0; $i<count($file); $i++)
{if ($i!=$_GET['id']-1)
{
$data .= $file[$i]."\n";
}}
Then delete the contents of your existing file and write the contents of $data back to the file, which will include the same data with the line you want removing missing. Something like that should do. The first slot in the array is 0, so to delete line 5, its 5-1 which is the 4th slot. Hope that makes sense.
dc
Thanks very much.
I have tried to implement it only it comes up with errors:
Warning: fopen() expects parameter 1 to be string, array
I am getting the id from a link.
Here is my code:
if($_GET["action"] == "delete") {
$file = file('events.txt');
//Loop through reading all lines except the one you want to delete
for ($i=0; $i<count($file); $i++)
{
if ($i!=$_GET['id']-1)
{
$data .= $file[$i]."\n";
}
}
$blank = "blank";
$fp = fopen($file,"w");
fwrite($fp,$blank); // Delete all the file
fclose($fp);
$fp2 = fopen($file,"a");
fwrite($fp2,$data); // Write information to the file
fclose($fp2);
}
[uk.php.net...]
$file = file('myfile.txt');
$data = '';for ($i=0; $i<count($file); $i++)
{if (!strstr($file[$i],$_GET['id'])
{
$data .= $file[$i]."\n";
}}
//Then delete the original file
if (file_exists('myfile.txt'))
{
unlink('myfile.txt');
}//and write the new data
$fp = fopen('myfile.txt',"w");
if ($fp)
{
fwrite($fp,$data);
fclose($fp);
}
dc
It sort of worked. It deleted the name I think, but nothing else. I am not sure it formatted correctly when writing it back.
Here is my posting events workings page:
<?
#####################
#
# Events compose
#
#####################
session_start();
require_once("includes/config.php");
if($_SESSION["valid"] == true)
{
if($_POST["action"] == "send")
{
if ($_POST["imagefile"]!= "") {
if (($_FILES['imagefile']['type'] == "image/jpeg")¦¦($_FILES['imagefile']['type'] == "image/gif"))
{
copy ($_FILES['imagefile']['tmp_name'], "gallery/".$_FILES['imagefile']['name'])
or die ("Could not copy");
}
else
{
echo "
<br>
<br>
";
echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")
<br>
";
}
}
$title = $_POST["title"];
$timearrive = $_POST["timearrive"];
$date_start = $_POST["date_start"];
$date_finish = $_POST["date_finish"];
$about = $_POST["about"];
$status = $_POST["status"];
$imageornot = $_POST["chkbox"];
$image = $_FILES['imagefile']['name'];
$id = rand(1000, 9999);
$filename = 'events.txt';
$pipe = "¦";
$cr = "\n";
$data1 .= $title . $pipe . $timearrive . $pipe . $date_start . $pipe . $date_finish . $pipe . $about . $pipe . $id . $pipe . $status;
if ($imageornot =="y") {
$data2 .= $pipe . $imageornot . $pipe . $image;
}
$data3 .= $cr;
$dataall = $data1 . $data2 . $data3;
$fp = fopen($filename,"a");
if($fp){
fwrite($fp,$dataall); // Write information to the file
fclose($fp); // Close the file
Here is the events delete page with your code implemented:
if($_GET["action"] == "delete") {
$file = file('data/events.txt');
$data = '';
for ($i=0; $i<count($file); $i++)
{
if (!strstr($file[$i],$_GET['id']))
{
$data .= $file[$i]."\n";
}
}
//Then delete the original file
if (file_exists('events.txt'))
{
unlink('events.txt');
}
//and write the new data
$fp = fopen('events.txt',"w");
if ($fp)
{
fwrite($fp,$data);
fclose($fp);
}}
It gives a permission denied warning for unlink but it still deletes the information...
Another weird thing is that when there are 2 entries and I click delete of one, it deletes it fine (with the warning) only when I visit the events page I can see the "Delete" link of the one I deleted with no name before it.
The events.txt file shows that there is no entry there but there is a space. When I delete the space, it doesn't show the "delete" link for the entry - it has gone.
Any ideas?
check the permissions/owner on the file, then compare that with the user that php runs as
I would fix your warning first then look at the rest as there may be some connection. I don't quite understand the other problem but it almost sounds as if the data is gone but you are leaving a carriage return/newline so that you get a blank showing up.