Rename file during upload

Hello Forum Members,
I’ve a field with datatype Document (File Name). For the specified field I want when the end user upload any file that will automatically renamed according to a global variable. Is it possible? Very much needed.

Thanks
Rajibul

Use “rename()” in onAfterInsert/onAfterUpate events.

Hello Giuseppe,

Thanks for your prompt response. Rename() will rename the file in physical location. Would you please explain it litte bit.

Thanks

Rajibul

Indeed, the file is first copied to a temporary directory and then moved to the final location. Useually you need to save first to get the job done as the file is not present in the final location after upload is complete. Afaik it’s not by default possible to change filenames during the process. If you don’t want to dive into the generated code, you need a work-around. What Giuseppe means I think is that after save the file is moved to the final destination. You can change the name then and adapt the new name into the database. You have to do a manual update of the record.

Exactly.

1.- You upload file and record is saved.
2.- You obtain path and rename physical file.
3.- You update your record with the name of the new file.

I have in my ToDo to create a function to share because this is a commonly question, but as always, lack of time is the problem.

Got it. Thank you very much Giuseppe and Albert Drent.

Giu and aducom, as always, useful information, will wait for the function Giu, you are right, it will help many users, it is common question… so thanks in advanced :slight_smile:

for me when i faced the need and couldn’t do it, i used the sub folder in the field settings (image - file name) and that will store the same image in separate folder which makes it far to be override by same name again :slight_smile: another workaround lool

Hi,
How can i obtain path directory from document(file name) type ?
i have tried to search it from forum conversation and still have no clue.

i have form to change document to the new one.
it will upload new file, update file name in database,
and once update done, new file name should be uploaded into doc directory
and old file name should be removed to archive directory.
how can i do that ?

I have tried to use rename() function, but it always failed with reason file not found.
I think i should get correct directory first to use rename() function.
please help …
appreciate your help on this

moelath

moelath, see the file directory in your Application > file path

obviously in development environment will be: /wwwroot/scriptcase/file/doc

in deployment is projectroot/_lib/file/doc (or img) then add the file name

OK, Thanks MikeDE i saw it,
Does it mean i hard-coded my directory path during “development phase” ?
reading from forum discussion, this “Application > file path” value will be override when we deploy the application (advance mode), is it correct ?
if it is true, what should we put in Application > file path before deployment? is it “/_lib/file/doc” ?

what happened after the deployment, later on user want to change the directory ?

can i put this path into a parameter table ?
so, whenever user want to change the path, they just need to update the parameter table value.

sorry if i have too many question, i just need to understand how it work …
thanks again

[QUOTE=moelath;28398]
Does it mean i hard-coded my directory path during “development phase” ? [/QUOTE]

no, once you deploy it should take the other link automatically /_lib/file/doc, unless you chose advanced deployment and configure otherwise.

it will not override, it will take the path of deployment

no need, when you are developing, test your applications on development environment using the provided path in Application > file path… once you deploy (normal deployment) the path will be /_lib/file/… unless you chose it differently during deployment

not recommended, files will remain on the old path, if you changed the path in second deployment or change field settings then re-deployed, new files will be stored in the new path, hence the older files, if requested by user will not work (since their path is wrong) - why would you do that anyway!? use stable file path, you can use a sub-folder settings as well, then do-it-once-and-forget-it (that is my opinion)

i think so, try onApplicationInIt, define path and use it within code, make few tests, see what happens and let us know (i didn’t try this myself)

[QUOTE=moelath;28398]

sorry if i have too many question, i just need to understand how it work …
thanks again[/QUOTE]

you are welcome :slight_smile:

Woww… Thanks MikeDE,
Thanks for your patience to address all my questions. Really appreciate it.
And yes, i agree with you, now i also understand why directory name cannot be changed after deployment.

And now i also understand, why on other thread, someone creating script to distinguish directory path during development and production as below :


// Identify if running on Prod or Dev environment
if ((substr($_SERVER[‘HTTP_HOST’],-4) == ‘8083’) OR (substr($_SERVER[‘HTTP_HOST’],-4) == ‘8098’)) { // Current Port?
[prod] = FALSE;
[fp] = “…/…/…/file/doc/timesheets/”;

} else {
[prod] = TRUE;
[fp] = “…/_lib/file/doc/timesheets/”;
}

Thanks again …

no problem, if you want more about paths, see this post, started with something and then stuck diving with paths earlier :slight_smile:
http://www.scriptcase.net/forum/showthread.php?6854-sending-email-with-image-attachment-driving-me-crazy

Hello everyone,
i need rename() function run in scriptcase for file name change in localhost directory.
i have error when run the rename function as “The system cannot find the path specified. (code: 3)”.
how to set default path for rename function.
the following code is given in on validate event.
rename("/grp__NM__bg__NM__y5 LOGO1.jpg",“bgr.jpg”);
i have the file in wwwroot directory.

MIKEDESAMACHANSEMPAI has become a beast in this =S…

oh no no boss sempai i didn’t work on renaming, actually i’m preparing for you the long list and this is kinda included loool just time doesn’t help at all, will be back to you soon… meanwhile if you can help the dude up here please give him a hint, i checked my functions couldn’t find something related

cheers

rename("/grp__NM__bg__NM__y5 LOGO1.jpg",“bgr.jpg”); ??
What is that slash doing there? I assume you want a relative path from the server dir or something? Or did you mean rename(“grp__NM__bg__NM__y5 LOGO1.jpg”,“bgr.jpg”); (no slash)…
It is not common to rename a file to another directory which is what you are doing…
Check out: http://php.net/manual/en/reserved.variables.server.php
This is your document root: $_SERVER[‘DOCUMENT_ROOT’]
This is thus where you www files are placed. So doing a rename needs to take that path into account. Unless you are already on the proper dir that is. Use getcwd() for that.

Forgive me if this is considered cross posting. I just wanted this to show up for any one searching for file path and renaming. I used various codes on this forum and worked out the following solution.
Goal : Upload a file (Filename), then set the file name along with it’s path into the table.
After setting the form, I went to the onApplicationInit event and set the following
if ((substr($_SERVER[‘HTTP_HOST’],-4) == ‘8090’) OR (substr($_SERVER[‘HTTP_HOST’],-4) == ‘8090’)) { // Current Port?
$prod = TRUE;
[fpg] = “…/…/…/file/pdffiles/”;

} else {
$prod = FALSE;
[fpg] = “…/_lib/file/doc/pdffiles/”;
}
PLEASE NOTE : THE 8090 above could be different in your system

Next against the field general settings (under which the document is uploaded),I set the sub folder value to [fpg]

Next in the after onAfterInsert event I put the following code :
$fp = [fpg];
//In the following part I am generating new file name by adding recordID newly generated , I am splitting the filename into 2 parts primary and extension. then I am adding the recordid to the primary part., after that adding the extension

$filename_in_parts = explode(’.’, {document_upload_path});
$newfilename = $filename_in_parts[0] .$document_id ."." .$filename_in_parts[1]; // existing primary name, record ID, period separator, then extension
$filename = $fp .{document_upload_path};
$newfilename = $fp .$newfilename;

//following is actual code to rename the file to above generated new file name

if(file_exists($newfilename))
    { 
       echo "Error While Renaming $filename" ;
    }
else
    {
       if(rename( $filename, $newfilename))
       { 
       //echo "Successfully Renamed $filename to $newfilename" ;
       }
      else
      {
       echo "A File With The Same Name Already Exists" ;
      }
    }

sc_commit_trans(); // THIS IS VERY MUCH NECESSARY. I HAD PROBLEMS without this. I am generating record id using AutoIncrement (Manual).
// Therefore when I am trying to search for the new record in the update command below the record is not found. I crossed check with the database table and the record was not getting inserted. // So I use the commit trans.

/**

  • Update a record on another table
    */

// SQL statement parameters
$update_table = ‘documents’; // Table name
$update_where = “document_id = " .{document_id}; // Where clause
$update_fields = array( // Field list, add as many as needed
“document_upload_path = '” .$newfilename .”’",

);

// Update record
$update_sql = ‘UPDATE ’ . $update_table
. ’ SET ’ . implode(’, ', $update_fields)
. ’ WHERE ’ . $update_where;
echo $update_sql;
sc_exec_sql($update_sql);

/* SCRIPTCASE REALLY IS COOL */

2 Likes