make a variable query generated within another variable

0

I have this query to see if the file exists inside a folder called _sellos

<?php 
  $nombre_fichero = 'C:\xampp\htdocs\_sellos92-2018-E01-sellos.pdf'; 
  if (file_exists($nombre_fichero)) {

this name 7992-2018-E01-sellos.pdf is obtained from the result of 3 queries, this way:

7992 = <?php echo substr($contrato['numero_contrato'],0,4) ;?>

2018 = <?php echo $contrato['year']; ?>

E01 = <?php echo substr($contrato['numero_contrato'],-3) ; ?>-sellos.pdf

leaving something like this to generate the name to compare:

<?php $nombre_fichero = 'C:\xampp\htdocs\_sellos\<?php echo
substr($contrato['numero_contrato'],0,4) ;?>-<?php echo
$contrato['year']; ?>-<?php echo
substr($contrato['numero_contrato'],-3) ; ?>-sellos.pdf';

clearly this does not work because I include the? php and?, I have no idea how to do it within the variable without including the? php and?

When testing the answer with the following code the query stops working

$var1 = substr($contrato['numero_contrato'],0,4); 
$var2 = $contrato['year']; 
$var3 = substr($contrato['numero_contrato'],-3);

$nombre_fichero = "C:\xampp\htdocs\_sellos\$var1-$var2-$var3-sellos.pdf";

if (file_exists($nombre_fichero)) {
    echo "El fichero $nombre_fichero existe"; 
} else {
    echo "El fichero $nombre_fichero no existe";
}
    
asked by Ivan Diaz Perez 22.01.2018 в 15:34
source

1 answer

2

It will not work for you because you are calling the data wrong, you are confused with the basic PHP concepts.

The echo function is to present data on the screen, you should not use it to generate variables.

As a solution you could create variables for each string and then generate your $ variable that contains the address of the file, something like this:

$var1 = substr($contrato['numero_contrato'],0,4);
$var2 = $contrato['year'];
$var3 = substr($contrato['numero_contrato'],-3);

$nombre_fichero = "C:\xampp\htdocs\_sellos\$var1-$var2-$var3-sellos.pdf";

Note the use of "" (double quotes) instead of '' (single quotes), so that the variables are not taken as strings, but take the value assigned to them.

    
answered by 22.01.2018 / 16:05
source