By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

Delete (unlink) problem!

P: n/a
Im a complete n00b at PHP so please forgive me if this i a stupid
question
Why wont the following work.

PHP Code:

<?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo "$filename <br>". "<a href="unlink('$filename');">Delete</
a>";
}
?>

I want a list of files in the dir and a link next to each file that
deletes the file if clicked.
Any hints or solutions are very much appreciated.

-Effix

Aug 7 '08 #1
Share this Question
Share on Google+
15 Replies


P: n/a
<comp.lang.php>
<>
<Thu, 7 Aug 2008 09:32:20 -0700 (PDT)>
<8e**********************************@p25g2000hsf. googlegroups.com>
Im a complete n00b at PHP so please forgive me if this i a stupid
question
Why wont the following work.

PHP Code:

<?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo "$filename <br>". "<a href="unlink('$filename');">Delete</
a>";
}
?>

I want a list of files in the dir and a link next to each file that
deletes the file if clicked.
Any hints or solutions are very much appreciated.
Although the below code is intended to remove .php files - it could be
adapted to use .jpg files instead .

Remove the # character in front of the print "" stuff to display the
filenames in your browser .

IMHO ignore and resist the temptation to delete the .jpg files to start
with as your first task is .....

(1) grab the .jpg files in the folder
(2) display the .jpg filename - with a clickable link to DELETE it

<?php

$batman=0;

$dir="images/"; $joker=opendir($dir);
while (false!==($boywonder=readdir($joker)))
{
$pisky=1;
if ($boywonder==".") {$pisky=0;}
if ($boywonder=="..") {$pisky=0;}
if ($pisky==1) {$files[]=$boywonder; $batman=$batman+1;}
}
closedir($joker);

$temp=0; $spock=0;
while ($temp<$batman)
{
$demo=$files[$temp];
# print "demo = $demo <br>";
$qaz=strpos($demo,"_time.php");
if ($qaz==true)
{
$jack=strlen($demo);
$film1=substr($demo,0,$jack-9);
$film2=$demo;
$spock=$spock+1;
$kook1[$spock]=$film1;
$kook2[$spock]=$film2;
}
$temp=$temp+1;
}

# print "<br><br>";

if ($spock>0)
{
$yard=1;
while ($yard<$spock+1)
{
# print "$yard = $kook1[$yard] <br>";
# print "$yard = $kook2[$yard] <br>";
$filename="images/" . $kook2[$yard]; $fp=fopen($filename,"r");
$qaz=fgets($fp); $tempt=trim($qaz); fclose($fp);
$kom=time();
if ($kom>$tempt)
{
$gort="images/" . $kook1[$yard] . ".jpg"; unlink($gort);
$gort="images/" . $kook1[$yard] . "_temp.jpg"; unlink($gort);
$gort="images/" . $kook1[$yard] . ".php"; unlink($gort);
$gort="images/" . $kook1[$yard] . "_time.php"; unlink($gort);
}
$yard=$yard+1;
}
}

?>

Aug 7 '08 #2

P: n/a
On 7 Aug., 18:59, Krustov <m...@privacy.netwrote:
<comp.lang.php>
<>
<Thu, 7 Aug 2008 09:32:20 -0700 (PDT)>
<8ef1a638-475f-412b-b54a-d75665811...@p25g2000hsf.googlegroups.com>
Im a complete n00b at PHP so please forgive me if this i a stupid
question
Why wont the following work.
PHP Code:
<?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo "$filename <br>". "<a href="unlink('$filename');">Delete</
a>";
}
?>
I want a list of files in the dir and a link next to each file that
deletes the file if clicked.
Any hints or solutions are very much appreciated.

Although the below code is intended to remove .php files - it could be
adapted to use .jpg files instead .

Remove the # character in front of the print "" stuff to display the
filenames in your browser .

IMHO ignore and resist the temptation to delete the .jpg files to start
with as your first task is .....

(1) grab the .jpg files in the folder
(2) display the .jpg filename - with a clickable link to DELETE it

<?php

$batman=0;

$dir="images/"; $joker=opendir($dir);
while (false!==($boywonder=readdir($joker)))
{
$pisky=1;
if ($boywonder==".") {$pisky=0;}
if ($boywonder=="..") {$pisky=0;}
if ($pisky==1) {$files[]=$boywonder; $batman=$batman+1;}}

closedir($joker);

$temp=0; $spock=0;
while ($temp<$batman)
{
$demo=$files[$temp];
# print "demo = $demo <br>";
$qaz=strpos($demo,"_time.php");
if ($qaz==true)
{
$jack=strlen($demo);
$film1=substr($demo,0,$jack-9);
$film2=$demo;
$spock=$spock+1;
$kook1[$spock]=$film1;
$kook2[$spock]=$film2;

}
$temp=$temp+1;
}

# print "<br><br>";

if ($spock>0)
{
$yard=1;
while ($yard<$spock+1)
{
# print "$yard = $kook1[$yard] <br>";
# print "$yard = $kook2[$yard] <br>";
$filename="images/" . $kook2[$yard]; $fp=fopen($filename,"r");
$qaz=fgets($fp); $tempt=trim($qaz); fclose($fp);
$kom=time();
if ($kom>$tempt)
{
$gort="images/" . $kook1[$yard] . ".jpg"; unlink($gort);
$gort="images/" . $kook1[$yard] . "_temp.jpg"; unlink($gort);
$gort="images/" . $kook1[$yard] . ".php"; unlink($gort);
$gort="images/" . $kook1[$yard] . "_time.php"; unlink($gort);

}
$yard=$yard+1;
}
}

?>
Thank you for your reply. But I cant seem to get your code to work.
And also it seems to be a bit too complex for the fairly simple
function that I want.
The code that I have works fine when I remove the delete link.
<?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo "$filename <br>";
?>

This gives me a nice list of the jpg files in my folder.
All I need to to is to add a link to the that activates
unlink('$filename')
How do I do this?
Aug 7 '08 #3

P: n/a
Hi krustov, did you write this code? If so have you ever heard of
using meaningful variable names? It makes code much easier to read/
understand/manipulate etc.

Maybe you should read this...

http://en.wikipedia.org/wiki/Identif...ing_convention
Aug 7 '08 #4

P: n/a
Message-ID:
<c9**********************************@l64g2000hse. googlegroups.comfrom
Mo***********@gmail.com contained the following:
>Thank you for your reply. But I cant seem to get your code to work.
And also it seems to be a bit too complex for the fairly simple
function that I want.
The code that I have works fine when I remove the delete link.
<?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo "$filename <br>";
?>

This gives me a nice list of the jpg files in my folder.
All I need to to is to add a link to the that activates
unlink('$filename')
How do I do this?
You really don't want to do this. Let's say you have a page containing
links that delete files. Let's suppose you accidentally let that get
spidered by a search engine. What happens when the SE dutifully follows
the links?

I'd use a form and place a checkbox next to each image, something like
echo "<input type='checkbox' name ='del[]' value='$filename'>";

The 'del[]' construct makes sure that the filenames are conveniently
placed in an array.

Then have a submit (<input type='submit' name='delete' value=Delete all
checked boxes'>delete) button somewhere.

Assuming the page is calling itself you would then do something like

<?php
if(isset($_POST['delete'])){
foreach ($_POST['del'] as $filename){
if(unlink($filename)){
echo "$filename deleted!<br>";
}
}
}
?>

Untested.

In practice I'd probably store the names of the deleted files in a
session variable and have an interim confirmation screen. And I'd also
password protect my delete script. But you get the idea.

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk
Aug 7 '08 #5

P: n/a
<comp.lang.php>
<>
<Thu, 7 Aug 2008 10:23:29 -0700 (PDT)>
<c9**********************************@l64g2000hse. googlegroups.com>
Thank you for your reply. But I cant seem to get your code to work.
And also it seems to be a bit too complex for the fairly simple
function that I want.
The code that I have works fine when I remove the delete link.
<?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo "$filename <br>";
?>

This gives me a nice list of the jpg files in my folder.
All I need to to is to add a link to the that activates
unlink('$filename')
How do I do this?
Part of the learning process is tinkering .

Strip away the bottom two thirds of the code - and then add a little bit
at a time .

That means ..... simply adapting some code you have that already works
isnt always suitable ..... and in some cases when you want to achieve a
certain effect ..... then you dont have a choice but to learn some new
techniques ..... or do without .

So , Your first task is to GRAB the list of filenames from the directory
and then DISPLAY them in the browser .

But if your petty little brain says 'but i dont want to do that' then i
suggest you surf www.magicwand.com as it offers a one click solution to
every problem known to mankind .
Aug 7 '08 #6

P: n/a
Hi try something like this (untested code), might help:
<?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo $filename.' <a href="'.$_SERVER['PHP_SELF'].'?delete_img='.
$filename.'">Delete '.$filename.'</a><br>';
?>
Then have something like

<?php

function del_file($dir,$filename){

if (file_exists($dir.'/'.$filename)){
unlink($dir.'/'.$filename);
echo $filename.' was deleted';
}

}
$dir = '../images/profil/';
if (isset($_GET['delete_img'])){
del_file($dir,$_GET['delete_img']);

}

?>
Aug 7 '08 #7

P: n/a
On 7 Aug., 19:52, macca <ptmcna...@googlemail.comwrote:
Hi try something like this (untested code), might help:

<?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo $filename.' <a href="'.$_SERVER['PHP_SELF'].'?delete_img='.
$filename.'">Delete '.$filename.'</a><br>';
?>

Then have something like

<?php

function del_file($dir,$filename){

if (file_exists($dir.'/'.$filename)){
unlink($dir.'/'.$filename);
echo $filename.' was deleted';

}
}

$dir = '../images/profil/';
if (isset($_GET['delete_img'])){
del_file($dir,$_GET['delete_img']);

}

?>
Thank you for your reply macca but I already got it working. I
appreciate the time you took to help me.
Thank you
- Effix
Aug 7 '08 #8

P: n/a
On 7 Aug., 19:39, Geoff Berrow <blthe...@ckdog.co.ukwrote:
Message-ID:
<c955e1f5-f5b3-4916-a9e5-69a1d9fe2...@l64g2000hse.googlegroups.comfrom
Morteneist...@gmail.com contained the following:
Thank you for your reply. But I cant seem to get your code to work.
And also it seems to be a bit too complex for the fairly simple
function that I want.
The code that I have works fine when I remove the delete link.
<?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo "$filename <br>";
?>
This gives me a nice list of the jpg files in my folder.
All I need to to is to add a link to the that activates
unlink('$filename')
How do I do this?

You really don't want to do this. Let's say you have a page containing
links that delete files. Let's suppose you accidentally let that get
spidered by a search engine. What happens when the SE dutifully follows
the links?

I'd use a form and place a checkbox next to each image, something like
echo "<input type='checkbox' name ='del[]' value='$filename'>";

The 'del[]' construct makes sure that the filenames are conveniently
placed in an array.

Then have a submit (<input type='submit' name='delete' value=Delete all
checked boxes'>delete) button somewhere.

Assuming the page is calling itself you would then do something like

<?php
if(isset($_POST['delete'])){
foreach ($_POST['del'] as $filename){
if(unlink($filename)){
echo "$filename deleted!<br>";
}
}}

?>

Untested.

In practice I'd probably store the names of the deleted files in a
session variable and have an interim confirmation screen. And I'd also
password protect my delete script. But you get the idea.

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011http://slipperyhill.co.uk
Thanks very much for the help Geoff! worked like a charm.
-Effix
Aug 7 '08 #9

P: n/a
Message-ID:
<2c**********************************@k37g2000hsf. googlegroups.comfrom
macca contained the following:
><?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo $filename.' <a href="'.$_SERVER['PHP_SELF'].'?delete_img='.
$filename.'">Delete '.$filename.'</a><br>';
?>

Same problem. If the page gets spidered, the files are toast.
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk
Aug 7 '08 #10

P: n/a
..oO(Geoff Berrow)
>Message-ID:
<c9**********************************@l64g2000hse .googlegroups.comfrom
Mo***********@gmail.com contained the following:
>>This gives me a nice list of the jpg files in my folder.
All I need to to is to add a link to the that activates
unlink('$filename')
How do I do this?

You really don't want to do this. Let's say you have a page containing
links that delete files. Let's suppose you accidentally let that get
spidered by a search engine. What happens when the SE dutifully follows
the links?
Indeed.

And it doesn't have to be a SE. It can even be your own browser that
wipes out your files, simply by prefetching some of the following links
in the background while you're still watching the overview page. Using
the GET method to directly trigger modifications on the server without
a POST confirmation is _very_ dangerous.
>I'd use a form and place a checkbox next to each image, something like
echo "<input type='checkbox' name ='del[]' value='$filename'>";

The 'del[]' construct makes sure that the filenames are conveniently
placed in an array.

Then have a submit (<input type='submit' name='delete' value=Delete all
checked boxes'>delete) button somewhere.
Yep. That's the more secure way.

Micha
Aug 7 '08 #11

P: n/a
<comp.lang.php>
<Michael Fesser>
<Thu, 07 Aug 2008 21:34:18 +0200>
<i7********************************@4ax.com>
You can safely ignore the Krusty clown.
RTFM .

But if it wasnt for krusty - then how many people would only have
replied with the above .

Or perhaps mr fesser is going to claim that nobody on comp.lang.php
would ever say such a thing to a newbie user who asked for help .

Aug 7 '08 #12

P: n/a
Krustov wrote:
<comp.lang.php>
<Michael Fesser>
<Thu, 07 Aug 2008 21:34:18 +0200>
<i7********************************@4ax.com>
>You can safely ignore the Krusty clown.

RTFM .

But if it wasnt for krusty - then how many people would only have
replied with the above .

Or perhaps mr fesser is going to claim that nobody on comp.lang.php
would ever say such a thing to a newbie user who asked for help .

You have a very warped opinion of your own self worth. A legend (only)
in your own mind.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 7 '08 #13

P: n/a
..oO(Jerry Stuckle)
>Krustov wrote:
>But if it wasnt for krusty - then how many people would only have
replied with the above .

Or perhaps mr fesser is going to claim that nobody on comp.lang.php
would ever say such a thing to a newbie user who asked for help .


You have a very warped opinion of your own self worth. A legend (only)
in your own mind.
Maybe OLPC was not the best idea. ;)

Micha
Aug 7 '08 #14

P: n/a
Michael Fesser wrote:
.oO(Jerry Stuckle)
>Krustov wrote:
>>But if it wasnt for krusty - then how many people would only have
replied with the above .

Or perhaps mr fesser is going to claim that nobody on comp.lang.php
would ever say such a thing to a newbie user who asked for help .

You have a very warped opinion of your own self worth. A legend (only)
in your own mind.

Maybe OLPC was not the best idea. ;)

Micha
Oh, I think it is. But some people need to grow enough to be classified
as a child. :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 7 '08 #15

P: n/a
..oO(Jerry Stuckle)
>Michael Fesser wrote:
>.oO(Jerry Stuckle)
>>Krustov wrote:

But if it wasnt for krusty - then how many people would only have
replied with the above .

Or perhaps mr fesser is going to claim that nobody on comp.lang.php
would ever say such a thing to a newbie user who asked for help .
You have a very warped opinion of your own self worth. A legend (only)
in your own mind.

Maybe OLPC was not the best idea. ;)

Micha

Oh, I think it is. But some people need to grow enough to be classified
as a child. :-)
Then maybe it's just a diaper change long overdue. That would explain
something.

Micha
Aug 8 '08 #16

This discussion thread is closed

Replies have been disabled for this discussion.