Recent Topics

help deleting uploaded images / files

Started by on Mar 14, 2005 – Contents updated: Mar 14, 2005

Mar 14, 2005 14:36    

Hello,
I've found that all images and files that have been uploaded with the posts are not deleted when the posts are deleted.
I want to have 3 private blogs for 3 different groups, all their uploaded files go to the media dir, and it could get very big if the files stay there forever :-/

Any sugestions ?

Thanks in advance

Mar 14, 2005 16:48

Log into your ftp server and delete them from there?

Mar 14, 2005 18:09

Log into your ftp server and delete them from there?

Thanks Graham, but it is not practical.
There are several people in each group posting with attach files and images. All have permisions to delete their posts, all the attacments/images uploaded by any person from any group are uploaded to the media dir. I can not keep track of what files in the media dir are from deleted posts.

Surely there must be another way... :-/ ++

Mar 14, 2005 19:07

You could hack admin/edit_actions.php to scan the content of the post when it's being deleted and extract the image filenames and delete them

?

Mar 14, 2005 19:23

You could hack admin/edit_actions.php ...

I was afraid that I had to go deep down into php code :(
Any idea where to start in admin/edit_actions.php?

Help will be apreciated.

Mar 14, 2005 19:35

This is the section that does the deleting :-

	case 'delete':
		/*
		 * --------------------------------------------------------------------
		 * DELETE a post from db
		 */
		$admin_pagetitle = T_('Deleting post...');
		require( dirname(__FILE__) . '/_menutop.php' );
		require( dirname(__FILE__) . '/_menutop_end.php' );

		param( 'post', 'integer' );
		// echo $post;
		if( ! ($postdata = get_postdata( $post )) )
		{
			echo '<div class="panelinfo"><p class="error">'.( T_('Oops, no post with this ID!') ).'</p></div>';
			break;
		}
		$blog = get_catblog( $postdata['Category'] );
		$blogparams = get_blogparams_by_ID( $blog );
		$location = 'b2browse.php?blog='.$blog;

		// Check permission:
		$current_User->check_perm( 'blog_del_post', '', true, $blog );

		echo "<div class=\"panelinfo\">\n";
		echo '<h3>', T_('Deleting post...'), "</h3>\n";

		// DELETE POST FROM DB:


		if( bpost_delete( $post ) )
		{
			if( isset($sleep_after_edit) && $sleep_after_edit > 0 )
			{
				echo '<p>', T_('Sleeping...'), "</p>\n";
				flush();
				sleep( $sleep_after_edit );
			}
			
			echo '<p>'.T_('Deleting Done...')."</p>\n";
		}
		else
		{
			echo '<p>'.T_('Error')."!</p>\n";
		}

		echo '</div>';
		
		break;

You can get the content of the post with :-
$postdata['Content']

Mar 14, 2005 19:46

You can get the content of the post with :-
$postdata['Content']

Thanks...

I'll try but my knoledge of php is very very poor.
I'm sure I'll need to cry for help... :'(

Mar 14, 2005 20:02

Lol, know how you feel regarding PHP, my knowledge of it is limited to say the least.

If you get stuck holler back and I'll see what I can do (might take a tad though)

?

Mar 14, 2005 20:09

I'm stuck already... :(
I start with

echo $postdata['Content'];


to see the content but I don't get any value...

Mar 14, 2005 20:40

Backup your images and admin/edit_actions.php

Then add the red bits, hit delete and cross your fingers :p

case 'delete':
/*
* --------------------------------------------------------------------
* DELETE a post from db
*/
$admin_pagetitle = T_('Deleting post...');
require( dirname(__FILE__) . '/_menutop.php' );
require( dirname(__FILE__) . '/_menutop_end.php' );

param( 'post', 'integer' );
// echo $post;
if( ! ($postdata = get_postdata( $post )) )
{
echo '<div class="panelinfo"><p class="error">'.( T_('Oops, no post with this ID!') ).'</p></div>';
break;
}
$blog = get_catblog( $postdata['Category'] );
$blogparams = get_blogparams_by_ID( $blog );
$location = 'b2browse.php?blog='.$blog;

// Check permission:
$current_User->check_perm( 'blog_del_post', '', true, $blog );

echo "<div class=\"panelinfo\">\n";
echo '<h3>', T_('Deleting post...'), "</h3>\n";

// DELETE POST FROM DB:

if( bpost_delete( $post ) )
{
if( isset($sleep_after_edit) && $sleep_after_edit > 0 )
{
echo '<p>', T_('Sleeping...'), "</p>\n";
flush();
sleep( $sleep_after_edit );
}

// delete files in post hack start
if(strpos('media',$postdata['Content'])){
echo "<p>Deleting files</p>";
$theImages=explode('media/',$postdata['Content']);
foreach($theImages as $theFile){
$theFile="../media/".substr($theFile,0,strpos($theFile,'"'));
unlink(@$theFile);
}
echo "<p>Files deleted</p>";
}
// end of delete files in post hack

echo '<p>'.T_('Deleting Done...')."</p>\n";
}

?

Mar 14, 2005 21:26

I'm going in the right way but still not there.

Thanks wery much for the code.
First it did not work at all, but I noticed that t

strpos('media',$postdata['Content']))


should really be

strpos($postdata['Content'],'media'))

Now it deletes the posts and the files, but I get the following error:


Deleting files
Warning: unlink(../media/<img src="%29:" no="" such="" file="" or="" directory="" in="">/var/www/html/fer20/admin/edit_actions.php on line 451
Files deleted

Mar 14, 2005 21:31

To be honest, you've about hit the limit of my php knowledge, the code I posted worked for me, but I run a windows server so their may be something I'm missing if you're on a linux box.

?

*edit* try adding :-
echo $theFile;

just before it tries to delete it, it looks like it's not getting the filename properly

Mar 14, 2005 22:26

:D :D ++
BINGO!
That's right, in the loop of the foreach, $theFile gets some other values as well as the file names.
The ulink was deleting the files and giving a warning when it didn't encounter a file.
I've solve it with:

 
// delete files in post hack start
			if(strpos($postdata['Content'],'media')){
			echo "<p>Deleting files</p>";
			$theImages=explode('media/',$postdata['Content']);
			foreach($theImages as $theFile){
			$theFile="../media/".substr($theFile,0,strpos($theFile,'"'));
			if (file_exists($theFile)) {unlink("$theFile") or die ("The  file $theFile can not be deleted!");}
					}
			echo "<p>Files deleted</p>";
			}
			// end of delete files in post hack 

     


Now it looks like it works .
Platform: LAMP (Linux, Apache, Msql and PHP)

Thanks again
Fernando
P.S.: I'm sure that a php guru would have done it without any warnings

Mar 14, 2005 23:04

Assuming you mean the warning as the file is deleted, try changing :-
if (file_exists($theFile)) {unlink("$theFile") or die ("The file $theFile can not be deleted!");}

to :-
if (file_exists($theFile)) unlink(@$theFile);

?


Form is loading...

Social CMS software – This forum is powered by b2evolution CMS, a complete engine for your website.