Jump to content

Deleting files


Luke Warm

Recommended Posts

I'm working on a project to upload files. The uploading is working fine. I want to add a button to delete individual files loaded into a list. I've tried the unlink() function to no avail.  I've attached my code.

 

Any help would be appreciated...

 

[attachment deleted by admin]

Link to comment
Share on other sites

I took your advice and created a function from the code you sent but it still doesn't work.

I inserted the function into the page PHP code. Did not create a separate "include" file, if that makes a difference.

 

Here's the function:

 

function deleteMe() {

if(unlink("/var/www/html/uploads/".$_FILES["file"]["name"])){

echo 'Deleted original file!';

}

}

Link to comment
Share on other sites

I've added this code to the page:

 

// Delete file when button clicked

if(isset($_GET['delete']))

{

unlink("uploads/".$image);

echo "<center>File deleted!</center>";

}

 

With a link added for each file to be deleted:

<a href='imageUploads.php?delete=$image'>Delete</a>

 

It deletes ALL the images in the directory instead of just one.

NOTE: "uploads" is the directory where the files are stored...

Link to comment
Share on other sites

I would echo out your unlink path+file to make sure you are actually getting the correct location/file.

 

By the way the $_FILES array only is used when there is a file uploaded from your script via POST. If the file has already been uploaded your sever previously, it wont be in $_FILES.

Link to comment
Share on other sites

When I put my mouse over the "delete" link, it shows the correct path:

 

imagesUpload.php?delete=imagename.jpg

 

Even if I change the code so "imagesUploads.php?delete=" shows full path, still deletes all files in the directory.

 

Looks like you never define $image. so unlink is removing all your files. I would use is_file() function along with unlink()

 

<?php
$path = "Whatever your path is"; 
$image = $_GET['delete'];
$pf = $path.$image;
if(is_file($pf)){
    unlink($pf);
}
?>

 

 

 

Link to comment
Share on other sites

$path = "/var/www/html/uploads"; 

$image = $_GET['delete'];

$pf = $path.$image;

if(is_file($pf)){

     unlink($pf);

}

 

<a href='imageUploads.php?delete=$image'>Delete</a>

 

Getting:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in imagesUpload.php

Link to comment
Share on other sites

$path = "/var/www/html/uploads";

$image = $_GET['delete'];

$pf = $path.$image;

if(is_file($pf)){

    unlink($pf);

}

 

<a href='imageUploads.php?delete=$image'>Delete</a>

 

Getting:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in imagesUpload.php

 

Where is the error occurring?

 

by the way

$path = "/var/www/html/uploads/";

 

Link to comment
Share on other sites

This is the start of the php:

 

<?php # This script lists the images in the "uploads" directory.

require_once ('includes/functions.inc.php');

$dir = 'uploads/'; // Define the directory to view.

 

$files = scandir($dir); // Read all the images into an array.

 

// Display each image caption as a link to the JavaScript function:

foreach ($files as $image) {

 

if (substr($image, 0, 1) != '.') { // Ignores anything starting with a period.

 

// Get the image's size in pixels:

$image_size = getimagesize ("$dir/$image");

 

// Calculate the image's size in kilobytes:

$file_size = round ( (filesize ("$dir/$image")) / 1024) . "kb";

 

// Make the image's name URL-safe:

$image = urlencode($image);

 

This is where the "$path" statment begins...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.