473,911 Members | 5,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<siz eof($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,$r ecords[$i]); //write unselected records to data.dat
else // extract the wanted filename in specific record
{
$columns = explode("|",$re cords[$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.g if
Warning: unlink() failed (Invalid argument) in c:\program
files\....\admi n\delete_file_r ec.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 6702
Eric wrote:
else // extract the wanted filename in specific record
{
$columns = explode("|",$re cords[$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("|",$re cords[$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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4384
by: 3A Web Hosting | last post by:
Hi Folks I'm trying to delete a file. unlink('../images/photos/1967_GT_500_Shelby_Mustang.jpg'); That works $fm = "../images/photos/1967_GT_500_Shelby_Mustang.jpg"; unlink($fm); That doesn't. Why?
1
2805
by: Chamomile | last post by:
I am having trouble getting unlink() to work on my local machine (win 2000, Apache, php 4.2.2) when I invoke unlink() in order to change a file name during a 'picture edit' change from a form upload on my local machine using simple stuff like: <?php if(file_exists($new_file_name)){
4
3729
by: rbt | last post by:
Can someone detail the differences between these two? On Windows which is preferred? Also, is it true that win32api.DeleteFile() can remove the 'special' files located in the 'special' folders only accessible by the shell object such as Temporary Internet Files, etc. Thanks!
6
16787
by: Benjamin | last post by:
Hello, I'm running PHP 5.2.0 as a Apache 2.2 server module. When, I try to unlink I get this: "Warning: unlink(file.txt) : Permission denied..." What and how do I change this so it works? Thanks
4
12841
by: nitinpatel1117 | last post by:
Hi I am trying to delete a csv file using the unlink() function, but i keep getting a permission denied warning Warning: unlink(UsedNOS200704191.CSV): Permission denied in E:\webroot\dev\file_test\index.php on line 416 My script basically reads email attachments and downloads the attachment file into the current directory of the running script. The data in the attachements is then read and stored into MySQL. Once i've finished reading...
1
2509
by: fcaserio | last post by:
Is there any PHP.INI config or windows permission (win 2003) that can prevent PHP to unlink files with the system path? unlink works with relative path: @unlink("temp\0000058.txt") but it is not workinng with the absolute path @unlink("C:\htdocs\temp\0000058.txt") the thing is I use unlink within a function called from different directories, so I use absolute path to make sure the path is correct. I've installed my application on...
15
3795
by: Morteneistrom | last post by:
Im a complete n00b at PHP so please forgive me if this i a stupid question Why wont the following work. PHP Code: <?php foreach (glob('../images/profil/*.jpg') as $filename) { echo "$filename <br>". "<a href="unlink('$filename');">Delete</ a>";
11
4482
by: byteoart | last post by:
I'm a newbie to php. I am trying to delete a file from the server that I put there (one file, not the whole directory). I am using unlink ($filename); I did the path as a relative path as follows: .../images/6/image_001.JPG I did a scandir $files = scandir("../images/6"); The print_r ($files); shows that the file is one of the files present.
18
14682
by: Coffee Pot | last post by:
Thanks for any advice. ~ CP
0
10038
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10921
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11057
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10541
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7250
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5940
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6142
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.