473,395 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Advice on deleting images from server using PHP

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
16 3064
<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
*** 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
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
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
"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
<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
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
<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
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
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
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
<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
"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

"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
<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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Me | last post by:
Hi Folks, I'm trying to delete 2 files using fso. The mail file name is stored in an access db. using the code below I can delete the main file but not the thumbnail. The thumbnail has the...
4
by: greyseal | last post by:
I've gone around and around this thing for the last day or so. Using WIA, I can take and copy images from my Nikon D2H. However, when I attempt to delete the image I find that that capability is...
1
by: john_20_28_2000 | last post by:
Using asp.net, c#, mysql or SQL Server. I have a image uploading and wanted it to be "secure." Each user has their own section to login and upload images. Should I store the images in a database...
2
by: I am Sam | last post by:
I have successfully been able to upload images to a web server using the ASP.NET Upload control as well I can index the images in a database. Nevertheless, I need to be able to delete the files off...
3
by: KWilliams | last post by:
I'd like to get some good advice about our old ASP site. You can see our home page at: http://www.douglas-county.com/ ....and an example application page at:...
0
by: Mike P | last post by:
When you specify updateparameters or deleteparameters for your gridview, I was under the impression that whether or not you write some code in the rowcommand event to deal with this, the update or...
0
by: Wannabe | last post by:
I have been trying to figure this out for days now...can someone please help? I am using ASP.Net 2.0 and have a gridview on my page. I have everything working except the delete command. The page...
3
by: dhaneshrs | last post by:
I have a gridview i managed to update using the edit option in the smartmenu. The problem comes when i delete a value or make a value read only. Say i have a table Employee with the following...
8
by: Frank Millman | last post by:
Hi all This is not strictly a Python question, but as the system to which relates is written in Python, hopefully it is not too off-topic. I have an accounting/business application, written in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...

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.