Only getting the file ID instead of ID & Extention
Only getting the file ID instead of ID & Extention

I have successfully created a file (image) upload, and I can see the image in the table column
However, it only gives me the ID without the extension.
How can I get the web_path or system_path inserted instead of the ID?
Any help is most welcome.
Colin
Field::inst( 'live.image' )
->setFormatter( Format::ifEmpty( null ) )
->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/IMAGES/uploads/__ID__.__EXTN__' )
->db( 'files', 'id', array(
'filename' => Upload::DB_FILE_NAME,
'filesize' => Upload::DB_FILE_SIZE,
'web_path' => Upload::DB_WEB_PATH,
'system_path' => Upload::DB_SYSTEM_PATH
) )
->validator( Validate::fileSize( 500000, 'Files must be smaller that 500K' ) )
->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
),
{
label: 'Picture:',
name: 'live.image',
type: 'upload',
display: (fileId) =>
`<img src='${editor.file('files', fileId).web_path}'/>`,
clearText: 'Clear',
noImageText: 'No image'
},
{
data: 'live.image',
render: function (file_id) {
return file_id
? `<img style="height: 100px;object-fit: fill;" src="${editor.file('files', file_id).web_path}"/>`
: null;
},
defaultContent: '<img style="height: 100px;object-fit: fill;" src="/IMAGES/uploads/8.png"/>',
title: 'Image'
},
This question has an accepted answers - jump to answer
Answers
Could you show me the JSON data that is being used to load your table please?
The setup above appears to be inline with what would expect to be working!
Thanks,
Allan
Hi Allan
Please see below
Is there a
files
object on the JSON object? If you can't make it public (totally understand that) can you PM me the full thing?Allan
Allan
Please see below the files option
"options": [],
"files": {
"files": {
"11": {
"id": "11",
"filename": "Colin.png",
"filesize": "192593",
"web_path": "/IMAGES/uploads/11.png",
"system_path": "/home/sites/*****.**/public_html/IMAGES/uploads/11.png"
}
}
},
Well your files object looks perfectly normal and it also contains web_path and system_path. Maybe we don't understand your issue?!
Thanks for the reply
My issue is that when an image is inserted into a record, it only gives me the ID, but not the ext e.g. in the row it says 11 but I need 11.png
Hope that helps
You seem to be using this example:
https://editor.datatables.net/examples/advanced/upload.html
The example works. Please take a look a the details of the example. Yes, the field "image" of the primary table only contains the id of the file as a foreign key, but that is ok. You need to retrieve the image from the files table. For that purpose you need the foreign key to join the file table with the primary table.
How is the join being done in the example?
In the example above the join of the file table with the primary table happens here. Editor's "upload" method does it using the file id saved in the "image" field of the primary table.
Here Editor passes "image" which is the file id into the "display" function to display the details loaded from the files table.
Here again "image" - labeled as "file_id" - is passed into another function. In this case the "render" function which gets the details from the files table. not from the primary table:
What you actually want is this in a simple SQL statement. You want what I call the technical file name which is part of web_path and system_path. Here is the SQL-statement to retrieve it.
The join between the two tables is done by Editor implicitly. You don't have to worry about it yourself. Neither do you have to write SQL-statements like this. Editor does this all for you.
Many thanks for the detailed walkthrough and the select statement.
I can confirm it works like a dream
Thanks again
Glad you got it working!
The SELECT statement is crap though. The WHERE clause should contain the value that you have. That is the value of "image" (foreign key).
So it should look like this:
But it's merely for illustration purposes ... not really required. You can extract the "technical file name" using Editor's "render" method on the client side as well.