String concatenation in a directory

Asked

Viewed 308 times

0

Good person, I want to concatenate a string, put it at the end of the directory, which will be my path where saved the images, each user has its folder on the server. I’m not able to concatenate due to the bars that in the explorer is the opposite,!

 <?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;
  • According to the php the information $_FILE was removed from $_REQUEST since the version 4.3.0, if you are using a higher version, switch to $_FILE. As for the bar, try "/UploadFotos/{$nomepasta}" or //' in place of /'.

  • The problem is that the image is saving outside the desired folder, you are saving in the root /Uploadphotos/, when you should save inside a folder Uploadphotos/[email protected]. That’s why I think it’s a question of Apas, because I did the test by putting the direct email in the code and it worked!

2 answers

1

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

To return the bars, the constant used is:

DIRECTORY_SEPARATOR

Example of use:

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

or:

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

By doing so, the same code will work properly independent of the operating system (provided the path is correct, of course).

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

If the paths come from a DB, you can always save them with normal bar and use a replace:

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

More details in the manual:

http://php.net/manual/en/dir.constants.php

  • Good people ! I found the answer: $file = fopen("Uploadphotos/$filename/".$filename, 'Wb')!

0

Juliano I’m analyzing your code and agree mention of William Novak, another thing bars before quotes 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 bar to be included in the text must be used as follows

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

the exhaust works to inform that the quotes 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 tried to explain in a simpler way, but it is already possible to have an idea of how you can try to solve.

  • $_REQUEST discontinued? when this?

  • The problem that my "folder name" is a variable that comes from my app, I don’t know if that’s not the problem,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.