Jump to content

Joomla JFile and JFolder - PHP


simpsonnth

Recommended Posts

Hi there,

I have decided using the Joomla API however, the documentation was for 1.5 and 2.5 system only but i'm using 3.0. I have a number of files that look like this:

"14-05-14 JoePharnel.pdf"

and

"13-05-12 HarryCollins.pdf"

Basically I want to create a PHP code that when someone ftp uploads those files to the upload folder that it will 1) Create a directory if it doesn't exist using there name 2) Move the files to the correct directory (E.g. JoePharnel to the JoePharnel Directory ignoring the number at the beginning)

My new code creates the folder but won't move the file in the upload into that new folder, code is below:

<?php

define
( '_JEXEC', 1);

define('JPATH', dirname(__FILE__) );

if (!defined('DS')){
define( 'DS', DIRECTORY_SEPARATOR );


$parts = explode( DS, JPATH );
$script_root = implode( DS, $parts ) ;

// check path
$x = array_search ( 'administrator', $parts );
if (!$x) exit;

$path = '';

for ($i=0; $i < $x; $i++){
$path = $path.$parts[$i].DS;
}
// remove last DS
$path = substr($path, 0, -1);

if (!defined('JPATH_BASE')){
define('JPATH_BASE', $path );
}

if (!defined('JPATH_SITE')){
define('JPATH_SITE', $path );
}

/* Required Files */
require_once ( JPATH_SITE . DS . 'includes' . DS . 'defines.php' );
require_once ( JPATH_SITE . DS . 'includes' . DS . 'framework.php' );
require_once ( JPATH_SITE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );


//Import filesystem libraries. Perhaps not necessary, but does not hurt

jimport('joomla.filesystem.path');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.user.user');



//First we set up parameters
$searchpath = JPATH_BASE . DS . "upload";





//Then we create the subfolder called png
if ( !JFolder::create($searchpath . DS ."Images") ) {
//Throw error message and stop script
}

//Now we read all png files and put them in an array.
$png_files = JFolder::files($searchpath,'.png');

//Now we need some stuff from the JFile:: class to move all files into the new folder
foreach ($png_files as $file) {
JFile::move($searchpath. DS . ".png" . $file, $searchpath . DS. "Images" . $file);
}

//Lastly, we are moving the complete subdir to the root of the component.
if (JFolder::move($searchpath . DS. "Images",JPATH_COMPONENT) ) {
//Redirect with perhaps a happy message
} else {
//Throw an error

}
}


?>

Only error i get is Notice: Use of undefined constant JPATH_COMPONENT - assumed 'JPATH_COMPONENT' in /upload.php on line 70. 

I have used and adapted multiple php code so any help would be appreciated. Or if you can point me to where i can find the answer that would be brilliant

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.