Forums

Moving files between folders

zperona 21 Dec, 2010
Hello, I'm trying to move a file (getting the file name from a textbox) from a folder to another. The folders must have the user name (that part looks to work), but I'm not sure where is the problem.


<?php
    $user =& JFactory::getUser();
    $username = $user->name;
	
    $file = 'text_5'; //<-- you get the file name from the form textbox

    $folder = JRequest::getString($username, '', 'post');
    $path1 = JPATH_SITE.DS.'images'.DS.'mainfolder'.DS.'all'.DS.$folder;
    $path2 = JPATH_SITE.DS.'images'.DS.'mainfolder'.DS.'all'.DS.'done'.DS.$folder;
    
    if ( !JFolder::exists($path2) ) {
        JFolder::create($path2);
    }
    $filename = basename($path1.DS.$file);
    JFile::move($path1.DS.$filename, $path2.DS.$filename);
    
?>
nml375 21 Dec, 2010
Hi,
You are actually using the hardcoded string 'text_5' as the filename, as opposed to the intended form input named 'text_5.'

Change the following line:
$file = 'text_5';

Into this:
$file = JFile::makeSafe(JRequest::getCmd('text_5'));


/Fredrik
zperona 21 Dec, 2010
Thanks for your reply, but It still doesn't work... The code creates the new folder correctly but still doesn't move the file
nml375 21 Dec, 2010
Hi,
I'm a bit puzzled why you use a form field name originating from the currently logged in user. You'd usually wan't form field names fixed.
In any case, I would recommend using the JFile::makesafe() classmethod to clean the input and avoid directory traversal bugs/exploits.

Apart from that, the code does seem proper. Have you tried adding some debug output to show the generated source and destination paths, and double-checked these against the filesystem and submitted data?

/Fredrik
zperona 21 Dec, 2010
Well, I have a file manager where I upload a file to my user folder and I only need a form to move it to another folder. It's very simple but I I'm gettin troubles to make it work correctly.

Thanks for answering
zperona 22 Dec, 2010
The error I get is this:

Fatal error: Class 'JFile' not found in /home/catbiore/public_html/components/com_chronocontact/libraries/customcode.php(51) : eval()'d code on line 18

Looking in the forum I found a code by Greyhead that looks very similar and works also:

<?php
    $user =& JFactory::getUser();
	$username = $user->name;
	
    $file = 'text_5';
	$carpeta1 = JPATH_SITE.DS.'images'.DS.'submissions'.DS.'all'.DS.$username;

    $folder = JRequest::getString($username, '', 'post');

    $target_folder = JPATH_SITE.DS.'images'.DS.'submissions'.DS.'all'.DS.'submitted'.DS.$username;

    if ( !JFolder::exists($target_folder) ) {
        JFolder::create($target_folder);
    }

    $filename = basename($carpeta1.DS.$file);

    if ( JFile::exists($carpeta1.DS.$filename) ) {
        JFile::move($carpeta1.DS.$filename, $target_folder.DS.$filename);
    }
?>

This creates the target folder but doens't move the file.
GreyHead 22 Dec, 2010
Hi zperona,

It's possible that you have FTP enabled in the Site Global Configuration. This sometimes seems to fail with File operations.

Otherwise I'd add debug code and go through step by step to pin down exactly where the failure is :-(

Bob
zperona 22 Dec, 2010
Thanks for your reply GreyHead. FTP layer is disabled, will try to debug it step by step as you say. Thanks again!
nml375 22 Dec, 2010
Hi,
You've forgotten to include the sources for JFile:
<?php
jimport('joomla.filesystem.file');
...


/Fredrik
zperona 22 Dec, 2010

Hi,
You've forgotten to include the sources for JFile:

<?php
jimport('joomla.filesystem.file');
...


/Fredrik



It works! thank you very much, I was getting mad at this... I owe you one sir.
This topic is locked and no more replies can be posted.