How to open a file in TYPO3 extbase extension controller? - typo3

I need to read an file inside of the extension controller, here as example my extension key is myext_key and the file I want to open is a JSON file data.json in the Resources/Private/JSON directory. My research gave me, that the best way to open a file wouldn't be with file_get_contents($path), instead with \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($path).
So I tried it with the following code, but that didn't work:
$content = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL('EXT:myext_key/Resources/Private/JSON/data.json');
Thanks for all help!

Not sure if anything is wrong with file_get_contents(), other than memory implications, because file_get_contents() essentially assigns a variable with the whole file content. As long as you are dealing with a small .json file is small you should be in the safe.
The method to get the absolute file name of a file inside an extension directory is getFileAbsFileName()
$fileContent = file_get_contents(
\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('EXT:myext_key/Resources/Private/JSON/data.json')
);

Related

Powershell Only:File name change according to mapping file and then copy to another directory

I have a mapping file in import.CSV as following:
Name|Business
Jack|Carpenter
Rose|Secretary
William|Clerk
Now, I have a directory which contains files like
90986883#Jack#Sal#1000.dat
76889992#Rose#Sal#2900.dat
67899279#William#Sal#1900.dat
12793298#Harry#Sal#2500.dat
Please note #Sal will always be there after Name. I need to pick these files and put into another directory and end result in second directory should look like.
90986883#Carpenter#Sal#1000.dat
76889992#Secretary#Sal#2900.dat
67899279#Clerk#Sal#1900.dat
Basically Files need to be renamed based upon CSV file and if Name is not there in file name , then there is no action required. Please note that source file should not be changed.
I will appreciate all kind of help.

django request absolute file path

using django requests, I am trying to get the absolute file path of original, uploaded file. The reason is I want to modify the original file so just the filename or some "media root" location just isn't enough.
I tried doing something like
request.FILES['file'].name
but that just gives me the name, not a path (neither absolute nor relative). google didn't really help, filenames are common, but path seems to be a special case. I hope it's even possible XP.
Thx in advance!
Try this.
myfile = request.FILES['filename']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
print(uploaded_file_url)
Also make sure you add the 'django.core.files.storage.FileSystemStorage' under Middleware_classes in settings.py file

Is there a way of changing a section of content in a file using powershell?

I have a file that I use as a template, which is copied over to each one of my customer folders. I manually go into the file and edit one line EarNotch=5/5 to EarNotch=13/6. Is there a way I can add it to my current PowerShell script that copies over the file that would edit the file with the new unique ID that I require for this customer?
The file type I am working with is a .xml file. The EarNotch is always on Line 2. Let me know what else you might need to know to help me figure this out.
The simplest way is to load the xml file using Get-Content, replace the string, then cast as xml so you can consume properly. This should answer your question.
$fileContents = Get-Content -Path C:\path\to\file.xml
[xml]$xmlFile = $fileContents.Replace('EarNotch="5/5"','EarNotch="13/6"')
For anything more complex, you might want to use XQuery, or other xml parsing.
Without knowing the structure of your xml file, the people on stack overflow can't help you. It's always best to post as much info as possible.

How can I read lines from a file in Typo3?

I have to read in a big file in Typo3 (Version 6.2.10) in a plugin we wrote. The file is uploaded via the backend and as it changes it will be newly uploaded.
Currently I use:
$file->getOriginalResource()->getContents();
$file is a \TYPO3\CMS\Extbase\Domain\Model\FileReference.
That works fine, as long as the file in question is small enough. The problem is, that the content of the file is read in the memory completely. With bigger files I reach the point, at which this fails. So my question is, how can I read in the contents of the file line by line?
You can copy it to a temporary local path with
$path = $file->getOriginalResource()->getForLocalProcessing(false);
Then you can use fgets as usual to loop through the file line by line.

access folders and get files

I start with Matlab and would like to know how could I access to a folder and get contents to access files and read them.
I have a variable in workspace tmpfolder that is equal to 'path to folder' but I don't find how could I make dir(tmpfolder) and get files, browse any file content to get a string value...
I would start with dir() and fopen().
More generally, try starting at the beginning: Working with Files and Folders.
If you have an image file in jpeg format in another folder named myimage and a text file called mytext, use:
prefix_image='myimage';
prefix_data='mytext';
fileformat='.jpg';
dataformat='.txt';
folder='C:\Users\khaled\Documents\MATLAB\';
image = imread(strcat(folder,prefix_image,fileformat));
data=textread(strcat(folder,prefix_data,fileformat),'%f');