How to filter that the name of a file does not exceed a character length in the browser or in PHP?

4

I want to save the name of a PDF file in a column of a database. The name of the column is nombre_pdf with a varchar(50) and the problem I have is that if the nombre_pdf exceeds the limit of 50 characters, the whole name will not be saved.

I know that with the function substr of PHP can be done, but I do not know where I can use it: if in the form, in the function where the insertion is made to the database, the following function is where it inserts to the database:

public function registro( $nombre_pdf, $autor, $fecha, $archivo, $carrera ){

    $sql = "INSERT INTO tesis ( nombre_pdf, autor,
    fecha, archivo, carrera ) VALUES ( '$nombre_pdf', '$autor', '$fecha', '$archivo', '$carrera' )";
    $consulta = $this->conecta()->query($sql);
    echo '<script language="javascript">
            alert("Los datos se registraron con exito");
            window.location.href="index.php";
          </script>';
}
    
asked by R.C. Ana 31.05.2017 в 19:40
source

2 answers

5

I recommend that you do not let the user write more than 50 characters from the form. For this, you will not need any PHP function but you can do it directly from the input itself with the attribute maxlength .

Example:

<form>
  <input type="text" maxlength="50">
</form>

However, since HTML can be modified on the client side, it is convenient that you also use PHP checking. To make sure, as you very well say, you can use the substr function, although you should do it before INSERT so you do not have problems inserting it into the database.

public function registro( $nombre_pdf, $autor, $fecha, $archivo, $carrera ){
    $nombre_pdf = substr($nombre_pdf, -1, 50);
    //Aquí iría tu INSERT
}

If you notice, in the second parameter I use -1 to start at the end and in this way also contain the extension .pdf and do not have problems when referring to the file afterwards.

    
answered by 31.05.2017 / 19:54
source
0

The logic by which you can only store a name of up to 50 characters would belong to the model part and although you should pass the "prepared" data to the "registro()" method, it would be "appropriate" to prepare the length of that name in the own method before mounting the INSERT .

Actually you should use a private method of the class to do that, or you'd rather create one in a helper class (which you could name StringTools , for example) in order to prepare the name if it is too long (and even if it is not).

A method that adapts the name of the file can allow you; preserve letters instead of spaces, replace spaces with scripts, substitute years for enes or remove accents (less future problems), etc. It is also advisable to use mb_substr () instead of substr () , which will not cut your 50 characters if you find accents, eñes, etc ... You can see the documentation: link

And your method would look like this:

public function registro( $nombre_pdf, $autor, $fecha, $archivo, $carrera ) {

    $nombre_pdf = StringTools::prepareFileName( $nombre_pdf );

    $sql = "INSERT INTO tesis ( 
        nombre_pdf, 
        autor,
        fecha, 
        archivo, 
        carrera 
    ) VALUES ( 
        '$nombre_pdf', 
        '$autor', 
        '$fecha', 
        '$archivo', 
        '$carrera' 
    )";
    $consulta = $this->conecta()->query( $sql );

    echo '<script language="javascript">
            alert("Los datos se registraron con exito");
            window.location.href="index.php";
          </script>';
}

And StringTools :: prepareFileName () would have as its purpose to change something like this:

"Éste puede ser un nombre demasiado largo para almacenar"

for this other:

"EstePuedeSerUnNombreDemasiadoLargoParaAlmacenar"

Writing it in another class will allow you to use it from other points of your application and easily take it to other projects.

One thing I do not like, although it is not part of the question is that you have code of sight (the alert) in the method that is storing information in the database, I recommend studying the MVC pattern strong>. In this respect, your method could return a boolean and generate a 'feedback' where appropriate.

Greetings.

    
answered by 31.05.2017 в 19:48