See my last question as this links to it: PHP - Moving multiple files with different files names to own directory
So i decided to 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:
"2005532-JoePharnel.pdf"
and
"1205121-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)
Updated: 23/10/14 - 14:05:
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. But doesn't stop it working, am so close on this any help is greatly appreciated. I want to know where its taking the image, i think i have worked out the "DS" now.
Thanks
Related
So I am trying to set up path for my pdf files that are stored in a folder structure. The file that has to be selected depends on the user input.
I want to set up first the absolute path and then a folder pattern.
The folder where I have stored my files is:
C:\Apache24\htdocs\archivedb\Tourenfahrer\2017\5
The last three folders changes as per user input.
I have setup my root directory in my config.php like this:
define( 'ROOT_DIR', dirname(__FILE__) );
Now in my php file I am recieving my variables, which will be my folder names to search for the files.
<?php
require_once 'conf/config.php';
if (!empty($_REQUEST['magName'] && $_REQUEST['year'] && $_REQUEST['issue']
)) {
$magazineName = $_REQUEST['magName'];
$year = $_REQUEST['year'] ;
$issue = $_REQUEST['issue'] ;
}
Now Please tell how to access the respective folder using these variables?
and how can i set up a pattern with this using glob(); ?
Please try this
define('ROOT_DIR', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
if (!empty($_REQUEST['magName'] && $_REQUEST['year'] && $_REQUEST['issue'])) {
$magazineName = $_REQUEST['magName'];
$year = $_REQUEST['year'];
$issue = $_REQUEST['issue'];
$dir = $magazineName . DS . $year . DS . $issue;
echo "<h3>Use scandir:</h3>";
$files = scandir(ROOT_DIR . DS . $dir);
foreach ($files as $file) {
echo basename($file) . "<br>";
}
echo "<h3>Use glob:</h3>";
foreach (glob($dir . DS . '*') as $filename) {
echo basename($filename) . "<br>";
}
}
Right so i'm making a configuration class that will use an array of different file types in 4 main locations. Now I want to make it so that the configuration class will search these locations in order at the moment i'm using the following
if (file_exists(ROOT . DS . 'Application/Config/' . APP_ENV . DS . $file)) {
$this->filePath = ROOT . DS . 'Application/Config/' . APP_ENV . DS . $file;
echo $this->filePath;
} else {
if (file_exists(ROOT . DS . "Application/Config/$file")) {
$this->filePath = ROOT . DS . "Application/Config/$file";
echo $this->filePath;
} else {
if (file_exists(CARBON_PATH . 'Config' . DS . APP_ENV . DS . $file)) {
$this->filePath = CARBON_PATH . 'Config' . DS . APP_ENV . DS . $file;
echo $this->filePath;
} else {
if (file_exists(CARBON_PATH . "Config/$file")) {
$this->filePath = CARBON_PATH . "Config/$file";
echo $this->filePath;
} else {
throw new \Exception("Unable to locate: $file, Please check it exists");
}
}
}
}
pretty messy and not very flexible.
What I want to be able to do is search the locations in the same order BY FILE NAME ONLY after finding the first match It would then return the file with the extension for the configuration class to use the correct method to parse into a php array and so on.
What is the best way to search these locations for a file name
Example
Say we want a database configuration file as you can see there are 2
ConfigLocation1/Dev/
/file.php
/database.json
ConfigLocation1/
/database.ini
/anotherfile.json
I would want to use the function like so
config::findFile('database');
and it return
$result = ConfigLocation1/Dev/database.json
but if it wasnt found here then then
$result = ConfigLocation1/database.ini
Not very good at explaining things so hope the example helps
As you mentioned you need to check for file in 4 locations, so instead of if conditions, create an array of directories and loop through.
and you can use glob, to find a file irrespective of extension. see my example below:-
//Make a array of directory where you want to look for files.
$dirs = array(
ROOT . DS . 'Application/Config/' . APP_ENV . DS,
CARBON_PATH . 'Config' . DS . APP_ENV . DS
);
function findFiles($directory, $filename){
$match = array();
foreach ($directory => $dir) {
$files = glob($dir.$filename);
foreach ($files as $file) {
$match[] = $file;
}
}
return $match;
}
// to find database
$results = findFiles($dirs, 'database.*');
This used to work in 1.5... I could set up the following code:
function startJoomla() {
define('_JEXEC', true);
define( 'DS', DIRECTORY_SEPARATOR );
$dir= dirname(__FILE__);
define('JPATH_BASE', $dir);
// load joomla libraries
require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
require_once JPATH_LIBRARIES . DS . 'loader.php';
jimport('joomla.base.object');
jimport('joomla.factory');
jimport('joomla.filter.filterinput');
jimport('joomla.error.error');
jimport('joomla.event.dispatcher');
jimport('joomla.event.plugin');
jimport('joomla.plugin.helper');
jimport( 'joomla.utilities.utility' );
jimport('joomla.utilities.arrayhelper');
jimport('joomla.environment.uri');
jimport('joomla.environment.request');
jimport('joomla.user.user');
// JText cannot be loaded with jimport since it's not in a file called text.php but in methods
JLoader::register('JText', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
JLoader::register('JRoute', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
$mainframe = & JFactory::getApplication('site');
$GLOBALS['mainframe'] = & $mainframe;
return $mainframe;
}
$mainframe = startJoomla();
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
And then, when I wanted to call something up from Joomla, I could do the following:
$custom = new CustomController(); // assuming there's a com_custom somewhere
This worked for 1.5. But in 2.5, the above doesn't work at all. I found a revised version of the load script:
function startJoomla() {
define('_JEXEC', 1);
define( 'DS', DIRECTORY_SEPARATOR );
$dir = dirname(__FILE__);
define('JPATH_BASE', $dir);
// load joomla libraries
require_once JPATH_BASE.'/includes/defines.php';
require_once JPATH_BASE.'/includes/framework.php';
require_once JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php';
// Instantiate the application.
$app = JFactory::getApplication('site');
// Initialise the application.
return $app->initialise();
}
$mainframe = startJoomla();
This enables things like 'JFactory::getUser();' but will no longer allow me to utilize commands like 'new MycustomcomponentController();' Gives me 'core' Joomla functionality, but skips everything I installed. Is there an appropriate new revised compilation of jimport instructions or perhaps a different function of JComponent that allows me to call up my custom functions along with other components and models for use in external php files? Assume this will be run from a CRON or similar.
I'm trying to copy my uploaded file to another directory called img folder with following code. But it doesn't work properly. I don't know why ? can you help me plz ?
php Code:
if($image["name"] != "")
{
//$path = PATH . DS . "uploads" . DS . "products" . DS . $id;
$path = "../../uploads" . DS . "products" . DS . $id;
$path2 = "img";
if(!is_dir($path))
{
mkdir($path);
}
chmod($path, 0755);
//move_uploaded_file($image["tmp_name"], $path . DS . $image["name"]);
move_uploaded_file($image["tmp_name"], $path . DS .
$uploadImage);//exit;
copy($uploadImage, $path2);
}
Following error message show:
Warning: copy(249.jpg) [function.copy]: failed to open stream: No such file or directory in...
The copy() function needs the full file path for the source file; you're just passing the filename, not the path.
As things stand, it's looking in the current folder for the file, not finding it, and throwing the error as a result.
From the previous line of code, it looks like your full path should be $path . DS . $uploadImage, so the copy command should look like this:
copy($path . DS . $uploadImage, $path2);
hope that helps.
Your copy function is wrong...
copy($uploadImage, $path2);
As Spudley answer says, you have to use the full path of the image. Also, $path2 is a directory. You have to give a name for the new copy of the image.
So, your copy function would be as follows:
copy($path . DS . $uploadImage, $path2. DS . $uploadImage);
Try it and let us know.
I don't know if this is exactly a problem in autoloading, but I am having this problem, here is my code:
index.php
require __DIR__ . '/app/autoload.php';
Folder structure:
index.php
app/
--autoload.php
autoload.php
function autoloader($className) {
// List Directories to Autoload Classes
$paths = array(
__DIR__ . '/system/',
__DIR__ . '/app/models/',
__DIR__ . '/app/dao/'
);
foreach($paths as $path) {
$file = $path . '/' . $className . '.php';
if (is_file($file))
include $file;
}
}
For some reason it doesn't work even I do:
__DIR__ . '../system/
... et al.
DIR in the autoload file will refer to /app.
try:
function autoloader($className) {
// List Directories to Autoload Classes
$paths = array(
'../system/',
'/models/',
'/dao/'
);
foreach($paths as $path) {
$file = $path . '/' . $className . '.php';
if (is_file($file))
require_once $file;
}
}
If that fails start echoing out paths and dir to see if they are referenced properly.
I would try it this way.
Anywhere before the autoload.php is loaded (should be file which is always loaded, i guess in your case its index.php) i would define ROOT variable somwhere at begining of the script
/**
* Use the DS to separate the directories (just a shortcut)
*/
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
/**
* The full path to the directory which holds application files, without a trailing DS.
*/
if (!defined('ROOT')) {
define('ROOT', dirname(__FILE__));
}
and then in your autoload use ROOT
$paths = array(
ROOT . DS. 'system' . DS,
ROOT . DS. 'app' . DS . 'models' . DS,
ROOT . DS. 'app' . 'dao' . DS
);