Moving a file to a new storage location programaticly

Permalink
I am trying to use the $f->setStorageLocation($item) method but am having problem defining $item.
What is $item ?
storage location ID? Storage location Name ?

Are there any code examples ?

I get this error when using the storage location name, but even though I get this warning it does not move the file to the new storage location.

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'FileVersion' does not have a method 'getFileStorageLocationID'

 
Mnkras replied on at Permalink Best Answer Reply
Mnkras
Its a file storage location object

for example:

$item = FileStorageLocation::getByID('derp');
afandino replied on at Permalink Reply
Understood but what is derp ? A storage location name?
Mnkras replied on at Permalink Reply
Mnkras
Its an Id, so 1, 2, 3, etc

On Sat, Nov 29, 2014, 10:03 AM concrete5 Community <
discussions@concretecms.com> wrote:
afandino replied on at Permalink Reply
M,

I got the method to set the path correctly but it does not seem to move the file to the correct folder. Below is a copy of the function I created. I need the file moved to the storage location. Does the method only set the path in the database ? That is what seems to be happening. I thought the method would actually move the file as well.

function file_upload() {

global $reportID;
global $reportLocation;
$questionName='UploadFileName';
Loader::library("file/importer");
Loader::model('file_storage_location');
$f = Loader::helper('concrete/file');
$fi = new FileImporter();
$newFile = $fi->import($_FILES[$questionName]['tmp_name'],
$_FILES[$questionName]['name']);
//get fileID

$reportID = $newFile->getFileID();

// move file to storage location
$f = File::getByID($reportID);
//$fsID = $f->getFileStorageLocationID();

$item = FileStorageLocation::getByID(1);

//$fsID = $f->getFileStorageLocationID();
//if (!$fsID > 0) {// move file to storage location
$f->setStorageLocation($item);
//}
$reportLocation = BASE_URL .ASSETS_URL_WEB .'/index.php/download_file/view_inline/'.$reportID.'/0/';

// add file to file set
Loader::model('file_set');
$fs = FileSet::getByName('TreasurersReports');
//$fs = FileSet::createAndGetSet('TreasurersReports', FileSet::TYPE_PUBLIC,
// $uID = false);
$fsf = $fs->addFileToSet($newFile);



}
Mnkras replied on at Permalink Reply
Mnkras
The default location is 1, did you want it somewhere else?

On Sat, Nov 29, 2014, 10:38 AM concrete5 Community <
discussions@concretecms.com> wrote:
afandino replied on at Permalink Reply
I thought the default was 0, when i view the object it was correct.
afandino replied on at Permalink Reply
Looked at the db file storage location and the location i want is ID 1
afandino replied on at Permalink Reply
M,

The idea is to move the file to a storage location that can not be accessed directly. This is not a file I want to be able to access directly. User must authenticate through concrete5 to view file.
Mnkras replied on at Permalink Reply
Mnkras
I understand that, by default the ID of the default (public) location is 1. if you go to
/dashboard/system/files/storage

and click on the location you want the secure files to be, the url will change to
/dashboard/system/files/storage/edit/<id here>

whatever is where <id here> is is what you need.
afandino replied on at Permalink Reply
working in ver, 5.6.32
afandino replied on at Permalink Reply
M,
Noticed something weird. On my development site, if I use file manager to reassign the storage location it does not move the file either. I am going to try my function on the prod. site to see if it works there.On the Prod. site I know the file manager storage change actually moves the file to the alternate location. The ID of 1 was assigning the correct path to the file.
afandino replied on at Permalink Reply
Works on the Production server. For some reason my webserver is not allowing a write to the alternate file location on the development site, Does not return any error in concrete
afandino replied on at Permalink Reply
Just in case anyone else finds this info helpful. In ver. 5.6.32 the default storage location is 0.
and the alternate location is 1. You may go directly to the DB and add more storage locations if you need them.