473,714 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete (unlink) problem!

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');">De lete</
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
15 3765
<comp.lang.ph p>
<>
<Thu, 7 Aug 2008 09:32:20 -0700 (PDT)>
<8e************ *************** *******@p25g200 0hsf.googlegrou ps.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');">De lete</
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!==($boyw onder=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($de mo,"_time.php") ;
if ($qaz==true)
{
$jack=strlen($d emo);
$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="imag es/" . $kook2[$yard]; $fp=fopen($file name,"r");
$qaz=fgets($fp) ; $tempt=trim($qa z); 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
On 7 Aug., 18:59, Krustov <m...@privacy.n etwrote:
<comp.lang.ph p>
<>
<Thu, 7 Aug 2008 09:32:20 -0700 (PDT)>
<8ef1a638-475f-412b-b54a-d75665811...@p2 5g2000hsf.googl egroups.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');">De lete</
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!==($boyw onder=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($de mo,"_time.php") ;
if ($qaz==true)
{
$jack=strlen($d emo);
$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="imag es/" . $kook2[$yard]; $fp=fopen($file name,"r");
$qaz=fgets($fp) ; $tempt=trim($qa z); 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('$filena me')
How do I do this?
Aug 7 '08 #3
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
Message-ID:
<c9************ *************** *******@l64g200 0hse.googlegrou ps.comfrom
Mo***********@g mail.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('$filen ame')
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='$filenam e'>";

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($file name)){
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 011000100110110 0010000000110
001101101011011 001000110111101 100111001011
100110001101101 111001011100111 010101101011
http://slipperyhill.co.uk
Aug 7 '08 #5
<comp.lang.ph p>
<>
<Thu, 7 Aug 2008 10:23:29 -0700 (PDT)>
<c9************ *************** *******@l64g200 0hse.googlegrou ps.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('$filena me')
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
Hi try something like this (untested code), might help:
<?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo $filename.' <a href="'.$_SERVE R['PHP_SELF'].'?delete_img=' .
$filename.'">De lete '.$filename.'</a><br>';
?>
Then have something like

<?php

function del_file($dir,$ filename){

if (file_exists($d ir.'/'.$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
On 7 Aug., 19:52, macca <ptmcna...@goog lemail.comwrote :
Hi try something like this (untested code), might help:

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

Then have something like

<?php

function del_file($dir,$ filename){

if (file_exists($d ir.'/'.$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
On 7 Aug., 19:39, Geoff Berrow <blthe...@ckdog .co.ukwrote:
Message-ID:
<c955e1f5-f5b3-4916-a9e5-69a1d9fe2...@l6 4g2000hse.googl egroups.comfrom
Morteneist...@g mail.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('$filena me')
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='$filenam e'>";

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($file name)){
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 011000100110110 0010000000110
001101101011011 001000110111101 100111001011
100110001101101 111001011100111 010101101011htt p://slipperyhill.co .uk
Thanks very much for the help Geoff! worked like a charm.
-Effix
Aug 7 '08 #9
Message-ID:
<2c************ *************** *******@k37g200 0hsf.googlegrou ps.comfrom
macca contained the following:
><?php
foreach (glob('../images/profil/*.jpg') as $filename) {
echo $filename.' <a href="'.$_SERVE R['PHP_SELF'].'?delete_img=' .
$filename.'">D elete '.$filename.'</a><br>';
?>

Same problem. If the page gets spidered, the files are toast.
--
Geoff Berrow 011000100110110 0010000000110
001101101011011 001000110111101 100111001011
100110001101101 111001011100111 010101101011
http://slipperyhill.co.uk
Aug 7 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6473
by: Phil Powell | last post by:
print_r(is_file("$logPath/$logFileName")); // RETURNS 1 unlink("$logPath/$logFileName"); // RETURNS WARNING: PERMISSION DENIED This code should tell me that the file located at $logPath/$logFileName should delete the log file, however, it never deletes it due to permissions error. However, upon checking the file properties I find this:
9
2793
by: Dejan | last post by:
Hy, Sorry for my terreble english I have this simple code for deleting rows in mysql table... Everything works fine with it. So, what do i wanna do...: my sql table looks something like this: id Name Surname pictr0 picrt1 pictr2 pictr3
11
5804
by: comp.lang.php | last post by:
Once again, I thought my class method deleteZip() would do the trick, but it never deletes any .zip* file found in a directory: /** * Delete any latent ZIP files found in this album. This method is to be inherited by all listing classes to allow for * list-wide deletion of latent server-created ZIP files for security purposes *
1
2080
by: Shazia Sadiq | last post by:
i had tried this code but it is not working function delete_file($file){ $delete = @unlink($file); clearstatcache(); if (@file_exists($file)) { $filesys = eregi_replace("/","\\",$file); $delete = @system("del $filesys"); clearstatcache(); if (@file_exists($file)) { $delete = @chmod ($file, 0775);
7
3873
by: daithimcc | last post by:
I have been trying to delete a file using a script and it is refusing to go. (I have very little experience of Perl). One script creates the temporary file (empty) and later another is to delete it. The url passes a parameter to the script: e.g. dfile.pl?f=abcde The filename f obviously varies. In the script:
8
3898
by: jessica87 | last post by:
hi there.... i need some guidance here with this code? Code: ( text ) 1. $myFile = "testFile.txt"; 2. $fh = fopen($myFile, 'w') or die("can't open file");
5
3553
by: Gordon | last post by:
I'm trying to remove a directory and all its contents from within a script. I wrote a recursive function to take care of it, but when I run it I get random "Directory not empty" error messages. I dropped some code in to echo out the name of the file that's about to be rmdir()ed or unoink()ed (depending if it's a file or a directory) to see if it was choking on a particular subdirectory, but doing that causes all the error messages to...
18
14632
by: Coffee Pot | last post by:
Thanks for any advice. ~ CP
9
8451
code green
by: code green | last post by:
I have this piece of code to tidy up after a function that calls move_uploaded_file() if(file_exists($destination.$filename)) { $exitmsg .= "<br>File still in temporary location <br>$filename <br>Attempting deletion "; if(unlink($destination.$filename)) $exitmsg .= '<br>Success deleting. Please try again '; else
0
8704
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9009
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7946
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4462
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3155
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.