Unlink and variables

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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.....
    Eric Guest

  2. Similar Questions and Discussions

    1. Still need unlink help!! God help me. - Revisited
      <20030808202717051099.GyazMail.neuroball@usa.net> <YW50aWdvbmU=.45ab06a65368617cf57750f9f6311818@1060402929.cotse.net>
    2. Still need unlink help!! God help me.
      Still having problem with unlink. My original problem began with deleting files from a list. I seem to have fixed the problem reading the list but...
    3. Help with Unlink please
      > Steve Grazzini wrote at Wed, 06 Aug 2003 23:38:00 -0400: Neither of these worked. I am beginning to think that there is something wrong with...
    4. problem using unlink()
      Hi, I have a problem using unlink(); on a system using FreeBSD and Apache... where my local system using Windows and Apache experiences no...
    5. unlink() in a loop???
      I have a script that allows users to upload pics and enter personal info into Database (not pictures i only store filename in db). The form passes...
  3. #2

    Default Re: Unlink and variables

    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.
    Pedro Guest

  4. #3

    Default Re: Unlink and variables -problem solved

    Pedro <hexkid@hotpop.com> wrote in message news:<blc7nk$aeg10$1@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
    Eric Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139