Recursive deleting files from storage Laravel - laravel

I have a need to be able to delete files from storage on laravel.
My idea is to loop through the files based on the folder name (course id) using this code:
$rootPath = public_path() .'/storage/' . $course_id . '/';
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($rootPath),
\RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir() === FALSE){
// Get real and relative path for current file
$filePath = $file->getRealPath();
//HERE I WANT TO DELETE THE ACTUAL FILE - BUT I CAN'T GET THE FILE NAME
//$name and $file are not carrying the filenames and hence I cant delete
the file
}
}
and then I will delete each file using: Storage::delete($file);
My issue is that my iteration is not showing the filenames within the directory and I have no idea what I am doing incorrectly.

You could just iterate over the directories and use
$success = Storage::cleanDirectory($directory);
to remove all files and directories in the specified directory.

Related

Reading a .txt file and storing each line as a model

I have a .txt file that has names:
john doe
mary roe
...
...
I've a model Person which has the following $fillable:
protected $fillable = [
'list_id',
'name'
];
I'm trying to populate a specific List with the names from this specific file, but I'm sort of stuck trying to understand how to properly do this. I'm looking mostly to seed the database with a series of lists and names on each one (coming from a .txt file each list).
What would be the most convinient way to read the file and tell Laravel "Hey, store each line under (lets say) list 1!"?
$file = new \SplFileObject(‘/path/to/your/file.txt’);
$list = List::where(…)->first(); // find the list matching your file
while (!$file->eof()) {
// assuming you have List::people() hasMany relation:
$list->people()->create([
’name’ => trim($file->fgets()); // you can format, trim, sanitize your line here
]);
// Without relation:
Person::create([
’list_id’ => $list->id,
‘name’ => trim($file->fgets());
]);
}
the solution would be:
$file = new \SplFileObject("file.txt");
while (!$file->eof()) {
// Echo one line from the file.
$echo $file->fgets();
}
$file = null;
Hope this helps someone out there.
Hi SplFileObject is a neat way and object oriented, further info: SplFileObject
$myFile = new SplFileObject("data.txt");
while (!$myFile->eof()) {
echo $myFile->fgets() . PHP_EOL; //line N
//Here you can pass list id and the value retrieved above and store it on the DB
}

Is there an openTBS command to write/update the Notes section of a pptx slide?

I am using openTBS to update a slide deck and need to also update/replace template content in the Notes section. Is there a command that supports that?
There is not native command for doing this but there is work around.
In a PPTX, comments are stored into sub XLM files such as ppt/comments/comment2.xml.
Each slide may have its own Comment sub-file.
I’s a bit complicated to find which Comment file is corresponding to what slide.
But you can find all Comment files in the PPTX using a code like this :
$all = $TBS->Plugin(OPENTBS_GET_FILES);
$comments = array();
foreach ($all as $file) {
if (substr($file, 0, 13) == 'ppt/comments/') {
$comments[] = $file;
}
}
Then you can open each sub-file and merge data as an XML file.
foreach ($comments as $file) {
$TBS->PlugIn(OPENTBS_SELECT_FILE, $file);
$TBS->MergeFields(...);
}

images are saving like tmp file in public folder

hi m trying to add data in db but images saves like tmp file, m storing these pics in ecommmerce\public\images\backend_images\category_images and these real pics are not saving instead their real name in db they are saving in folder like this: https://ibb.co/CHXTm3j .. any solution . here is my code:
store function:
$category = new Category;
$category->category_name = $request->category_name;
$category->category_description = $request->category_description;
$category->category_slug = $request->category_slug;
$path = $request->file('category_image');
$image = $path->getClientOriginalName();
$path->move(public_path('images/backend_images/category_images'));
$category->category_image = $image;
$category->save();
Since you are missing the parameter for filename there. Your function to move the original file should be:
$path->move(public_path('images/backend_images/category_images'), $image);
If you do not provide that parameter, the default value would be temp name of the file rather than it's original name.

Store results of split function in variables

I have files in a directory like below:
first-file-name
2nd-file-name
3rd-file-name
.
.
n-file-name
I need to store each portion of file name in a separate variable because I want to insert these values in separate columns of table.
For this, I used the below script to get the each portion of a file name:
$var1=$item.BaseName.Split("-",3)[0]---------first
$var2=$item.BaseName.Split("-",3)[1]---------file
$var3=$item.BaseName.Split("-",3)[2]---------name
and can save these values in a variable. But the question is how can I do this for all files, if I use foreach loop then the variable values will be overwritten???
foreach(item in $items)
{
$var1=$item.BaseName.Split("-",3)[0]---------first
$var2=$item.BaseName.Split("-",3)[1]---------file
$var3=$item.BaseName.Split("-",3)[2]---------name
}
Here, in $items I got the file path using get-childitem.
I would create a PsCustomObject with the three parts:
$parts = $items | ForEach-Object {
[PsCustomObject]#{
FirstPart = $item.BaseName.Split("-",3)[0]
SecondPart = $item.BaseName.Split("-",3)[1]
ThirdPart = $item.BaseName.Split("-",3)[2]
}
}
now $parts is an array of these objects so you can access them using e. g.
$parts[0].FirstPart

Pick the latest file based on timestamp provided in the filename

I have to pick the files in order(first file first) from say a folder (C:\Users) and file name has the timestamp in it.
For example below are my files in C:\Users\ and the time stamp is after the first underscore i.e. 20170126102806 in the first file below. I have to loop through files and pick the first file and so on. so out of 5 files below,20170123-000011_20170126101823_AAA is the first file. How do I do this in SSIS?
1.20170123-000011_20170126102806_AAA
2.20170123-000011_20170126103251_AAA
3.20170123-000011_20170126101823_AAA
4.20170123-000011_20170126103305_AAA
5.20170123-000011_20170126102641_AAA
You can act in two ways:
use the foreach loop container to get the list of files, and then populate a database table.
Then, outside the foreach loop, use an Execute SQL to select from that table using an appropriate ORDER BY. Load an object variable with the result set. Then use a second foreach loop to step through the variable object and collect files.
use a Script Task to retrieve the contents of the folder (the list of files) and sort files then load an object variable with the dataset. Then use a foreach loop to step through the variable object to collect files.
I hope this help.
You could use a script task in a For Each Loop. Use the filename returned as the source to load each time.
using System.IO;
public void Main()
{
string filePath = "D:\\Temp";
DirectoryInfo dir = new DirectoryInfo(filePath);
var files = dir.GetFiles("*_AAA");//Or from a variable
DateTime fileCreateDate1 = File.GetCreationTime(filePath + "\\" + files[0]);
if (files.Length >= 2)
{
for (int i = 1; i < files.Length; i++)
{
DateTime fileCreateDate2 = File.GetCreationTime(filePath+ "\\" + files[i]);
if (fileCreateDate1 < fileCreateDate2)
{
fileCreateDate1 = fileCreateDate2;
}
}
}
Dts.Variables["User::FileToLoad"].Value = fileCreateDate1;
Dts.TaskResult = (int)ScriptResults.Success;
}
You will have to remove the file after it was loaded or else it will be loaded each time as it is the oldest or latest file.
There might be a bug or so, but have similar code that works. Just iron it out if needed.

Resources