phpdockerimagemove-uploaded-file

How to display an image (picture) inside PHP using Docker bind mounts


I'm trying to upload an image using the img tag in PHP.

I'm using Docker with bind mounts (read and write permission) for a non-root user for my upload folder in Docker. My image still isn't showing up. The $path variable is showing as "../upload/ferrari_logo.php" but it doesn't seem to display the image after using the move_uploaded_file function in PHP.

How can I get the image to properly show up in the "about_me_img" tag?

mypage.php

<div class="photo_column">
<form method="post" id="upload_form" action="mypage.php" enctype="multipart/form-data">
    <div id="about_me_photo">

    <?php
        $message = '';
        $moved = false;

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            if ($_FILES['photo_upload']['error'] === 0) {
                $temp = $_FILES['photo_upload']['tmp_name'];
                $path = '../upload/' . $_FILES['photo_upload']['name'];
                $moved = move_uploaded_file($temp, $path);
            } 

            if ($moved === true) {
                echo '<img id="about_me_img" src="' . $path . '"alt="">';
            } else {
                echo '<img id="about_me_img" src="" alt="">';
            }
        } else {
            echo '<img id="about_me_img" src="" alt="">';
        }

    ?>
            <div class="about_me_row">
                <input type="file" id="photo_upload" name="photo_upload" accept="image/*">
                <input type="submit" class="mypage_button"  id="submit_photo" value="Upload">
            </div>
            
        
    </div>
</form>
</div>

docker-compose.yml

version: "3.9"
services:
  php-apache:
    ports:
      - "8000:80"
    build: './build/php'
    volumes:
      - ./app/public:/var/www/html
      - ./src:/var/www/src
      - ./config:/var/www/config
      - ./upload:/var/www/upload
      - ./img:/var/www/img
    command: /bin/sh -c "sudo chmod -R 777 /var/www/upload && apache2-foreground"
  mysql:
    image: mysql:latest
    ports:
      - "3306:3306"
    build: './build/mysql'
    environment:
      MYSQL_ROOT_PASSWORD: "password"
      MYSQL_DATABASE: "commune"
    volumes:
      - dbData:/var/lib/mysql
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
    depends_on:
      - mysql
    restart: always
    ports:
      - 8080:80
volumes:
  app:
  src:
  config:
  upload:
  img:
  dbData:

Solution

  • I had the wrong paths in Docker. I changed ./upload:/var/www/upload to ./app/public/upload:var/www/upload and now it works.

    My folder structure is:

    I didn't receive a destination error because I had a duplicate upload folder outside the app folder, which I just deleted.

    EDIT: Also, I meant "transitioning" not transmitting in the comments but can't edit.