With formgenerator I created a form to upload files. So far so good.
For reasons I am not using a MySql database but make use of data.dat file (Ascii file) where records are being saved.
In the file delete_file_rec.php I have added some lines of code to implement new functionalitity, so that I am not only deleting a
selected record in the data.dat file but deleting the related file from the files directory as well. To realise this I use the standard
PHP function unlink(). To extract the needed filename from data.dat I use the PHP function explode() on the selected record
so I later can select the wanted filename in this record with the correct array value.
for($i=0;$i<sizeof($records);$i++) //This is still the excisting code that writes the records back to the data.dat file
{
if($i!=$id) //Note the $id is the value of the choosen record to delete
fwrite($file,$records[$i]);
else // This is the new added code to extract the filename
{
$columns = explode("|",$records[$id]);
//$filename = "TryMe.txt"; //for testing
$filename = $columns[sizeof($columns)-1];
echo "$filename"; //for testing
if(unlink("../files/$filename")) echo "The file $file is succesfull deleted";
}//else
}//for
Now the problem:
After uploading a new file using my uploadform the choosen file is copied to the ../files directory.
To delete this file I start up the administration form ../Admin/index.php and pick out the uploaded file and delete it.
Running the new added code -in the else loop- the unlink function generates an error.
However the echo displays the correct filename on the screen.
The oddest thing is that when I run the same code using $filename = "TryMe.txt"; intead of $filename =
$columns[sizeof($columns)-1]; things work fine.
To do this I copied a TryMe.Txt file to the "../files" directory first! After that I choose a random record to delete in the
administration. The result is that echo displays the correct filename and the file TryMe.Txt is deleted from the ../files
directory!!
Summarized:
$filename = "TryMe.txt"; - unlink runs fine
ref. output echo, echo:
===============================================
../files/TryMe.txtThe file Resource id #1 is succesfull deleted
===============================================
meijee, I have followed your lead and seem to have it working partially, however I have up to three files able to be uploaded via my form, how do I modify your code to allow for three files, also how do I get it to not show an error when a user has only uploaded one file and not three?
An error I am getting is
11_43_07_test.png The file Resource id #3 is succesfully deleted
Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/forms/filesup/admin/delete_file_rec.php:41) in /home/mysite/public_html/forms/filesup/admin/delete_file_rec.php on line 47
How or what is causing this?
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Try the following code to delete any number of files in a specific record.
// This is the new added code to extract the filename
$columns = explode("|",$records[$id]);
for($j=0;$j<sizeof($columns);$j++) {
$filename = $columns[$j];
$filename = trim($filename);
$filename = strip_tags($filename);
if(file_exists("../files/".$filename)&&($filename!="")) {
unlink("../files/$filename");
}
}
}
All this does is test every form entry for a specific record ID. It will only match file names that exist in the files folder and deletes all the matched files.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi All
I am having the same problem and have tried the above solutions with no joy.
Could someone please post the delete_file_rec.php script, so I can check I have everything right.
(Sorry, newbe with php)
tia
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Slam here again
I still have this problem.
I have tried the above solution, but without sucsess
Can someone please post the delete_file_rec.php with the changes above so I can check against mine?
Cheers
Slam
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
if($id=="") {
header("Refresh: 0;url=index.php");
} else {
$file = fopen("data.dat","r+") or die ("Could not open file!");
$garbage = fgets($file,4096);
$i=0;
while(!feof($file)) {
$temp = fgets($file,4096);
if($temp!="") {
$records[$i]=$temp;
$i++;
}
}
$top_line = $garbage;
ftruncate($file,0);
fseek($file,0);
fwrite($file,$top_line);
for($i=0;$i<sizeof($records);$i++) {
//This is still the excisting code that writes the records back to the data.dat file
//Note the $id is the value of the choosen record to delete
if($i!=$id) {
fwrite($file,$records[$i]);
} else {
// This is the new added code to extract the filename
$columns = explode("|",$records[$id]);
for($j=0;$j<sizeof($columns);$j++) {
$filename = $columns[$j];
$filename = trim($filename);
$filename = strip_tags($filename);
if(file_exists("../files/".$filename)&&($filename!="")) {
unlink("../files/$filename");
}
}
}
}
header("Refresh: 0;url=index.php");
}
?>
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
With formgenerator I created a form to upload files. So far so good.
For reasons I am not using a MySql database but make use of data.dat file (Ascii file) where records are being saved.
In the file delete_file_rec.php I have added some lines of code to implement new functionalitity, so that I am not only deleting a
selected record in the data.dat file but deleting the related file from the files directory as well. To realise this I use the standard
PHP function unlink(). To extract the needed filename from data.dat I use the PHP function explode() on the selected record
so I later can select the wanted filename in this record with the correct array value.
for($i=0;$i<sizeof($records);$i++) //This is still the excisting code that writes the records back to the data.dat file
{
if($i!=$id) //Note the $id is the value of the choosen record to delete
fwrite($file,$records[$i]);
else // This is the new added code to extract the filename
{
$columns = explode("|",$records[$id]);
//$filename = "TryMe.txt"; //for testing
$filename = $columns[sizeof($columns)-1];
echo "$filename"; //for testing
if(unlink("../files/$filename")) echo "The file $file is succesfull deleted";
}//else
}//for
Now the problem:
After uploading a new file using my uploadform the choosen file is copied to the ../files directory.
To delete this file I start up the administration form ../Admin/index.php and pick out the uploaded file and delete it.
Running the new added code -in the else loop- the unlink function generates an error.
However the echo displays the correct filename on the screen.
The oddest thing is that when I run the same code using $filename = "TryMe.txt"; intead of $filename =
$columns[sizeof($columns)-1]; things work fine.
To do this I copied a TryMe.Txt file to the "../files" directory first! After that I choose a random record to delete in the
administration. The result is that echo displays the correct filename and the file TryMe.Txt is deleted from the ../files
directory!!
Summarized:
$filename = "TryMe.txt"; - unlink runs fine
ref. output echo, echo:
===============================================
../files/TryMe.txtThe file Resource id #1 is succesfull deleted
===============================================
$filename = $columns[sizeof($columns)-1]; - unlink generates error.
ref. output echo, Warning:
================================================
../files/10_16_40_boef.gif
Warning: unlink() failed (Invalid argument) in c:\program files\easyphp\www\homepage\knmi-postbus\admin\delete_file_rec.php
on line 43
Is there sombody who can help me out??
In another newsgroup I got the clue.
$filename = trim($filename); // to remove unwanted spaces and/or newlines.
How simple live can be......
meijee, I have followed your lead and seem to have it working partially, however I have up to three files able to be uploaded via my form, how do I modify your code to allow for three files, also how do I get it to not show an error when a user has only uploaded one file and not three?
An error I am getting is
11_43_07_test.png The file Resource id #3 is succesfully deleted
Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/forms/filesup/admin/delete_file_rec.php:41) in /home/mysite/public_html/forms/filesup/admin/delete_file_rec.php on line 47
How or what is causing this?
Thanks
Email me a copy of your delete_file_rec.php file and I will see if I can duplicate your error.
Try the following code to delete any number of files in a specific record.
// This is the new added code to extract the filename
$columns = explode("|",$records[$id]);
for($j=0;$j<sizeof($columns);$j++) {
$filename = $columns[$j];
$filename = trim($filename);
$filename = strip_tags($filename);
if(file_exists("../files/".$filename)&&($filename!="")) {
unlink("../files/$filename");
}
}
}
All this does is test every form entry for a specific record ID. It will only match file names that exist in the files folder and deletes all the matched files.
Hi All
I am having the same problem and have tried the above solutions with no joy.
Could someone please post the delete_file_rec.php script, so I can check I have everything right.
(Sorry, newbe with php)
tia
Hi Slam here again
I still have this problem.
I have tried the above solution, but without sucsess
Can someone please post the delete_file_rec.php with the changes above so I can check against mine?
Cheers
Slam
<?php
// Global registers by pass
include("global.inc.php");
pt_register('GET','id');
// Start regular stuff
if($id=="") {
header("Refresh: 0;url=index.php");
} else {
$file = fopen("data.dat","r+") or die ("Could not open file!");
$garbage = fgets($file,4096);
$i=0;
while(!feof($file)) {
$temp = fgets($file,4096);
if($temp!="") {
$records[$i]=$temp;
$i++;
}
}
$top_line = $garbage;
ftruncate($file,0);
fseek($file,0);
fwrite($file,$top_line);
for($i=0;$i<sizeof($records);$i++) {
//This is still the excisting code that writes the records back to the data.dat file
//Note the $id is the value of the choosen record to delete
if($i!=$id) {
fwrite($file,$records[$i]);
} else {
// This is the new added code to extract the filename
$columns = explode("|",$records[$id]);
for($j=0;$j<sizeof($columns);$j++) {
$filename = $columns[$j];
$filename = trim($filename);
$filename = strip_tags($filename);
if(file_exists("../files/".$filename)&&($filename!="")) {
unlink("../files/$filename");
}
}
}
}
header("Refresh: 0;url=index.php");
}
?>