Access files outside the project root folder (Web Development)

Asked

Viewed 1,211 times

1

I’m working on a web system using Docker with Nginx. And I am not being able to access files that are not in the root folder of the project, if possible I would like to understand what is happening and how I can solve.

Below is the code of the Docker-Compose.yml

 nginx:
   image: tutum/nginx
   ports:
     - "80:80"
   links:
     - phpfpm
   volumes:
     - ./nginx/default:/etc/nginx/sites-available/default
     - ./nginx/default:/etc/nginx/sites-enabled/default
     - ./logs/nginx-error.log:/var/log/nginx/error.log
     - ./logs/nginx-acess.log:/var/log/nginx/acess.log

phpfpm:
    image: php:fpm
    ports:
      - "9000:9000"
    volumes:
      - ./html:/usr/share/nginx/html

mysql:
    image: mariadb
    environment:
      - MYSQL_ROOT_PASSWORD=admin
    volumes:
      - ./mysql:/var/lib/mysql

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    links:
      - mysql
    ports:
      - 8183:80
    environment:
      - MYSQL_USERNAME=admin
      - MYSQL_ROOT_PASSWORD=admin
      - PMA_ARBITRARY=1

Below is the default for NGINX:

server {
listen 80;

root /usr/share/nginx/html;

server_name "IP_LOCAL";

location / {
    try_files $uri /index.php$is_args$args;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass phpfpm:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
   }
}

Image of how the project is structured with the notes inserir a descrição da imagem aqui

Output on localhost inserir a descrição da imagem aqui

Code index.php

<head>
    <meta charset ="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel= "stylesheet" href="config/css/bootstrap.min.css">
    <link rel= "stylesheet" href="config/css/login.css">

</head>

<body>  
    <div class="login-form col-xs-10 col-xs-offset-1  col-sm-6 col-sm-offset-3  col-md-4 col-md-offset-4">
        <header>
            <h1><img class="img-responsive" src="img/app-mini.png"></h1>
            <h2 class="text-center"> Entre com o seu <b>usuário</b> e <b>senha</b> </h2>
        </header>
        <form>
            <div class="form-group">
                <div class="input-group">
                    <div class="input-group-addon">
                        <span class="glyphicon glyphicon-user"></span>
                    </div>
                    <input type="text" name="usuário" class="form-control" placeholder="Usuário">
                </div>
            </div>
            <div class="form-group">
                <div class="input-group">
                    <div class="input-group-addon">
                        <span class="glyphicon glyphicon-option-horizontal"></span>
                    </div>
                    <input type="password" name="senha" class="form-control" placeholder="Senha">
                </div>
            </div>
            <footer>
            <div class="checkbox pull-left">
                <label> <input type="checkbox" name="Lembrar">Esquecer a Senha</label>              
            </div>
            <button type="submit" class="btn btn-primary pull-right"> Entrar</button>
            </footer>
        </form>
    </div>

    <script src= "config/js/jquery-3.2.1.min.js"></script>
    <script src="config/js/bootstrap.min.js"></script>

</body>
  • "- I am unable to access the files." What files? Where are you "invoking them"? Post your code in text form: manual on how NOT to ask questions.

  • Lipespry-off-, Code added.

1 answer

1

  • When you invoke some file on the server, you can "level up" the directory with ../ without being limited to the site’s root directory.

  • When you invoke some file on the client (browser), you get limited to the root directory and its subdirectories.

See in your index.php these lines:

...
<link rel= "stylesheet" href="config/css/bootstrap.min.css">
<link rel= "stylesheet" href="config/css/login.css">
...
<script src= "config/js/jquery-3.2.1.min.js"></script>
<script src="config/js/bootstrap.min.js"></script>
...

Note the directory structure where is the index.php:

Níveis

On the client, you can invoke the files from the root adding a / at the beginning of the path. Remember that the root is the directory where the index.php, that is to say: http://meusite.com.br/index.php.

On the server, with PHP, you can access any folder. As long as the user running Apache has such permission. Then the / represents the root directory of your server, when it comes to Linux.

Ideal is for you to see where the summoner file is and, to each directory you return, add ../ on the way.

A simple example: say you want the contents of the file default that’s in the folder nginx from the index.php that’s in the folder app:

<?php
    $arquivo = file_get_contents('../nginx/default');
?>
  • I’m in the briefcase app (calling from the index.php);
  • I returned a folder: ../;
  • I got into the folder nginx/;
  • I indicated the file default;
  • I understand but still can not bring the bootstrap and nor access an own image bringing to root in a test page.

  • @Jack Have you ever tried to access these files directly, via browser?! Example: http://localhost/config/js/bootstrap.min.js. Have you checked if apache has read permission in these files you’re trying to access? Have you checked the browser console?

Browser other questions tagged

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