Strip filename from generic_file

FRW

Well-known member
I want to store the filename "test" of a file called "test.pdf" which is uploaded through a field generic_file.

Can anyone give me a hint, how I can get the filename with a function out of "C:\fakepath\test.pdf"? I think the problem are the backslashes.
"stripshlashes" gives some strange sings, and "explode" cannot search after "\" :(
 

eugenio

Administrator
Staff member
Hello,
this should help you:
http://php.net/manual/en/function.basename.php

Best,
 

FRW

Well-known member
With a field "generic_file" I choose a file called "Test.pdf"
I used a field "name" as a calculated field with "dadabik_dateiname" as function to show only the filename.

But this:
[pre]
function dadabik_dateiname($parameters_ar){
if ( $parameters_ar['datei'] !== '' && !is_null($parameters_ar['datei'])){
$dateiname = basename($parameters_ar['datei'] , ".pdf");
return $dateiname;
}
else{
return NULL;
}
}
[/pre]

has as result: "C:\fakepath\Test" so it only stripped the suffix but cannot strip the path.
Any other hints?
 

eugenio

Administrator
Staff member
Hello,
are you on Windows or Unix (the server I mean), probably what you describes can happen if you are on Unix but you are managing windows paths.
You can also look at this other function:
http://php.net/manual/en/function.pathinfo.php

What is the final purpose of your function? It is not 100% clear to me, you just want to display a filename without path and extension? The field "name" is not the generic_file field, am I right?

Best,
 

FRW

Well-known member
It's right, the server is on Unix (Suse Linux) and I will handle files from a windows machine.

the field name is just a text field which schould be store only the filename without path and extension (to simplify it for the users)

Tomorrow I will take a look at function.pathinfo...
 

FRW

Well-known member
[pre]
$pfadname = pathinfo($parameters_ar['datei']);
$dateiname = $pfadname['filename'];
return $dateiname;

Return of the options is:
[dirname] = "."
[extension] = "pdf"
[basename] = C:\fakepath\Test.pdf
[filename] = C:\fakepath\Test
[/pre]

To make it clearer:
I have a table "Anlagen" with a generic_file (to upload pdf-files) and a textfield "name"

In another Form (let's call "Info") I have a select_single filed with lookup to table "Anlagen", so the users can select the uploaded file. Now I want to have another linked filed from the lookup table called "name" with only the filename so I can show the user in the result grid of "info" the file for downloading and it's name.

I see, if I look in the editform for table "Anlagen" there is the right name of the file (just with extension) near the checkbox to delete this file. It is this Information I want to see as another linked field from the lookup table "Anlagen"
 

FRW

Well-known member
ALL TO STOP!

The function is working, as you supposed:
If I choose the file in the insert form, the field "name" is filled from the calculated function as I described above (C:Fakepath....). After Inserting it, the correct and supposed filename "Test" is shown in the details and also to handle from the Info-table as a lookup field.

I just had to do one more click(tu)
 
D

Deleted member 75341

Guest
Really interesting usage (tu).
Please can you write here the complete working function?
Thank you in advance.
 

FRW

Well-known member
[pre]
function dadabik_dateiname($parameters_ar){
if ( $parameters_ar['datei'] !== '' && !is_null($parameters_ar['datei'])){
$pfadname = pathinfo($parameters_ar['datei']);
$dateiname = $pfadname['filename'];
return $dateiname;
}
else{
return NULL;
}
}
[/pre]

table for the files with generic_field = "datei" and text_field = "name"
field "name" with calculated function = dadabik_dateiname
function (see above) in custom_functions.php
 

eugenio

Administrator
Staff member
I am glad it works now. Just a note: as you probably know, the name that DaDaBIK assigns to a file when a user uploads it is not the real name of the file, to guarantee uniqueness a suffix is added, so a file text.txt will become something like text_21.txt

Best,
 

FRW

Well-known member
Yes, I know - maybe a future request to keep the original-name and set as (original-) filename when the file is downloaded by user?
B.t.w.: maybe we can use the filename as extracted when downloading the file? It's to think about how to set the filename, when clicking on "Download" and save the file....

At this moment: A great thank for the extraordinary support from you, eugenio!
 

eugenio

Administrator
Staff member
You are welcome!
Yes that would be useful and I also thought about it, but it's not a trivial modification: providing a different name for the saving process is pretty simple (still you have to keep track of the original name in the db) but I think it would be confusing for the user if anywhere in the application they see the DaDaBIK name (e.g. test_456.pdf) and when they download the file they see a different name (test.pdf) ... so implementing this improvement would need to change the display nam also in the rest of the application.

Best,
 
Top