close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Rename a file when uploading

Thread began 1/22/2019 4:36 am by María Luisa | Last modified 1/24/2019 10:44 am by Ray Borduin | 533 views | 5 replies |

María Luisa

Rename a file when uploading

Hi!
I have the following form to upload files:
-----
<form method="post" action="" name="FAIv1Pru" id="FAIv1Pru" enctype="multipart/form-data">

<div class="row" style="padding: 30px 0px;">
<!--fecha V1-->
<div class="col-sm-12" style="padding-bottom:20px;">
<label for="V1Date" style="color:#4437A3; font-weight:800;">Indicar fecha del vídeo 1</label>
<input type="text" class="form-control" name="V1Date" placeholder="aaaa-mm-dd" required>
</div>
<!--upload v1-->
<div class="col-sm-6" style="margin-top:30px;">
<div class="form-group">
<input type="hidden" name="MAX_FILE_SIZE" value="52428800" />
<label for="V1Pru" style="color:#4437A3; font-weight:800;">Subir vídeo Ensayo de Prueba</label>
<input type="file" name="V1Pru" id="V1Pru" accept="video/mp4,video/x-m4v,video/*" >
</div>
</div>
<!--upload cert 1-->
<div class="col-sm-6" style="margin-top:30px;">
<div class="form-group">
<label for="V1Cert" style="color:#4437A3; font-weight:800;">Subir certificado Ensayo de Prueba</label>
<input type="file" class="form-control-file" id="V1Cert" name="V1Cert" accept=".doc, .DOC, .docx, .DOCX">
</div>
<div>
<div class="clearfix"></div>
</div>
<div class="row" style="padding: 30px 0px;">
<div class="col-sm-12">
<input type="submit" value="Subir vídeo Ensayo de Prueba y su documentación" name="FAIV1" style="width:100%; background-color:#1837A3; color:#ffffff; padding:8px;">
</div>
</div>
</form>
-----

I want to change the name of those files: users in Spain name their files using characters that are not valid in programming languages ​​such as "á" or "ñ".

I prepared the following script:

----
<?php
//SCRIPTS renombrar, para upload e insert
function replace_specials_characters($s) {
$s = preg_replace("/á|à|â|ã|ª/","a",$s);
$s = preg_replace("/Á|À|Â|Ã/","A",$s);
$s = preg_replace("/é|è|ê/","e",$s);
$s = preg_replace("/É|È|Ê/","E",$s);
$s = preg_replace("/í|ì|î/","i",$s);
$s = preg_replace("/Í|Ì|Î/","I",$s);
$s = preg_replace("/ó|ò|ô|õ|º/","o",$s);
$s = preg_replace("/Ó|Ò|Ô|Õ/","O",$s);
$s = preg_replace("/ú|ù|û/","u",$s);
$s = preg_replace("/Ú|Ù|Û/","U",$s);
$s = str_replace(" ","-",$s);
$s = str_replace("ñ","n",$s);
$s = str_replace("Ñ","N",$s);
$s = preg_replace('/[^a-zA-Z0-9_.-]/', '-', $s);
return $s;
}
//campo file: V1Pru
//boton: FAIV1
//form: FAIv1Pru
if(isset($_POST["FAIV1"]) && !empty($_FILES["V1Pru"]["name"])){
$dataV = date('Y-m-d');
$nombreVideo = replace_specials_characters($_FILES["V1Pru"]["name"]);
$nombreVideoBBDD = $dataV."_".basename($nombreVideo);
}
//campo file: docFAI
//boton: FAIV1
//form: FAIv1Pru
if(isset($_POST["FAIV1"]) && !empty($_FILES["V1Cert"]["name"])){
$data = date('Y-m-d');
$nombreDocumento = replace_specials_characters($_FILES["V1Cert"]["name"]);
$nombreDocBBDD = $data."_".basename($nombreDocumento);
}
?>
----

My idea, taking as an example the upload of the video, is that the document uploads rename eliminating the special characters as it marks my function "replace_specials_characters" that I pick up in the variable "$nombreVideo".

My problem is to incorporate "$nombreVideo" to the process of renaming the file.

These are my scripts generated with WA:

----
<?php
// WA_UploadResult1 Params Start
$WA_UploadResult1_Params = array();
// WA_UploadResult1_1 Start
$WA_UploadResult1_Params["WA_UploadResult1_1"] = array(
'UploadFolder' => "__02_video_pru/",
'FileName' => "[FileName]",
'DefaultFileName' => "",
'ResizeType' => "0",
'ResizeWidth' => "0",
'ResizeHeight' => "0",
'ResizeFillColor' => "" );
// WA_UploadResult1_1 End
// WA_UploadResult1 Params End?>

<?php
WA_DFP_SetupUploadStatusStruct("WA_UploadResult1");
if(isset($_POST["FAIV1"]) || isset($_POST["FAIV1_x"])){
WA_DFP_UploadFiles("WA_UploadResult1", "V1Pru", "2", "$nombreVideo", "false", $WA_UploadResult1_Params);
}//Here I included the name of the variable $nombreVideo to rename
?>
<?php
if (abs($WA_DFP_UploadStatus["WA_UploadResult1"]["statusCode"]) != 1) {
$WAFV_Redirect = "";
$_SESSION['WAVT_crearFAI2_506_Errors'] = "";
if ($WAFV_Redirect == "") {
$WAFV_Redirect = $_SERVER["PHP_SELF"];
}
$WAFV_Errors = "";
$WAFV_Errors .= WAValidateNM($WA_DFP_UploadStatus["WA_UploadResult1"]["WA_UploadResult1_1"]["fileSize"] . "",0,52428800,""," ,",true,1);

if ($WAFV_Errors != "") {
PostResult($WAFV_Redirect,$WAFV_Errors,"crearFAI2_506");
}
}
?>
----

But the fact is that it does upload the file, but does not rename it with the criteria "date_NameFileWithoutSpecialCharacters".

If anyone has any idea about how I can fix it, I appreciate it very much.

A greeting!

Sign in to reply to this post

Ray BorduinWebAssist

I think you just have to change:
'FileName' => "[FileName]",

to:
'FileName' => "".$nombreVideoBBDD ."",

Sign in to reply to this post
Did this help? Tips are appreciated...

María Luisa

Thanks, Ray!!

Sign in to reply to this post

María Luisa

The extension is doubled

Hello
I have entered the name of the variable, but what happens now is that the extension is duplicated.
In other words, if you upload a file called:

Vídeo ensayo Matrícula 2134569.mov

Your name changes to:

2019-01-24_Video-ensayo-Matricula.mov.mov

I re-copy the code (only the part corresponding to the upload of the video):

----
<?php
//SCRIPTS renombrar, subir e insertar, dejar mensajes
$statusMsg = '';
function replace_specials_characters($s) {
$s = preg_replace("/á|à|â|ã|ª/","a",$s);
$s = preg_replace("/Á|À|Â|Ã/","A",$s);
$s = preg_replace("/é|è|ê/","e",$s);
$s = preg_replace("/É|È|Ê/","E",$s);
$s = preg_replace("/í|ì|î/","i",$s);
$s = preg_replace("/Í|Ì|Î/","I",$s);
$s = preg_replace("/ó|ò|ô|õ|º/","o",$s);
$s = preg_replace("/Ó|Ò|Ô|Õ/","O",$s);
$s = preg_replace("/ú|ù|û|ü/","u",$s);
$s = preg_replace("/Ú|Ù|Û/","U",$s);
$s = str_replace(" ","-",$s);
$s = str_replace("ñ","n",$s);
$s = str_replace("Ñ","N",$s);
$s = preg_replace('/[^a-zA-Z0-9_.-]/', '-', $s);
return $s;
}
//campo file: V1Pru
//boton: FAIV1
//form: FAIv1Pru
//reemplazar caracteres ilegales y añadir la fecha al nombre
if(isset($_POST["FAIV1"]) && !empty($_FILES["V1Pru"]["name"])){
$dataV = date('Y-m-d');
$nombreVideo = replace_specials_characters($_FILES["V1Pru"]["name"]);
$nombreVideoBBDD = $dataV."_".$nombreVideo;
}
/ WA_UploadResult1 Params Start
$WA_UploadResult1_Params = array();
// WA_UploadResult1_1 Start
$WA_UploadResult1_Params["WA_UploadResult1_1"] = array(
'UploadFolder' => "__02_video_pru/",
'FileName' => "$nombreVideoBBDD",
'DefaultFileName' => "",
'ResizeType' => "0",
'ResizeWidth' => "0",
'ResizeHeight' => "0",
'ResizeFillColor' => "" );
// WA_UploadResult1_1 End
// WA_UploadResult1 Params End?>
?>
<?php
WA_DFP_SetupUploadStatusStruct("WA_UploadResult1");
if(isset($_POST["FAIV1"]) || isset($_POST["FAIV1_x"])){
WA_DFP_UploadFiles("WA_UploadResult1", "V1Pru", "2", "$nombreVideoBBDD", "false", $WA_UploadResult1_Params);
}
?>

----

I do not understand why this happens. Can someone give me a clue?
Thank you

Sign in to reply to this post

Ray BorduinWebAssist

You will have to strip the extension from the file name since it is added automatically to match the file. I think you can just update your function to do that:

function replace_specials_characters($s) {
$s = preg_replace("/á|à|â|ã|ª/","a",$s);
$s = preg_replace("/Á|À|Â|Ã/","A",$s);
$s = preg_replace("/é|è|ê/","e",$s);
$s = preg_replace("/É|È|Ê/","E",$s);
$s = preg_replace("/í|ì|î/","i",$s);
$s = preg_replace("/Í|Ì|Î/","I",$s);
$s = preg_replace("/ó|ò|ô|õ|º/","o",$s);
$s = preg_replace("/Ó|Ò|Ô|Õ/","O",$s);
$s = preg_replace("/ú|ù|û|ü/","u",$s);
$s = preg_replace("/Ú|Ù|Û/","U",$s);
$s = str_replace(" ","-",$s);
$s = str_replace("ñ","n",$s);
$s = str_replace("Ñ","N",$s);
$s = preg_replace('/[^a-zA-Z0-9_.-]/', '-', $s);
if (strpos($s,".") !== false) $s = substr($s, 0 , (strrpos($s, ".")));
return $s;
}

Sign in to reply to this post
Did this help? Tips are appreciated...

María Luisa

Thanks Ray, I'm going to try it

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...