Upload with a special file name

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
A Bindi
Veteran Member
Posts: 51
Joined: 2018-01-04 18:45

Upload with a special file name

Post by A Bindi » 2018-11-13 11:28

Hi,

I have a table with a field "File upload" type and I would want upload that file renaming it with a string that is specified in a filed: how can do this ?

ALex.

A Bindi
Veteran Member
Posts: 51
Joined: 2018-01-04 18:45

Re: Upload with a special file name

Post by A Bindi » 2018-11-14 07:27

For example, i have two table fields:
- Document (Varchar, 40) - Appgini type "File upload" -> Used to select and upload a file document (.doc/.xls/.pdf ...)
- Filename (Varchar, 120) -> Used to store the new filename I want use for "Document" above

When edit the table:
1) I store the string "Computer user manual" to field Filename
2) I select a file called "SOMETHING.DOC" using field "Document"
3) save the record.

When I open the record and I click on "Document" icon i would want download a file called "Computer user manual.doc".

:)

ALex.

A Bindi
Veteran Member
Posts: 51
Joined: 2018-01-04 18:45

Re: Upload with a special file name

Post by A Bindi » 2018-11-19 10:05

No ideas ?
:)

ALex.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Upload with a special file name

Post by pbottcher » 2018-11-19 20:34

Hi,

you can try this:

add a function to the hooks/tablename.php file and edit the after_insert and after_update functions:

Code: Select all

function update_filename($id) {
  if (!$_REQUEST['file_remove']) { 
  $dir = getUploadDir($dir);
  $extension=pathinfo($data['Document'], PATHINFO_EXTENSION);
  if ($extension != '') $extension=".".$extension;
  rename($dir."/".$data['Document'], $dir.$data['Filename'].$extension);
  sqlvalue("UPDATE tablename set Document='".$data['Filename'].$extension."' where ID=".$ID']);
  }

}

function tablename_after_insert($data, $memberInfo, &$args){
  update_filename($data['selectedID']);
 return TRUE;
}

function tablename_after_update($data, $memberInfo, &$args){
  update_filename($data['selectedID']);
 return TRUE;
}
replace tablename with your applicable tablename.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

charmed_ones
Veteran Member
Posts: 32
Joined: 2013-03-22 13:31

Re: Upload with a special file name

Post by charmed_ones » 2021-03-03 16:08

I gave this a shot and it doesn't work at all

function update_filename($id) {
if (!$_REQUEST['file_remove']) {
$dir = getUploadDir($dir);
$extension=pathinfo($data['Images'], PATHINFO_EXTENSION);
if ($extension != '') $extension=".".$extension;
rename($dir."/".$data['Images'], $dir.$data['TAG'].$extension);
sqlvalue("UPDATE Defective_Master_List set Images='".$data['TAG'].$extension."' where ID=".$ID']);
}

}

added this to the Defective_Master_List-dv.js file and then added

update_filename($data['selectedID']);
to the hooks php and when you load the image it loads it but never changes the name just get a blank page when adding a new image to the record
any help with this?

charmed_ones
Veteran Member
Posts: 32
Joined: 2013-03-22 13:31

Re: Upload with a special file name

Post by charmed_ones » 2021-03-03 19:01

charmed_ones wrote:
2021-03-03 16:08
I gave this a shot and it doesn't work at all

function update_filename($id) {
if (!$_REQUEST['file_remove']) {
$dir = getUploadDir($dir);
$extension=pathinfo($data['Images'], PATHINFO_EXTENSION);
if ($extension != '') $extension=".".$extension;
rename($dir."/".$data['Images'], $dir.$data['TAG'].$extension);
sqlvalue("UPDATE Defective_Master_List set Images='".$data['TAG'].$extension."' where ID=".$ID']);
}

}

added this to the Defective_Master_List-dv.js file and then added

update_filename($data['selectedID']);
to the hooks php and when you load the image it loads it but never changes the name just get a blank page when adding a new image to the record
any help with this?

Error log shows me this

PHP Fatal error: Call to undefined function update_filename() in /hooks/Defective_Master_List.php on line 242,

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Upload with a special file name

Post by onoehring » 2021-03-08 11:00

Hi,

please read pbötchers post precisely: You need to put ALL of this code into the /hooks/Defective_Master_List.php (you are writing, you put it into Defective_Master_List-dv.js - which is wrong.

Olaf

charmed_ones
Veteran Member
Posts: 32
Joined: 2013-03-22 13:31

Re: Upload with a special file name

Post by charmed_ones » 2021-03-11 17:09

Thanks Olaf,
I tried that and I'm still getting blank white pages, the site dies completely
No pages can be displayed when this is inserted in to /hooks/Defective_Master_List.php

function update_filename($id) {
if (!$_REQUEST['file_remove']) {
$dir = getUploadDir($dir);
$extension=pathinfo($data['Image'], PATHINFO_EXTENSION);
if ($extension != '') $extension=".".$extension;
rename($dir."/".$data['Image'], $dir.$data['TAG'].$extension);
sqlvalue("UPDATE Defective_Master_List set Image='".$data['TAG'].$extension."' where ID=".$ID']);
}

}

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Upload with a special file name

Post by pbottcher » 2021-03-11 19:47

You have a syntax error.

Try

Code: Select all

function update_filename($id) {
if (!$_REQUEST['file_remove']) {
$dir = getUploadDir($dir);
$extension=pathinfo($data['Image'], PATHINFO_EXTENSION);
if ($extension != '') $extension=".".$extension;
rename($dir."/".$data['Image'], $dir.$data['TAG'].$extension);
sqlvalue("UPDATE Defective_Master_List set Image='".$data['TAG'].$extension."' where ID=".$ID);
}

}
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

charmed_ones
Veteran Member
Posts: 32
Joined: 2013-03-22 13:31

Re: Upload with a special file name

Post by charmed_ones » 2021-03-11 22:18

Thank you Olaf, that fixed the site crashing, but it never updated the image file name
This is in the error log

[Thu Mar 11 17:01:40.273951 2021] [:error] [pid 28254] [client xxx.xxx.xxx.xxx] PHP Warning: imagejpeg(./images/0565_tv.JPG): failed to open stream: Permission denied in /home/.sites/148/site6/web/admin/incFunctions.php on line 278, referer: http://xxx.xxxxx.com/Defective_Master_List_view.php
[Thu Mar 11 17:01:40.367676 2021] [:error] [pid 28270] [client xxx.xxx.xxx.xxx] PHP Warning: imagejpeg(./images/4573_tv.JPG): failed to open stream: Permission denied in /home/.sites/148/site6/web/admin/incFunctions.php on line 278, referer: http://xxx.xxxxx.comm/Defective_Master_List_view.php
[Thu Mar 11 17:01:40.503070 2021] [:error] [pid 7373] [client xxx.xxx.xxx.xxx] PHP Warning: imagejpeg(./images/7482_tv.JPG): failed to open stream: Permission denied in /home/.sites/148/site6/web/admin/incFunctions.php on line 278, referer: http://xxx.xxxxx.com/Defective_Master_List_view.php
[Thu Mar 11 17:02:36.434501 2021] [:error] [pid 24070] [client xxx.xxx.xxx.xxx] PHP Warning: filemtime(): stat failed for ./images/imagesCA8MDE0T_dv.jpg in /home/.sites/148/site6/web/thumbnail.php on line 50, referer: http://xxx.xxxxx.com/Defective_Master_L ... 1789583645
[Thu Mar 11 17:11:22.687325 2021] [:error] [pid 28271] [client xxx.xxx.xxx.xxx] PHP Warning: filemtime(): stat failed for ./images/322_dv.gif in /home/.sites/148/site6/web/thumbnail.php on line 50, referer: http://xxx.xxxxx.com/Defective_Master_L ... =760975424

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Upload with a special file name

Post by onoehring » 2021-03-12 07:01

Hi,

well, incCommon.php seems to be application dependent in that line (at least for me, checking a project), there are references to a fieldname (and -source). I would suppose, the fieldname supplied here in your request, specific, either 'Image' or 'TAG' are not 100% correct (check spelling + CaSe).

The last two errors seem to me, that pbötchers code intercepts the AG code, before AG has actually finished working with the file.

But:
Why are you not saving your tags (TAG) in a different field?
Why don't you want to keep the random filename and simply add a name, that you show to the user in another field? For example, I am using the random filename and adding the name of the actual file to another field (see images). The user in AG only sees the field with the name of the file which he uploaded.

Database:
filename.png
filename.png (7.12 KiB) Viewed 6776 times
AppGini application
filename_clear.png
filename_clear.png (2.69 KiB) Viewed 6776 times
Olaf

charmed_ones
Veteran Member
Posts: 32
Joined: 2013-03-22 13:31

Re: Upload with a special file name

Post by charmed_ones » 2021-03-12 18:03

Thanks I found a solution

thank you for all your help

function Rename_FileName($filename,$tag_number){
$extension = end(explode('.',$filename ));
if ( !rename ( "/home/.sites/148/site6/web/images/$filename" , "/home/.sites/148/site6/web/images/$tag_number.$extension") )
die('can not rename the file');
}


function Rename_Image_Filename_Before_Insert_Or_Update($data){
$sql = "select TAG_Number from Defective_Returns where id='{$data->TAG}'";
$tag_number = sqlValue($sql);
if ( $data->Images )
Rename_FileName($data->Images,"$tag_number");
if ( $data->image2)
Rename_FileName($data->image2,"$tag_number-1");
if ( $data->image3)
Rename_FileName($data->image3,"$tag_number-2");
return TRUE;
}




function Defective_Master_List_before_update(&$data, $memberInfo, &$args){
if ( $_REQUEST['Images_remove'] )
return true;
return Rename_Image_Filename_Before_Insert_Or_Update((object)$data);
}



Part Two

function Rename_Image_Filename_After_Insert_Or_Update($data){
$sql = "select TAG_Number from Defective_Returns where id='{$data->TAG}'";
$tag_number = sqlValue($sql);

if ( $data->Images ){
$extension = end(explode('.',$data->Images ));
$query="UPDATE Defective_Master_List SET Images= '{$tag_number}.{$extension}' WHERE id=$data->id";
sqlValue($query);
}

if ( $data->image2 ){
$extension = end(explode('.',$data->Images ));
$query="UPDATE Defective_Master_List SET image2 = '{$tag_number}-1.{$extension}' WHERE id=$data->id";
sqlValue($query);
}
if ( $data->image3 ){
$extension = end(explode('.',$data->Images ));
$query="UPDATE Defective_Master_List SET image3 = '{$tag_number}-2.{$extension}' WHERE id=$data->id";
sqlValue($query);
}

}


function Defective_Master_List_after_update($data, $memberInfo, &$args){
if ( $_REQUEST['Images_remove'] )
return true;

return Rename_Image_Filename_After_Insert_Or_Update((object)$data);
}



This is all in the Table.php in the hooks directory
This doesn't require any field to be populated with a name it picks up a field name from the Table

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Upload with a special file name

Post by onoehring » 2021-03-14 08:04

Hi,

thank you for your code.
I do not really understand why you are using this method. Aren't you overwriting files that have the same tag?
It also seems to me, that a file can have only 1 tag. Is that what you want? If so, why not simply save the tag in a different field of the database?

Might be questions only I ask myself, maybe it's worth asking.
Olaf

charmed_ones
Veteran Member
Posts: 32
Joined: 2013-03-22 13:31

Re: Upload with a special file name

Post by charmed_ones » 2021-03-14 16:00

yep found it worked and then didn't work for that reason! oops

so we went this way instead

function Defective_Master_List_after_insert($data, $memberInfo, &$args)
{

return Rename_Image_Filename_After_Insert_Or_Update($data);
}

function Rename_FileName($filename, $tag_number)
{
$extension = end(explode('.', $filename));
if (!rename("/home/.sites/148/site6/web/images/$filename", "/home/.sites/148/site6/web/images/$tag_number.$extension"))
die('can not rename the file');
return $extension;
}



function Rename_Image_Filename_After_Insert_Or_Update($data){
$data = (object) $data;
$sql = "select TAG_Number from Defective_Returns where id='{$data->TAG}'";
$tag_number = sqlValue($sql);

if ( $data->Images ){

$extension = Rename_FileName($data->Images,"$tag_number");
$query="UPDATE Defective_Master_List SET Images= '{$tag_number}.{$extension}' WHERE id=$data->id";
sqlValue($query);
}

if ( $data->image2){
$extension = Rename_FileName($data->image2,"$tag_number-1");
$query="UPDATE Defective_Master_List SET image2= '{$tag_number}-1.{$extension}' WHERE id=$data->id";
sqlValue($query);
}

if ( $data->image3){
$extension = Rename_FileName($data->image3,"$tag_number-2");
$query="UPDATE Defective_Master_List SET image3= '{$tag_number}-2.{$extension}' WHERE id=$data->id";
sqlValue($query);
}
return TRUE;
}




function Defective_Master_List_after_update($data, $memberInfo, &$args)
{
return Rename_Image_Filename_After_Insert_Or_Update($data);
}


User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: Upload with a special file name

Post by onoehring » 2021-03-15 16:48

Hi,

I understand you found a solution that seems to work for you. But I would like to recommend overthinking your approach.
- It's not satisfying for the user if he get's the "die" message - what should he do about it?
- As I wrote above:
It also seems to me, that a file can have only 1 tag. Is that what you want? If so, why not simply save the tag in a different field of the database?
I would assume, there are more disadvantages by saving the tag into the filename - or, put differently - one would need very big advantages to use this approach.

I do not intend to talk bad here, I just want to put out a warning, that your approach might turn up some very ugly results and problems.

Olaf

charmed_ones
Veteran Member
Posts: 32
Joined: 2013-03-22 13:31

Re: Upload with a special file name

Post by charmed_ones » 2021-03-15 17:28

we have a very large advantage to this approach as we send the TAG and all the fields to the vendor automatically when the images are inserted and the vendors require the images to be named the same as the return request.
This saves time in the end of our staff having to rename each image to the TAG number then uploading them to the system

Once they are uploaded, the system creates a ZIP file on the three images and attaches them to an email that is send off to the contact in the database that is linked to the vendor


Post Reply