Change the contents of the Insert in an input file
Change the contents of the Insert in an input file
Hello:
I have this form to upload and, also, write in the database:
<form action="" method="post" name="form1" id="form1" enctype="multipart/form-data">
<table width="100%" align="center" cellpadding="6" cellspacing="6">
<tr valign="baseline">
<td width="50%" align="right" valign="top" class="8px_relleno"><span class="TGral_Der_peq">Tipo de documento / File type</span>:</td>
<td width="50%" align="left" valign="top" class="8px_relleno"><select name="doc_tipo">
<option value="TC" <?php if (!(strcmp("TC", ""))) {echo "SELECTED";} ?>>Dato 1 / Dato 1</option>
<option value="CE" <?php if (!(strcmp("CE", ""))) {echo "SELECTED";} ?>>Dato 2 / Dato 2</option>
<option value="RR" <?php if (!(strcmp("RR", ""))) {echo "SELECTED";} ?>>Dato 3 / Dato 3</option>
</select></td>
</tr>
<tr valign="baseline">
<td width="50%" align="right" valign="top" class="8px_relleno"><span class="TGral_Der_peq">Éste es su nombre de usuario PML / This is your PML username:</span></td>
<td width="50%" align="left" valign="top" class="8px_relleno"><input name="us_nombre" type="text" value="<?php echo $_SESSION['NombreProv']; ?>" size="32" readonly style="background-color:#E0EAE2;" /></td>
</tr>
<tr valign="baseline">
<td width="50%" align="right" valign="top" class="8px_relleno"><span class="TGral_Der_peq">Seleccione su documento / Choose your file:</span></td>
<td width="50%" align="left" valign="top" class="8px_relleno">
<input type="file" name="nombreorig_doc" id="nombreorig_doc" value="" size="32" required /></td>
</tr>
<tr valign="baseline">
<td width="50%" align="right" valign="top" class="8px_relleno"><span class="TGral_Der_peq">Describa o comente brevemente su asunto (Máximo 250 caracteres)<br />
Describe or briefly comment the subject (Max. 250 characters):</span></td>
<td width="50%" align="left" valign="top" class="8px_relleno"><textarea name="doc_asunto" id="doc_asunto" cols="45" required></textarea>
</td>
</tr>
<tr>
<td align="right" valign="top">
<span class="TGral_Der_peq">Fecha</span>
</td>
<td align="left" class="8px_relleno">
<input name="fecha" type="text" id="fecha" value="<?php echo date("Y-m-d"); ?>" size="15" readonly />
</td>
</tr>
<tr valign="baseline">
<td width="50%" align="right" valign="top" class="8px_relleno"> </td>
<td width="50%" align="left" valign="top" class="8px_relleno"><input name="SubirDocBot" type="submit" id="SubirDocBot" value="Subir documento / Upload file" /></td>
</tr>
</table>
<input type="hidden" name="fecha" value="" />
</form>
The users use the input file to upload their files. As there are several problems: users do not name their files correctly (they use characters different from those of English when naming their files) and, also, a certain protocol is necessary to name the file adding part of the user's name, date, type of document that goes up and other things, the following script was created that renames the file replacing the "illegal" characters and adding to the name those prefixes that identify the user, etc . I have to clarify that I have not managed to do this with Data Assist or with MySQLi SB:
<?php
//SCRIPTS renombrar, subir e insertar
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;
}
//Para incluir mensajes de error
$statusMsg = '';
if(isset($_POST["SubirDocBot"]) && !empty($_FILES["nombreorig_doc"]["name"])){
/*Codigo w3schools*/
$target_dir = "../_docs_para_pml/";
$documento = $_POST['doc_tipo']; //Establezco variables para incluir en el nuevo nombre del documento subido evitando las repeticiones de nombres
$data = date('Y-m-d');
$nombre_usuario = $_POST['us_nombre'];
$nombre_extract = substr($nombre_usuario,0,7);
//Doy el valor a la variable target_file incluyendo las variables anteriores
$nombreDocumento = replace_specials_characters($_FILES["nombreorig_doc"]["name"]);
$target_file = $target_dir.$nombre_extract."_".$documento."_".$data."_".basename($nombreDocumento);
$uploadOk = 1;
$TipoArchivo = pathinfo($target_file,PATHINFO_EXTENSION);
// Comprueba si el archivo existe
if (file_exists($target_file)) {
$statusMsg = "Lo sentimos, ese archivo ya existe. Renómbrelo y, por favor, súbalo de nuevo / Sorry, that file already exists. Please rename the file and upload it again.";
$uploadOk = 0;
}
// Comprueba el tamaño del archivo file size
if ($_FILES["nombreorig_doc"]["size"] > 2000000) {
$statusMsg = "Lo sentimos, su archivo es demasiado grande. Sólo documentos de menos de 2MB (E2) / Sorry, your file is too large. Please upload documents no larger than 2MB (E2).";
$uploadOk = 0;
}
//Solo ciertos formatos de archivo
if($TipoArchivo != "jpg" && $TipoArchivo != "jpeg" && $TipoArchivo != "doc"
&& $TipoArchivo != "docx" && $TipoArchivo != "pdf" && $TipoArchivo != "xlsx" && $TipoArchivo != "xls" && $TipoArchivo != "zip" && $TipoArchivo != "rar") {
$statusMsg = "Lo sentimos solo son permitidos archivos jpg, jpeg, doc, docx, xls, xlsx, zip o rar (E3). / Sorry, we can only accept the following document types: jpg, jpeg, doc, docx, xls, xlsx, zip and rar (E3).";
$uploadOk = 0;
}
// Chequea si la variable $uploadOk está a 0 por algún error
if ($uploadOk == 0) {
$statusMsg = "Lo sentimos, su archivo no ha sido subido (E4) / Sorry, your file has not been uploaded /E4).";
// si todo va bien se sube el archivo
} else {
if (move_uploaded_file($_FILES["nombreorig_doc"]["tmp_name"], $target_file)) {
$statusMsg = "El documento / The file ". basename( $_FILES["nombreorig_doc"]["name"]). " ha subido correctamente / has been correctly uploaded.";
} else {
$statusMsg = "Lo sentimos, ha sucedido un error en la subida de archivos. Por favor inténtelo de nuevo. (E5) / Sorry, there was an error in the uploading process. Please try again (E5).";
}
}
}
?>
But I want to do the insert in the database with DataAssist
My input file is the following:
<input type="file" name="nombreorig_doc" id="nombreorig_doc" value="" size="32" required />
Next I include the code of the insert:
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$InsertQuery = new WA_MySQLi_Query($conexion_i);
$InsertQuery->Action = "insert";
$InsertQuery->Table = "docs_subidos";
$InsertQuery->bindColumn("doc_tipo", "s", "".((isset($_POST["doc_tipo"]))?$_POST["doc_tipo"]:"") ."", "WA_BLANK");
$InsertQuery->bindColumn("us_nombre", "s", "".((isset($_POST["us_nombre"]))?$_POST["us_nombre"]:"") ."", "WA_BLANK");
$InsertQuery->bindColumn("nombreorig_doc", "s", "".((isset($_FILES["nombreorig_doc"]))?$_FILES["nombreorig_doc"]["name"]:"") ."", "WA_BLANK");
$InsertQuery->bindColumn("doc_asunto", "s", "".((isset($_POST["doc_asunto"]))?$_POST["doc_asunto"]:"") ."", "WA_BLANK");
$InsertQuery->bindColumn("fecha", "s", "".((isset($_POST["fecha"]))?$_POST["fecha"]:"") ."", "WA_BLANK");
$InsertQuery->saveInSession("Doc_subido");
$InsertQuery->execute();
$InsertGoTo = "";
if (function_exists("rel2abs")) $InsertGoTo = $InsertGoTo?rel2abs($InsertGoTo,dirname(__FILE__)):"";
$InsertQuery->redirect($InsertGoTo);
}
?>
But I want that in the column of my table "nombreorig_doc" actually include the variable $ target_file that I defined before to rename the file uploaded by the user.
How can I do it? Can anybody help me?
Thank you