This topic is locked

How to add timestamp to uploaded file name

6/10/2015 9:17:17 PM
PHPRunner Tips and Tricks
admin

Applies to PHPRunner 6.x-8.x. Paste this code to BeforeAdd/BeforeEdit events and replace UploadFieldName with the actual field name.
Note that this code only makes sense in 'Basic upload control' mode. In standard mode timestamp is added to file name automatically.

// field name that stores uploaded file name

$field="UploadFieldName";
global $pageObject;

foreach( $pageObject->filesToMove as $key=>$val)

{

if($pageObject->filesToMove[$key]->sourceFilename==$_FILES["value_".$field."_".postvalue("id")]["tmp_name"]) {
$fileName = $values[$field];

$ext = ".".pathinfo($fileName, PATHINFO_EXTENSION);

$name = basename($fileName, $ext);

$newFileName = $name."_".date('YmdHis').$ext;

$values[$field]=$newFileName;

$pageObject->filesToMove[$key]->destFilename = $newFileName;

}

}
return true;
lefty 6/19/2015

Thanks for this info , as a lot of users can now upload 1 file and if mobile for ex. IPAD will ask for ( take a picture / or use from library ). Also timestamp is simple like old version just add the code above in event instead.
You now will not have to use JSON Data in your DB. And it is a lot easier to embed an image in an email. ( public folder )
Now : of course the next question is ? how to get URL of private folder outside root in an event email and embed in an image. If you use multiupload it seems even harder as mfhandler takes over URL and saves in JSON format.
Sergey : I know you gave me a file ( FileField fix ) for Ipads : please add in your next version for other users.
Thanks: Please add any thoughts to this statement

B
bioman 9/30/2015

Hi,

I tried the method described but nothing changed in the naming or saving of my image file. The field that I store the image name in is called "image" and this is the code I used in the before add event:
// field name that stores uploaded file name

$field="image";
global $pageObject;

foreach( $pageObject->filesToMove as $key=>$val)

{

if($pageObject->filesToMove[$key]->sourceFilename==$FILES["value".$field."_".postvalue("id")]["tmp_name"]) {
$fileName = $values[$field];

$ext = ".".pathinfo($fileName, PATHINFOEXTENSION);

$name = basename($fileName, $ext);

$newFileName = $name."
".date('YmdHis').$ext;

$values[$field]=$newFileName;

$pageObject->filesToMove[$key]->destFilename = $newFileName;

}

}
return true;
This is the same code as described above but with my field name, yet it doesn't seem to have any effect. What am I doing wrong? Is 'before add' the wrong place for the code?



Thanks for this info , as a lot of users can now upload 1 file and if mobile for ex. IPAD will ask for ( take a picture / or use from library ). Also timestamp is simple like old version just add the code above in event instead.
You now will not have to use JSON Data in your DB. And it is a lot easier to embed an image in an email. ( public folder )
Now : of course the next question is ? how to get URL of private folder outside root in an event email and embed in an image. If you use multiupload it seems even harder as mfhandler takes over URL and saves in JSON format.
Sergey : I know you gave me a file ( FileField fix ) for Ipads : please add in your next version for other users.
Thanks: Please add any thoughts to this statement

lefty 10/13/2015



Hi,

I tried the method described but nothing changed in the naming or saving of my image file. The field that I store the image name in is called "image" and this is the code I used in the before add event:
// field name that stores uploaded file name

$field="image";
global $pageObject;

foreach( $pageObject->filesToMove as $key=>$val)

{

if($pageObject->filesToMove[$key]->sourceFilename==$FILES["value".$field."_".postvalue("id")]["tmp_name"]) {
$fileName = $values[$field];

$ext = ".".pathinfo($fileName, PATHINFOEXTENSION);

$name = basename($fileName, $ext);

$newFileName = $name."
".date('YmdHis').$ext;

$values[$field]=$newFileName;

$pageObject->filesToMove[$key]->destFilename = $newFileName;

}

}
return true;
This is the same code as described above but with my field name, yet it doesn't seem to have any effect. What am I doing wrong? Is 'before add' the wrong place for the code?



In date field use {'YmdHis' } you have parenthesis . Also make sure id of your autonumber field is id . If not use proper case . That's all I see.

B
bioman 10/14/2015

Thanks for the reply John,

I had already purchased support and here is the code that ended up working:
Add this code to the Before record added/Before record update

------------------------------------------------------------------------------------------------

// field name that stores uploaded file name

$field="image";

$timestamp = date('YmdHis');

foreach( $pageObject->filesToSave as $key=>$val)

{

$fileName = $pageObject->filesToSave[$key]->destFilename;

$ext = ".".pathinfo($fileName, PATHINFOEXTENSION);

$name = basename($fileName, $ext);

$newFileName = $name."
".date('YmdHis').$ext;

if ( !strpos($values[$field], $timestamp) )

{

$values[$field]=$newFileName;

}

$pageObject->filesToSave[$key]->destFilename = $newFileName;

}

------------------------------------------------------------------------------------------------
Two files will be saved in the ...files/images folder with names like these: "<originalName><timestamp>.jpg" and "th<originalName><timestamp>.jpg"

The DB values should be written like this: "<originalName><timestamp>.jpg"

You'll see the image on the list and the name below formatted like this "<originalName>
<timestamp>.jpg"

If you don't want to see time stamp in the name of the file on the list you should add this code to the View as -> File -> Show custom expression:

------------------------------------------------------------------------------------------------

$ext = ".".pathinfo($data["image"], PATHINFO_EXTENSION);

$name = basename($data["image"], $ext);

$name = substr($name, 0, -15);

$value = '
<a href="mfhandler.php?file='.$data["image"].'&table=quiz&field=image&pageType=list&key1='.$data["id"].'" dir="LTR">'.$name.$ext.'</a>';



In date field use {'YmdHis' } you have parenthesis . Also make sure id of your autonumber field is id . If not use proper case . That's all I see.

lefty 10/16/2015



Thanks for the reply John,

I had already purchased support and here is the code that ended up working:
Add this code to the Before record added/Before record update

------------------------------------------------------------------------------------------------

// field name that stores uploaded file name

$field="image";

$timestamp = date('YmdHis');

foreach( $pageObject->filesToSave as $key=>$val)

{

$fileName = $pageObject->filesToSave[$key]->destFilename;

$ext = ".".pathinfo($fileName, PATHINFOEXTENSION);

$name = basename($fileName, $ext);

$newFileName = $name."
".date('YmdHis').$ext;

if ( !strpos($values[$field], $timestamp) )

{

$values[$field]=$newFileName;

}

$pageObject->filesToSave[$key]->destFilename = $newFileName;

}

------------------------------------------------------------------------------------------------
Two files will be saved in the ...files/images folder with names like these: "<originalName><timestamp>.jpg" and "th<originalName><timestamp>.jpg"

The DB values should be written like this: "<originalName><timestamp>.jpg"

You'll see the image on the list and the name below formatted like this "<originalName>
<timestamp>.jpg"

If you don't want to see time stamp in the name of the file on the list you should add this code to the View as -> File -> Show custom expression:

------------------------------------------------------------------------------------------------

$ext = ".".pathinfo($data["image"], PATHINFO_EXTENSION);

$name = basename($data["image"], $ext);

$name = substr($name, 0, -15);

$value = '
<a href="mfhandler.php?file='.$data["image"].'&table=quiz&field=image&pageType=list&key1='.$data["id"].'" dir="LTR">'.$name.$ext.'</a>';



Bioman,

You think this would work in an email with your code?

$value = '
<a href="mfhandler.php?file='.$data["image"].'&table=quiz&field=image&pageType=list&key1='.$data["id"].'" dir="LTR">'.$name.$ext.'</a>';
To email an image ? just trying to take this a little farther. If you are using multiple files then the output is JSON you need to extract/parse the JSON array See the only documentation on thisif just basic upload this should work but when I see the mfhandler code it tells me that you are using multiple file upload . Check database file for the file name? What version of PHPrunner are you using . This makes a bit of difference ?

B
bioman 10/20/2015

Hi John,

I really am not sure. Sorry, but I'm not that great with code yet. Best of luck,

Brett



Bioman,

You think this would work in an email with your code?

$value = '
<a href="mfhandler.php?file='.$data["image"].'&table=quiz&field=image&pageType=list&key1='.$data["id"].'" dir="LTR">'.$name.$ext.'</a>';
To email an image ? just trying to take this a little farther. If you are using multiple files then the output is JSON you need to extract/parse the JSON array See the only documentation on thisif just basic upload this should work but when I see the mfhandler code it tells me that you are using multiple file upload . Check database file for the file name? What version of PHPrunner are you using . This makes a bit of difference ?