Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Member #
    26064
    Hi guys,

    I have managed to successfully create a form that allows a user to upload a file which gets uploaded to a directory I've created. What I want is for a user to be able to delete these files (single, one at a time) with a (separate) button click. I'm not fussed on security measures or who this user is for the time being, just trying to figure out how to delete one. I've tried unlink(filename), but to no avail.

    I have searched the earth and back and tried a few things including unlink(file), but nothing seems to work.

    any suggestions? help?

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,716
    Member #
    5580
    Liked
    718 times
    unlink should work.

    Maybe your unlink path/URL is wrong.

    Show us the script you use for uploading.

    Also the script you have that has the delete button.


  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Member #
    26064
    This is my upload file.

    <?php
    //This file checks to see if the file the user is trying to upload is too large or has the correct extension and then uploads it to the server

    $maxSize = 1048576;
    $targetPath = "uploads/";
    $allowed = array('.jpg', '.gif', '.png', '.doc', '.docx', '.txt', '.pdf');
    $name = $_FILES['uploadedfile']['name'];
    $extension = substr($name, strpos($name,'.'), strlen($name)-1);

    if (!in_array($extension, $allowed)) {
    die("The file you tried to upload is not allowed. Please try again");
    }

    if (filesize($_FILES['uploadedfile']['tmp_name']) > $maxSize) {
    die("The file you tried to upload is too large. Please try again");
    }

    $targetPath = $targetPath.basename($_FILES['uploadedfile']['name']);

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$targetPath)) {
    echo "<div id='filemsg'> The file".basename( $_FILES['uploadedfile']['name']).
    " has been uploaded </div>";
    }
    else {
    echo "<p>Sorry there was a problem with the upload. Please try again!</p>";
    }

    header("Location:cms.php");

    ?>

    and this is (part) of my php file that gives the button its value:

    foreach ($files as $name) {

    if (substr($name, 0, 1) != '.') {

    $fileName = urlencode($name);

    echo "<li id='list'><a href=\" javascript:newWindow('$name')\">$name</a></li><input type='submit' id='togbt' value='Delete $name' onsubmit='delete.php?$name='".$name."'/>";

    }

    }

    This is the delete file:

    <?php
    $file = $name;
    unlink('uploads/$file');
    echo "<p>The file ".$file." has been deleted</p>";
    ?>

    any help?

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,716
    Member #
    5580
    Liked
    718 times
    In your delete script, you have to receive the filename from somewhere,
    either with a GET or POST. Below I just check for either one in my example.

    You need to realize that scrips cannot share variables, because the webhosts
    normally have register_globals turned off (disabled). It's a security issue.

    PHP Code:

    <?php

    if(isset($_POST['name'])){
    $name=$_POST['name'];
    }
    if(isset(
    $_GET['name'])){
    $name=$_GET['name'];
    }

    $file $name;
    unlink('uploads/$file');
    echo 
    "<p>The file ".$file." has been deleted</p>";
    ?>


  6. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Member #
    26064
    I sent it over using onsubmit='delete.php?$name='".$name."'/>"
    will that not work?

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,716
    Member #
    5580
    Liked
    718 times
    I don't think it will work ... you have <input> type not within a <form>, so it won't execute properly.

    Try this:

    echo "<li id='list'><a href=\" javascript:newWindow('$name')\">$name</a></li><a href=\"delete.php?$name='".$name."'\">delete</a>";

    In which case it should work, because you'll have the $_GET portion in your script (as I indicated before).



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 02:29 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com