How to upload same file in different folder? - yii2

I have this following controller, I want to upload same file and save in different folder, but my second saveAs() didn't work.
$model->profil_picture_file = UploadedFile::getInstance($model,'profil_picture_file');
$model->profil_picture_file->saveAs('path to first folder'); //it works image saved
$model->profil_picture_file->saveAs('path to second folder'); //doesn't work not saved
Let me know what I'm doing wrong? and why it error ?
Thanks in advance.

Use it like this:
$model->profil_picture_file = UploadedFile::getInstance($model,'profil_picture_file');
$model->profil_picture_file->saveAs('path to first folder', false); //set deleteTempFile param to false
$model->profil_picture_file->saveAs('path to second folder'); //it will work now and delete temp file
saveAs() accept 2 params - file and deleteTempFile
Docs: Yii2 UploadedFile::saveAs()

because file saveAs use move_uploaded_file which move file, so the second saveAs doesn't work because file already moved. So, use copy('path to first folder', 'path to second folder')

Related

Eggplant : how to update a .csv file in senstalk

I am new to eggplant functional. Didn't find any reference to update a csv using eggplant functional.
Reference: http://docs.testplant.com/ePF/using/epf-accessing-data-cell-by-name.htm
You can read files in and out like so, and editing a CSV is the same as other text files.
To read a file into a variable:
put file "/etc/passwd" into passwordInfo
To write a variable to a file:
put "0,0,0,0" into file "/tmp/testing/counters"
What I would do is read in your CSV and copy to a new variable, then output it. There's pretty useful syntax for moving to a specific line in your file. For example, if you wanted to increment the second column of the first row of your file "counters", you could use:
add 1 to item 2 of line 1 of file "/tmp/testing/counters"
All of this and more is in "File and Folder Interaction" provided on the Eggplant website.

Create a txt file with SSIS showing File Movements

I need to create a .txt file every time and SSIS job runs and in that file I need to put the name of the files that I am transmitting. I have a foreach container but I am not sure on how to create the file and then write to it as each file is being moved. Can someone please steer me in the right direction? Thanks in Advance.
You can follow below steps:
Create a dummy empty text file
Copy the dummy file to new name dummyfile_10252016 (using File System Task - need to handle it through variable to make the name dynamic)
Create variable to keep the dummyFilePath
Process your required files in for each loop and store the filename in a variable
Once your file is processed, write the name to dummyfile_10252016
Script task:
string filename = Dts.Variables["User::filename"].Value.ToString();
string path = Dts.Variables["User::dummyFilePath"].Value.ToString();;
System.IO.File.AppendAllText(path, filename );

delete file after sending to user

I try to send a file to the user with this function
Yii::$app->response->sendFile($tmp_filename, 'test.RData');
Now I want the file to be deleted after sending. I know that there's an event handler for send method in yii/web/Response called EVENT_AFTER_SEND
I tried to access this handler with the following code:
Event::on(\yii\web\Response::className(), \yii\web\Response::EVENT_AFTER_SEND, function ($event) {
unlink($event->response->filename);
});
But my problem is
a) I'm not sure if this is the right way
b) how to access the filename inside the event
Any help is appreciated!
I had the same problem this week. The documentation says that we can use the $data parameter to add any variable we want on this callback. Here is a example:
Yii::$app->response->sendFile('/path/of/my/temp/file')->on(\yii\web\Response::EVENT_AFTER_SEND, function($event) {
unlink($event->data);
}, '/path/of/my/temp/file');
If you will send the file immediately, you could
header('....');
echo file_get_contents(path/of/file)
unlink(path/of/file);
You could think to create a temporary file, so the operative system will delete it.

using docslist.getFolder

I've got a working app that saves a document into a user-specified folder in their collection.
This works fine except when they want to save into their root folder.
According to the docs,
function testGetRoot() {
var root = DocsList.getRootFolder();
var folderName = DocsList.getFolderById(rootid);
Logger.log("Folder name: " + folderName.getName());
}
folderName shows 'Root' as the getName for the root of my collection.
So, seeing that I added 'Root' to the ListBox that is populated by the names of the other folders in my collection. That of course, was too easy..
**var collectionFolder = DocsList.getFolder(selectedCollection)**;
I get a 'cannot find folder Root' error message.
So I can get the Name of the root, but can't seem to get it to be accepted by getFolder method.
What am I missing?
You're not missing anything. You should report this issue in the issue tracker, as it makes sense. But as a alternative, which I'd probably use even if this worked, is referencing always to the folder id. e.g.
listbox.addItem(folder.getName(), folder.getId());
//then, later on in your handler...
//you'll receive the selected folder id directly, instead of its name.
//allowing you to use the more reliable getFolderById
var selectedFolder = DocsList.getFolderById(e.paramater.listbox);
//which works also for the root folder
I wound up hardcoding 'Root' in the logic where I addtoFolder, so that if the user selects Root, the addToFolder method isn't called. The default behavior is that the document will be saved to the Root collection, unless otherwise specified.

SSIS Package - Looping through folder to check if files exists

Can anyone help:
Required: SSIS Package to loop through a folder (containing 100 files) and check whether required files (which are 5/6) are present in that folder.
Does anyone already has code for this - where we are checking for multiple files existence in the destination folder
Regards
Add a Foreach loop container to your Control Flow
Double click it and select Collection. On Enumerator, select Foreach
File Enumerator
Select your folder and the type of file
Select the return type when a file is found. The options are the
whole filename including extension and path, the name and extension
or simply the name of the file found
Select the checkbox if you want the subfolders
Click on the variables option on the left and then new variable or
select an existing variable.
At this point you have each file name on the folder. To prove it, add a script component, double click it, and your variable on the read Only Variable and click on Edit Script. Make your Main like this:
public void Main()
{
System.Windows.Forms.MessageBox.Show(Dts.Variables["FileName"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
now, the comparison you can do several ways. I dont know where do you have the "required files" list, but assuming it is on a database, you can add a data flow task and inside of it send the filename to the DB to do the comparisson.