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

Advice on deleting images from server using PHP

P: n/a
Hello:

I'm coding a website which allows the owner to make add/change/deletes to
his product database, which includes product images stored separately on the
server. I have coded the Delete page which deletes the product record from
the database, however the product image remains in the associated folder on
the server. I'd prefer these images not accumulate and would like to delete
these images along with the database record.

Can anyone refer me to a PHP script or suggest a way to do this?

Frank H.
Austin, TX
Jul 12 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
<comp.lang.php>
<Frankie>
<Wed, 12 Jul 2006 18:36:01 GMT>
<li*****************@newsread1.news.pas.earthlink. net>
I'm coding a website which allows the owner to make add/change/deletes to
his product database, which includes product images stored separately on the
server. I have coded the Delete page which deletes the product record from
the database, however the product image remains in the associated folder on
the server. I'd prefer these images not accumulate and would like to delete
these images along with the database record.

Can anyone refer me to a PHP script or suggest a way to do this?
$pass=0;
$filename="images/whatever.jpg";
if (!file_exists($filename)) {$pass=1;}

if ($pass==1) {unlink($filename);}
unlink() is a silly name for a delete command isnt it .
--
Encrypted email address
www.emailuser.co.uk/?name=KRUSTOV
Jul 12 '06 #2

P: n/a
*** Frankie escribió/wrote (Wed, 12 Jul 2006 18:36:01 GMT):
I have coded the Delete page which deletes the product record from
the database, however the product image remains in the associated folder on
the server. I'd prefer these images not accumulate and would like to delete
these images along with the database record.

Can anyone refer me to a PHP script or suggest a way to do this?
http://www.php.net/unlink

Of course, you must read the filename from DB *before* deleting the record
;-)
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Jul 12 '06 #3

P: n/a
On 2006-07-12, Krustov <me@privacy.netwrote:
unlink() is a silly name for a delete command isnt it .
Why? That's exactly what it does - removing the link to a file (and
probably decrementing the link count), isn't it? :)

--
Riemer Palstra Amsterdam, The Netherlands
ri****@palstra.com http://www.palstra.com/
Jul 13 '06 #4

P: n/a
On 2006-07-13, Riemer Palstra <ri****@palstra.comwrote:
On 2006-07-12, Krustov <me@privacy.netwrote:
>unlink() is a silly name for a delete command isnt it .

Why? That's exactly what it does - removing the link to a file (and
probably decrementing the link count), isn't it? :)
It does a fair bit more than that, though. It also changes inode tables,
changes directory listings, etc.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above domain.
"You people hate mathematics." -- James Harris
Jul 13 '06 #5

P: n/a
"Krustov" <me@privacy.netwrote in message
news:MP***********************@news.newsreader.com ...
>
$pass=0;
$filename="images/whatever.jpg";
if (!file_exists($filename)) {$pass=1;}

if ($pass==1) {unlink($filename);}
Thanks for your example! I like your code's simplicity. The documentation at
php.net is difficult for me to follow.

However, I need to use variables in this code.

Specifically, I'm trying to do something like this:

$pass=0;
$filename="../{$_POST['hfCategory']}/photos/{$_POST['hfItemNumber']}.jpg";
if (!file_exists($filename)) {$pass=1;}

if ($pass==1) {unlink($filename);}

...but it's not working.

Any additional advice appreciated.

F.H.

Jul 14 '06 #6

P: n/a
<comp.lang.php>
<Frankie>
<Fri, 14 Jul 2006 02:53:51 GMT>
<3H*****************@newsread1.news.pas.earthlink. net>
However, I need to use variables in this code.

Specifically, I'm trying to do something like this:

$pass=0;
$filename="../{$_POST['hfCategory']}/photos/{$_POST['hfItemNumber']}.jpg";
if (!file_exists($filename)) {$pass=1;}

if ($pass==1) {unlink($filename);}

..but it's not working.

Any additional advice appreciated.
I'd start by breaking it down a little .

$temp1="$_POST['hfCategory']";
print "temp1 = $temp1 <br>";

$temp2="$_POST['hfItemNumber']";
print "temp2 = $temp2 <br>";

$filename="$temp1/photos/$temp2" . ".jpg";

Making sure you have the path 100% correct is the first step .

TMK "../" doesnt need to be used unless you have good reason to do so .
--
Encrypted email address
www.emailuser.co.uk/?name=KRUSTOV
Jul 14 '06 #7

P: n/a
Just a sidebar here.

I normally don't pay attention to sigs. But I happened to glance at
yours and saw your encrypted email address. I think that's a great
idea. I'm going to have to see if there's something like that here in
the states.

Does it help stop spam?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 14 '06 #8

P: n/a
<comp.lang.php>
<Jerry Stuckle>
<Fri, 14 Jul 2006 12:30:22 -0400>
<Wa******************************@comcast.com>
Just a sidebar here.

I normally don't pay attention to sigs. But I happened to glance at
yours and saw your encrypted email address. I think that's a great
idea. I'm going to have to see if there's something like that here in
the states.

Does it help stop spam?
You dont say what sig your talking about - but i'll take a wild guess
and assume its my own .

I think there one called 'cerbermail.com' or summit that does the same
sort of thing if you dont mind google ads .

As for stopping spam - who knows .
--
Encrypted email address
www.emailuser.co.uk/?name=KRUSTOV
Jul 14 '06 #9

P: n/a
I just want to agree with Frankie on "going one step at a time" when
building any path on any file system. Moreover, simple echos to those
variable often save you lots of frustration. I was working on something
similar and had a heck of a time until I realized that the path isn't being
built right.
"Krustov" <me@privacy.netwrote in message
news:MP************************@news.newsreader.co m...
<comp.lang.php>
<Frankie>
<Fri, 14 Jul 2006 02:53:51 GMT>
<3H*****************@newsread1.news.pas.earthlink. net>
>However, I need to use variables in this code.

Specifically, I'm trying to do something like this:

$pass=0;
$filename="../{$_POST['hfCategory']}/photos/{$_POST['hfItemNumber']}.jpg";
if (!file_exists($filename)) {$pass=1;}

if ($pass==1) {unlink($filename);}

..but it's not working.

Any additional advice appreciated.

I'd start by breaking it down a little .

$temp1="$_POST['hfCategory']";
print "temp1 = $temp1 <br>";

$temp2="$_POST['hfItemNumber']";
print "temp2 = $temp2 <br>";

$filename="$temp1/photos/$temp2" . ".jpg";

Making sure you have the path 100% correct is the first step .

TMK "../" doesnt need to be used unless you have good reason to do so .
--
Encrypted email address
www.emailuser.co.uk/?name=KRUSTOV

Jul 14 '06 #10

P: n/a
Message-ID: <MP************************@news.newsreader.comfro m
Krustov contained the following:
>Does it help stop spam?

You dont say what sig your talking about - but i'll take a wild guess
and assume its my own .
Look at the headers.

Bloody hell krusty, Jerry Stuckle says you have a good idea. You'll not
be able to live this down in troll circles.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 15 '06 #11

P: n/a
Krustov wrote:
<comp.lang.php>
<Jerry Stuckle>
<Fri, 14 Jul 2006 12:30:22 -0400>
<Wa******************************@comcast.com>
>>Just a sidebar here.

I normally don't pay attention to sigs. But I happened to glance at
yours and saw your encrypted email address. I think that's a great
idea. I'm going to have to see if there's something like that here in
the states.

Does it help stop spam?


You dont say what sig your talking about - but i'll take a wild guess
and assume its my own .

I think there one called 'cerbermail.com' or summit that does the same
sort of thing if you dont mind google ads .

As for stopping spam - who knows .

Yep, I figured since I was replying to you it would be obvious. Guess
not :-).

Thanks of the tip on cerbermail.com - but think I'll skip the google
ads. Maybe put it on one of my own sites.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 15 '06 #12

P: n/a
<comp.lang.php>
<Geoff Berrow>
<Sat, 15 Jul 2006 01:43:14 +0100>
<3d********************************@4ax.com>
Bloody hell krusty, Jerry Stuckle says you have a good idea. You'll not
be able to live this down in troll circles.
Doubt his own version of it will be as good as mine :-)
--
Encrypted email address
www.emailuser.co.uk/?name=KRUSTOV
Jul 15 '06 #13

P: n/a
"Krustov" <me@privacy.netwrote in message
news:MP************************@news.newsreader.co m...
>
I'd start by breaking it down a little .

$temp1="$_POST['hfCategory']";
print "temp1 = $temp1 <br>";

$temp2="$_POST['hfItemNumber']";
print "temp2 = $temp2 <br>";

$filename="$temp1/photos/$temp2" . ".jpg";

Making sure you have the path 100% correct is the first step .

TMK "../" doesnt need to be used unless you have good reason to do so .
Thanks again for your help.

I have found the error. Your original code's conditional statement read:

if (!file_exists($filename)) {$pass=1;}

I missed the "!", so it would only delete the file if it did NOT exist. But
it does.

I have since corrected and simplified the code to this:

$pass=0;
$photo="../$hfCategory/photos/$hfItemNumber.jpg";
if (file_exists($photo)) {$pass=1;}

if ($pass==1) {unlink($photo);}

...and it works beautifully.

BTW: in my case, the "../" in the file path IS required in order to locate
the file. The actual sample file path is thus:
"../lifestyle/photos/6000.jpg"

F.H.
Jul 15 '06 #14

P: n/a

"Krustov" <me@privacy.netwrote in message
news:MP************************@news.newsreader.co m...
<comp.lang.php>
<Geoff Berrow>
<Sat, 15 Jul 2006 01:43:14 +0100>
<3d********************************@4ax.com>
>Bloody hell krusty, Jerry Stuckle says you have a good idea. You'll not
be able to live this down in troll circles.

Doubt his own version of it will be as good as mine :-)
If you REALLY want to eliminate ALL spam with ZERO false positives go look
at www.validmail.net. Except during a server upgrade and mismatch, I
haven't received spam from there in over a year and a half.

Shellyh
Jul 16 '06 #15

P: n/a
<comp.lang.php>
<Sheldon Glickler>
<Sun, 16 Jul 2006 14:27:33 GMT>
<p1*******************@twister.nyroc.rr.com>
Bloody hell krusty, Jerry Stuckle says you have a good idea. You'll not
be able to live this down in troll circles.
Doubt his own version of it will be as good as mine :-)

If you REALLY want to eliminate ALL spam with ZERO false positives go look
at www.validmail.net. Except during a server upgrade and mismatch, I
haven't received spam from there in over a year and a half.
You are either trolling or you are missing the point .

A usenet post can contain of method of contacting the poster by email
without the harvesting spammer bots being able to pick it up .
--
Encrypted email address
www.emailuser.co.uk/?name=KRUSTOV
Jul 16 '06 #16

P: n/a

"Krustov" <me@privacy.netwrote in message
news:MP************************@news.newsreader.co m...
<comp.lang.php>
<Sheldon Glickler>
<Sun, 16 Jul 2006 14:27:33 GMT>
<p1*******************@twister.nyroc.rr.com>
>Bloody hell krusty, Jerry Stuckle says you have a good idea. You'll
not
be able to live this down in troll circles.
Doubt his own version of it will be as good as mine :-)

If you REALLY want to eliminate ALL spam with ZERO false positives go
look
at www.validmail.net. Except during a server upgrade and mismatch, I
haven't received spam from there in over a year and a half.

You are either trolling or you are missing the point .

A usenet post can contain of method of contacting the poster by email
without the harvesting spammer bots being able to pick it up .
I missed that by happening to come in late.

Shelly
Jul 16 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.