fckeditor delete file/folder

By default the fckeditor file browser that comes with it doesn't allow file deleting or folder deleting because of the security risk. Its a bit of a pain but it is possible to get the file browser to delete files. Here is how I went about it for php:

edit "editor/filemanager/connectors/php/commands.php" and add:

PHP:
  1. function DeleteFile( $resourceType, $currentFolder ) {
  2.     $file = $_SERVER['DOCUMENT_ROOT'].$_GET['FileUrl'];
  3.     if (is_file($file)) {
  4.         unlink($file);
  5.     } else {
  6.         echo '<error number="1" originaldescription="unable to locate file">' ;
  7.     }
  8. }
  9. function DeleteFolder( $resourceType, $currentFolder ) {
  10.     $folder = $_SERVER['DOCUMENT_ROOT'].$_GET['FolderName'];
  11.     if (is_dir($folder) )   {
  12.         DELETE_RECURSIVE_DIRS($folder);
  13.     } else {
  14.         echo '<error number="2" originaldescription="unable to locate folder">' ;
  15.     }
  16. }
  17. function DELETE_RECURSIVE_DIRS($dirname) { // recursive function to delete
  18.     // all subdirectories and contents:
  19.     if(is_dir($dirname))$dir_handle=opendir($dirname);
  20.     while($file=readdir($dir_handle)) {
  21.         if($file!="." && $file!="..") {
  22.             if(!is_dir($dirname."/".$file)) {
  23.                 unlink ($dirname."/".$file);
  24.             } else {
  25.                 DELETE_RECURSIVE_DIRS($dirname."/".$file);
  26.             }
  27.         }
  28.     }
  29.     closedir($dir_handle);
  30.     rmdir($dirname);
  31. }

modify editor/filemanager/connectors/php/connector.php so that the command switch looks like this:

PHP:
  1. switch ( $sCommand ) {
  2.     case 'GetFolders' :
  3.         GetFolders( $sResourceType, $sCurrentFolder ) ;
  4.     break ;
  5.     case 'GetFoldersAndFiles' :
  6.     GetFoldersAndFiles( $sResourceType, $sCurrentFolder ) ;
  7.     break ;
  8.     case 'CreateFolder' :
  9.         CreateFolder( $sResourceType, $sCurrentFolder ) ;
  10.     break ;
  11.     /******ADDED DELETE COMMANDS******/
  12.     case 'DeleteFile' :
  13.         DeleteFile( $sResourceType, $sCurrentFolder ) ;
  14.     break ;
  15.     case 'DeleteFolder' :
  16.         DeleteFolder( $sResourceType, $sCurrentFolder ) ;
  17.     break ;
  18. }

modify editor/filemanager/connectors/php/config.php allowed commands:

PHP:
  1. $Config['ConfigAllowedCommands'] = array('QuickUpload', 'FileUpload', 'GetFolders', 'GetFoldersAndFiles', 'CreateFolder', 'DeleteFile', 'DeleteFolder') ;

You will also need to make sure that this connector is enabled in this file also but you knew that already didn't you?

PHP:
  1. $Config['Enabled'] = true ;

modify editor/filemanager/browser/default/frmresourceslist.html.html so that the functions look like this:

PHP:
  1. oListManager.GetFolderRowHtml = function( folderName, folderPath, folderUrl ) //added folderUrl
  2. {   
  3.     // Build the link to view the folder.
  4.     var sLink = '<a href="#" onclick="OpenFolder(\'' + folderPath.replace( /'/g, '\\\'') + '\');return false;">' ;
  5.  
  6.     return '<tr>' +
  7.             '<td width="16">' +
  8.                 sLink +
  9.                 '<img alt="" src="images/Folder.gif" width="16" height="16" border="0"></a>' +
  10.             '</td><td nowrap colspan="2">&nbsp;' +
  11.                 sLink +
  12.                 folderName +
  13.                 '</a>' +
  14.         '</td><td align="right"><a href="#" onclick="DeleteFolder(\''+folderName+'\',\''+ folderUrl.replace( /'/g, '\\\'') + '\');return false;">DELETE</a></td></tr>' ;
  15. }
  16.  
  17. oListManager.GetFileRowHtml = function( fileName, fileUrl, fileSize )
  18. {      
  19.     // Build the link to view the folder.
  20.     var sLink = '<a href="#" onclick="OpenFile(\'' + fileUrl.replace( /'/g, '\\\'') + '\');return false;">' ;
  21.  
  22.     // Get the file icon.
  23.     var sIcon = oIcons.GetIcon( fileName ) ;
  24.  
  25.     return '<tr>' +
  26.             '<td width="16">' +
  27.                 sLink +
  28.                 '<img src="'+fileUrl+'" border="0" style="border:1px solid black; margin:5px;" alt="" height="70" /></a>' +
  29.             '</td><td>&nbsp;' +
  30.                 sLink +
  31.                 fileName +
  32.                 '</a>' +
  33.             '</td><td align="right" nowrap>&nbsp;' +
  34.                 fileSize +
  35.                 ' KB' +
  36.         '</td><td align="right"><a href="#" onclick="DeleteFile(\''+fileName+'\',\'' + fileUrl.replace( /'/g, '\\\'') + '\');return false;">DELETE</a></td></tr>' ;
  37. }
  38.  
  39. function DeleteFile( fileName, fileUrl )
  40. {
  41.     if (confirm('Are you sure you wish to delete ' + fileName + '?')) {
  42.         oConnector.SendCommand( 'DeleteFile', "FileUrl=" + escape( fileUrl ), Refresh ) ;
  43.     }
  44.  
  45. }
  46.  
  47. function DeleteFolder( folderName, folderPath )
  48. {
  49.     if (confirm('Are you sure you wish to delete \'' + folderName + '\' and all files in it?')) {
  50.         oConnector.SendCommand( 'DeleteFolder', "FolderName=" + escape( folderPath + folderName ), Refresh ) ;
  51.     }
  52. }

*NOTE* This will also show a thumbnail image - you can change the function to turn this off if you want *NOTE*
oh and also in the function modify this..

PHP:
  1. oHtml.Append( oListManager.GetFolderRowHtml( sFolderName, sCurrentFolderPath + sFolderName + "/" ) ) ;

to be this...

PHP:
  1. oHtml.Append( oListManager.GetFolderRowHtml( sFolderName, sCurrentFolderPath + sFolderName + "/", sCurrentFolderUrl ) ) ;

I think that is all.. if I missed anything let me know and I will update this quide.

18 Responses to “fckeditor delete file/folder”


  1. 1 gwsmith

    I was led here by search for flex file manager through Google. It led me to a Flex file manager that I think is really cool how can I get the source code? I need to build the same thing. I currently use the asfusion file explorer but it has issues I cannot resolve for some users. So I need to make it like flex and I see its already been done. Can you help me?

    George Smith

  2. 2 hiepsixike

    oh, so great. Thank you so much

  3. 3 LoC101

    Could this ‘patch’ be posted as a download ;-)

    LoC

  4. 4 marc

    yeah, but then I would need to update the download with new releases of fck :-)

  5. 5 LoC101

    What about a full download :)

    Or.. which version are we talking about? Might be that I am trying to fix it in the wrong version (latest.. uuhhmm 2.5.1 or something like that. But tne again, wehere would i dl an older version…

    pretty annoying that i cant delete files with fck.

    LoC

  6. 6 marc

    This should work with the latest version unless I missed something. I’m fairly certain I documented all of my steps. Are you stuck?

  7. 7 oleonav

    Nice solution Marc,

    I works fine in version 2.5

    Version 2.5.1 (latest); it gives a js error in firefox (not tested other browsers)
    > folderUrl is not defined
    http://EXAMPLE.COM/js/fckeditor/editor/filemanager/browser/default/frmresourceslist.html
    Line 58

    Also I found a little bug in your code for commands.php

    Your code:
    —–
    function DELETE_RECURSIVE_DIRS($dirname) { // recursive function to delete
    // all subdirectories and contents:
    if(is_dir($dirname))$dir_handle=opendir($dirname);

    —–
    if(is_dir($dirname))$dir_handle=opendir($dirname);
    —–
    should read:
    if(is_dir($dirname)){
    $dir_handle=opendir($dirname);

  8. 8 Seb

    Thanks a lot ! Very usefull

    Two little things :

    -In the “editor/filemanager/connectors/php/commands.php” file you show, i think a “{” has been lost on line 19 (function DELETE_RECURSIVE_DIRS).

    -In the same file, the && need to be decoded on line 21.

  9. 9 marc

    Thanks Seb, I’ve fixed up those errors.

  10. 10 Dave Brondsema

    Thanks! This is just what I was looking for. I made some additional changes to avoid an error when creating a new folder. In frmresourceslist.html add to the top of oListManager.GetFolderRowHtml

    // when a folder is created
    if (folderUrl == null) {
    folderUrl = ”;
    }

    And to use thumbnails in Image mode and icons in all other modes, edit frmresourceslist.html and before ‘return’ of oListManager.GetFileRowHtml add:

    var thumbnail = ‘images/icons/’ + sIcon + ‘.gif’;
    var height = 16;
    if (oConnector.ResourceType == ‘Image’) {
    thumbnail = fileUrl;
    height = 70;
    }
    and make the ‘ +

  11. 11 Dave Brondsema

    The end of my comment should read like this (replace { and } with a tag angles):
    and make the {img line of the return statement be:
    ‘{img src=”‘+thumbnail+’” border=”0″ alt=”" height=”‘ + height + ‘” /}{/a}’ +

  12. 12 LoC101

    Fixed… no idea what went wrong initialy.. guess I needed that short leave to clear the mind.

    Doing some additions and changes to the code.. if ur intrested email me.

    LoC

  13. 13 Danny

    You are great Dude… I love this addon… Keep up the good work!!

  14. 14 Fukumen Rider

    Hello from Japan.
    Thanks for nice scripts.

    >Mr.Dave
    I modified look like this.

    var thumbnail = ‘images/icons/’ + sIcon + ‘.gif’;
    var height = 16;

    if (sIcon == ‘jpg’ || sIcon == ‘gif’ || sIcon == ‘png’ || sIcon == ‘bmp’) {
    thumbnail = fileUrl;
    height = 70;
    }

  15. 15 Fukumen Rider

    and I modified deleteFile&Folder function,

    global $Config ;
    $file = $Config[’UserFilesAbsolutePath’]…..

    if “$_SERVER[’DOCUMENT_ROOT’]”,I can delete other files.

  16. 16 dimants

    great thanx!

  17. 17 Kristaps

    Hi Marc! Nice job you’ve done. But it didn’t actually delete my files or folders because $_SERVER[’DOCUMENT_ROOT’].$_GET[’FileUrl’] wasn’t the place where my files were located so I took idea from Fukumen Rider
    and modified your code like this:
    in the view i just pass file/folder - names:
    [code]
    function DeleteFile( fileName )
    {
    if (confirm(’Are you sure you wish to delete ‘ + fileName + ‘?’)) {
    oConnector.SendCommand( ‘DeleteFile’, “FileName=” + escape( fileName ), Refresh ) ;
    }

    }
    [/code]
    in commands.php
    [code]
    function DeleteFile( $resourceType, $currentFolder ) {
    global $Config ;
    $file = $Config[’UserFilesAbsolutePath’].strtoupper($resourceType[0]).substr($resourceType,1).’/’.$_GET[’FileName’];
    if (is_file($file)) {
    unlink($file);
    } else {
    echo ” ;
    }
    }
    [/code]

  18. 18 Fabrizio

    Great patch, I think you should post it to FCK dev team here:
    http://dev.fckeditor.net/ticket/354
    cause it should be included in the default distribution

Leave a Reply