Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: PHP - Deleting variable and file together

  1. #1
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default PHP - Deleting variable and file together

    Hi,
    I have file uploading system which saves file and some info about it:
    PHP Code:
    $target_path "uploads/"// directory CHMODed to 777.

    $target_path $target_path basename$_FILES['uploadedfile']['name']); 

    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path); 
    $filename basename($_FILES['uploadedfile']['name']); 
    so I have file saved in uploads/ and also I have $filename
    how I can delete file and all the row in mysql by pushing one button???

    If you need more info -ask thanks

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    To delete a file, use unlink, to delete a variable use unset.

    Maybe?:
    PHP Code:
    unlink($target_path); 
    Jeremy | jfein.net

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    1. File: as above, use unlink() in PHP-- use the filename AND path, so unlink('uploads/example.file');

    2. To delete from a mysql database, use the DELETE command:
    DELETE FROM `tablename` WHERE `something` = `somevalue`;

    (For example, `id`=`8`, or `filename`=`example.file`)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    I got unlinking problem
    PHP Code:
    Warningunlink() [function.unlink]: http does not allow unlinking in /home/freecorplietuv/domains/laisvasprotas.xz.lt/public_html/admin/patvirtinti.php on line 19 
    in row 19 is written: $del = unlink($file_path);

    Whats wrong?

  5. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    You can't unlink 'http://'
    Jeremy | jfein.net

  6. #6
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    then what I have to unlink??
    Can you post some egzamples?

  7. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    PHP Code:
    unlink("file.php"); 
    $filepath has a prefix of http://
    Jeremy | jfein.net

  8. #8
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    PHP Code:
    <form method="post">
    <
    input name="set" type="hidden" value="1" />
    <
    input type="submit" value="Delete" />
    </
    form>
    if (isset(
    $_POST['set'])) {
    unlink($target_path $filename);
    SQL commands here to remove

    Corrections to my coding/thoughts welcome.

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    1. bluewalrus, that code is fine but very dangerous: having direct user input to deleting files is very messy. Some sort of error checking would be very wise. Make sure they aren't deleting a higher level folder, and make sure the folder exists before unlinking it, at the very least. Unless this is a very limited application, it's best to only allow that sort of thing for logged in admins, etc. Otherwise, having some sort of non-transparent system is best, like a form that corresponds to a database where the real path is stored-- then it is hidden from the users so they can't write in any folder to delete-- only the ones stored in the database.

    2. "http:"-- You do not use the URL (like "http://example.com/file.php"). Instead, use the RELATIVE path: (file.php) or (directory/file.php), etc.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    djr33, you cannot delete higher level files. But yes - NOT secure.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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