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
https://forums.phpfreaks.com/topic/181179-deleting-files/
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
https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-956299
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
https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-956995
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
https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957019
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
https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957041
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
https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957045
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
https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957053
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
https://forums.phpfreaks.com/topic/181179-deleting-files/#findComment-957063
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.