469,077 Members | 1,415 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,077 developers. It's quick & easy.

Unlink and variables

Hello,

I created a form to upload files. The formfield contents are stored as
records in an Ascii file "data.dat"; for reasons I am not using a
MySql database.
The upoaded file itself is stored in a directory called "files".
With an administration form I can read all contents of the data.dat
file and select records to delete (one at a time).
So far so good.

Now in one of the record fields the uploaded filename is stored.
To add some new functionality I will not only delete the record but
also delete the specific file mentioned in the filename field.

To realise this I use the standard PHP function unlink(). To extract
the needed filename from the selected record I use the PHP function
explode(); fields are seperated with "|".

By using the explode() function I can select the wanted filename in
this record with the correct array value.

for($i=0;$i<sizeof($records);$i++) //Part of code that writes all
the records back to the data.dat file
{
if($i!=$id) //$id is the value of the choosen record to
delete
fwrite($file,$records[$i]); //write unselected records to data.dat
else // extract the wanted filename in specific record
{
$columns = explode("|",$records[$id]);
//$filename = "TryMe.txt"; //Alternative used for testing
$filename = $columns[sizeof($columns)-1];
echo "$filename"; //echo to screen for testing
if(unlink("../files/$filename"))
echo "The file $filename is succesfully deleted";
//echo to screen for testing
}//else
}//for

Now the problem:

Running this 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 when I seperately copy a testfile named TryMe.txt
into the files directory and then run the same code using $filename =
"TryMe.txt" instead of $filename = $columns[sizeof($columns)-1];
things work fine. The result is that echo displays the correct
filename and the
file TryMe.Txt is deleted from the ../files directory!!

(To activate code I then choose a random record to delete in the
administration.)

Summarized:
$filename = "TryMe.txt"; - unlink runs fine
ref. screen output: echo, echo:
===============================================
.../files/TryMe.txtThe file TryMe.txt is succesfully deleted
===============================================

$filename = $columns[sizeof($columns)-1]; - unlink generates error.
ref. screen output: echo, Warning:
================================================
.../files/10_16_40_boef.gif
Warning: unlink() failed (Invalid argument) in c:\program
files\....\admin\delete_file_rec.php on line 43
=================================================

Is there anybody who can help me out and give me a clue??
Thanks in advance.....
Jul 17 '05 #1
2 6447
Eric wrote:
else // extract the wanted filename in specific record
{
$columns = explode("|",$records[$id]);
//$filename = "TryMe.txt"; //Alternative used for testing
$filename = $columns[sizeof($columns)-1]; [...] Is there anybody who can help me out and give me a clue??


Maybe there's some extra characters in $filename.

Try

$filename = trim($filename);

to remove unwanted spaces and/or newlines.
HTH

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #2
Pedro <he****@hotpop.com> wrote in message news:<bl************@ID-203069.news.uni-berlin.de>...
Eric wrote:
else // extract the wanted filename in specific record
{
$columns = explode("|",$records[$id]);
//$filename = "TryMe.txt"; //Alternative used for testing
$filename = $columns[sizeof($columns)-1];

[...]
Is there anybody who can help me out and give me a clue??


Maybe there's some extra characters in $filename.

Try

$filename = trim($filename);

to remove unwanted spaces and/or newlines.

Hello Pedro,

Thanks for the advise. Solution works fine, problem solved!!

Regards,
Eric
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by 3A Web Hosting | last post: by
1 post views Thread by Chamomile | last post: by
4 posts views Thread by rbt | last post: by
6 posts views Thread by Benjamin | last post: by
4 posts views Thread by nitinpatel1117 | last post: by
1 post views Thread by fcaserio | last post: by
15 posts views Thread by Morteneistrom | last post: by
11 posts views Thread by byteoart | last post: by
18 posts views Thread by Coffee Pot | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.