String Concatenation in a Directory

0

Good person, I want to concatenate a string, put it at the end of the directory, which will be my path where saved images, each user has their folder on the server. I am not able to concatenate due to the bars that in the explorer is the opposite, if someone can help me I am grateful!

 <?php
$nomepasta = "morto";
$base = $_REQUEST['image'];
$filename = $_REQUEST['filename'];
$binary = base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen(('UploadFotos/'.$nomepasta).$filename, 'wb');
$teste = 'UploadFotos\'.$nomepasta;
fwrite($file, $binary);
fclose($file);
echo $teste;
    
asked by anonymous 27.08.2016 / 00:32

2 answers

1

PHP already has appropriate constants to return the OS's bars, and other things related to the filesystem .

To return the slashes, the constant used is:

DIRECTORY_SEPARATOR

Example usage:

$path = 'uploadfotos'.DIRECTORY_SEPARATOR.'nomepasta';

or:

$path = join( DIRECTORY_SEPARATOR, array( 'raiz', 'uploadfotos', 'nomepasta' ) );

Doing this way, the same code will work correctly regardless of the operating system (provided the path is correct, of course).

Another advantage is that it prevents the bars from being understood as escape characters.

If the paths come from a DB, you can always record them with a regular slash and use a replace:

$path = str_replace( '/', DIRECTORY_SEPARATOR, $caminho_vindo_do_db );

More details in the manual:

  

link

    
27.08.2016 / 15:37
0

Julian I am analyzing your code and I agree with mention of William Novak, other thing before bars work as escape in php, see the examples:

    <?php
    \quando quer ter o resultado meu nome é "Antonio".
    echo 'meu nome é \"Antonio\"';

In order for the slash to be included in the text it should be user as follows

    <?php
    echo 'menu nome é \"Antonio\"';
    \o resultado será meu nome é Antonio\

The escape works to inform you that the quotation marks are not included in the code. try to use something like

    <?php
    $pasta_imagens = "diretorio_principal";
    $nomepasta = "/nome_da_pasta";
    //agora é possivel concatenar sem erros de scape ficando assim
    $file = fopen(($pasta_imagens.$nomepasta).$filename, 'wb');

There are many different ways of solving it. I tried to explain it in a simpler way, but you already have an idea of how you can try to solve it.

    
27.08.2016 / 15:34