close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Delete orphaned files and images

Thread began 10/27/2009 10:45 pm by troyd | Last modified 11/02/2009 9:04 pm by troyd | 7435 views | 16 replies |

troyd

Delete orphaned files and images

I'd like to create a page that shows all images contained in a directory, and then have the option of deleting them. Most of what I have read here describes using a record set and the column name for the images.

But what about deleting images that no longer appear in any table? When a record is deleted, the image file is still in a directory. After a while they build up. Is there a way to deal with them using DFP's delete behavior?

And while I'm wishing, it would be really cool to flag those images that ARE listed in a table so they do not get deleted by accident.

Thank You,
TroyD

Sign in to reply to this post

Jason ByrnesWebAssist

You should add the delete file functionality to the delete record page so that when a record is deleted, the associated files will also be deleted.

Sign in to reply to this post

troyd

Yes, but that won't always fit the application.

In one case, I have a user that wants to use a directory for uploading photos displayed in forums and a gallery. So the photos will be uploaded and used without any actual records. Basically he wants to host his own images.

In a second case, there is more than one table and different records might reference one photo. If they delete a record from one table and it includes a delete file behavior, then the other table loses the image. So I was hoping this could be a separate part of the CMS that only deals with a folder of images.

Thanks,
Troy

Sign in to reply to this post

troyd

Brian,

I was posting my last reply as you were. Thanks, I will try that if I can figure it out. I did read your suggestion on a previous post and had trouble figuring it out.

On the subject, are there any security issues I should know about? I ask because in my searches, I came across a couple of people who warned newbies that if they didn't set this type of scandir or unlock up correctly, it opened them up to someone from outside the server, deleting all their files. Have you heard of this?
I can't find the reference now, but it had something to do with using .. or / in your code.

TroyD

Sign in to reply to this post

troyd

Wow, I did it!

Using a combination of suggestions from Jason, Brian and some from "Vacunita" in another PHP forum (would like to give credit for them but not sure if it's allowed), I now have a single page that sees all files in a directory, displays them with check boxes and deletes them if checked. No Record Set or Table required.

Basically I used DFP's delete file behavior but instead of referencing a hidden field, I referenced the checkbox value. And the check box value is assigned dynamically.

I set the form action to "" and post. Then set the trigger for the DFP behavior to "any form post".

Now I just need to figure out how to show ONLY image files. Because, as it is now, all files are showing up and can be deleted.

Here's a generic example of what I did with everyone's help. Thanks again, TroyD

php:
<?php require_once("WA_DigitalFilePro/HelperPHP.php"); ?>

<?php
$WA_DeleteFileResult1 
false;
if(
$_SERVER["REQUEST_METHOD"] == "POST"){
    
$WA_DeleteFileResult1 WA_FileAssist_DeleteFile("images/""".((isset($_POST["image"]))?$_POST["image"]:"")  ."");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
img {
    width: 150px;
    }
-->
</style>
</head>
<body>
<form  action="" method="post" enctype="multipart/form-data">
  <?php
$mydir
="images/";
if (
$handle opendir($mydir)) {

    echo 
"Images:\n <table><tr><td>Delete</td><td>Filename</td></tr>";

    while (
false !== ($file readdir($handle))) {
    
    
        echo 
"<tr><td><input type='checkbox' name='image' value='$file'></td><td><img  src='$file'></td><td><img  src='images/$file' /></td><td>$file</td></tr>";
    }
echo 
"</table>";

   
closedir($handle);
}
?>
  <input name="Delete" type="submit" value="Delete" />
</form>
</body>
</html>



One thing to note for anyone still learning like me. When you go to the DFP delete behavior and click on the lightning bolt next to File name:, you won't see the check box as an option. That's because it's not recognized yet by the form since it has to be echoed I think. So I just typed in the name of the check box, in this case "image". Once it's on the server, then the check box becomes a recognized form element. (If that's not correct, please let me know).

Sign in to reply to this post

anonymous

Troy,

Use PHP's getimageinfo command to see if their images... you don't need even to get any info actually, you just want to confirm that they are images... do this like this:

if (getimageinfo(filename)) {

echo the output here which will display the image

}

Of course to make it easier you may want to close the php and reopen it to avoid a coding mess like this:

<?php

if (getimageinfo(filename)) { ?>

standard html with php echo commands added in


<?php } ?>


?>

I think you should use this method rather than the way did on the last post where you are echoing all that html... it could lead to a real headache in the future.

Regards,

Brian

Sign in to reply to this post

troyd

Brian,

Thanks. Yes, I saw your suggestion for this on a previous post. I know it's exactly what I need but my current level of knowledge isn't enough to apply this and make it work. I know that what you have typed here is arbitrary and I should swap out my values. But I can't figure out where to insert it in the working code I have now. Or if it replaces parts of it.

Your help is very much appreciated, it's me that can't get it to work. Some day maybe I'll laugh at the fact that I couldn't look at that and know right away what to do with it. Any chance you could elaborate a little more?

Where you say

php:
<?php


if (getimageinfo(filename)) { ?>

standard html with php echo commands added in


<?php ?>


?>



Should I add the current echo for the table there? Or is this in place of that?

Thanks again,
TroyD

Sign in to reply to this post

troyd

HELP!..please

Brian,

Sorry to be such a pain. Not sure what I did wrong here. I installed MAMP Pro (demo) and screwed something up. The code above that I had working to show images in a directory, then delete them with the DFP delete behavior has quite working.

The images still display along with the check boxes but when I click the delete button and submit, nothing gets deleted.

I tested this file on a remote server under a different domain and it works great once again, but only there.

So, I have something screwed up in either MAMP Pro, or in my site settings for my Sandbox_PHP web site.

My first thought is that the WA_DigitalFilePro/HelperPHP.php file isn't being called up. I've check everything I can think of but I'm lost.

What can I add to the require_once line that will give me an error message if the file is not being loaded? I assumed that if it wasn't I would get an error anyway. So that may not be it. So, something is in error with a path somehow, but where should I look?

My site settings are as follows; (BTW, DW doesn't let me use a "/" in my paths for folders. It want's a ":").
Local Info-
Local root folder: MACHDD:Users:myname:Desktop:WebSites:Sandbox_PHP:
Links relative to: Document
HTTP address: http://localhost/Sandbox_PHP/

Remote Info-
Access: None

Testing Server-
Server Model: PHP Mysql
Access: Local/Network
Testing server folder: MACHDD:Applications:MAMP:htdocs:Sandbox_PHP:

URL Prefix: http://localhost/Sandbox_PHP/

MAMP Pro-
Apache is set on 80
MySQL is set on 3306
PHP 5
Zend Optimizer
Cache Off

Dynamic DNS Off

Hosts
Server Name: localhost
Local name resolution
Port: Server setting
Disk location- /Applications/MAMP/htdocs

Thanks for taking the time,
TroyD

Sign in to reply to this post

anonymous

Hi Troy,

No pain! No worries... I will help as I can.

The first question with MAMP Pro... after having installed you should be able to add "sites" or virtual hosts and actually define their location on your harddrive - making it irrelevant to where localhost actually resides.

Are you not able to click the "hosts" tab and register new hosts?

Brian

Sign in to reply to this post

troyd

Originally Said By: SOJO web
  Are you not able to click the "hosts" tab and register new hosts?  



Yes, and I know that you suggested that in another forum to me. But I was confused as to what to put where and didn't want to keep asking. I read the docs, but didn't grasp the Port I should use and whether I should be using a Dynamic DNS and why.

Also, there are 2 + signs. One is for Aliases. I read that it's the same settings as the new host but why would I have an alias, or should I?

I think the Port was the biggest puzzle for me. Since Apache is on 80 and MySQL is on 3306 what should I use? And do I create a new host for each site?
Some of my sites are just sandboxes for learning and testing, so there are no remote versions. And some are just local and remote, with no need for testing servers. Then there are those that need both.

I will happily try adding a new host and see if that fixes my DFP behavior during testing. What port should I use if the site folder is contained in the "Sites" folder under my username? Or does it matter?

Can I create one new server host called "WASandbox" for example and put all my php test files in their and then leave Port set to "Server setting" as it is? Or is "Server setting" a cue for me to put something in there?

Thanks again,
TroyD

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...