Skip to content

Instantly share code, notes, and snippets.

@youthlin
Created September 8, 2017 02:06
Show Gist options
  • Select an option

  • Save youthlin/8f43cbeb3d853925106b72012359861a to your computer and use it in GitHub Desktop.

Select an option

Save youthlin/8f43cbeb3d853925106b72012359861a to your computer and use it in GitHub Desktop.

Revisions

  1. youthlin created this gist Sep 8, 2017.
    29 changes: 29 additions & 0 deletions PHP: image real path
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    <?php
    echo "<h1>Hello, World</h1>" . PHP_EOL;
    echo "<p>Welcome</p>" . PHP_EOL;

    $img = "{$_SERVER["REQUEST_SCHEME"]}://{$_SERVER["HTTP_HOST"]}/uploads/qtalk.jpg";
    $prefix = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . "/";
    echo "$prefix<br>" . PHP_EOL;
    if(0 === strpos($img, $prefix)){
    $img = substr($img, strlen($prefix));
    $path = realpath($img);
    echo "img = $img\tpath = $path<br>" . PHP_EOL;
    }

    $img = "//{$_SERVER["HTTP_HOST"]}/uploads/qtalk.jpg";
    $prefix1 = "//" . $_SERVER["HTTP_HOST"] . "/";
    if(0 === strpos($img, $prefix1)){
    $img = substr($img, strlen($prefix1));
    $path = realpath($img);
    echo "img = $img\tpath = $path<br>" . PHP_EOL;
    }

    $img = "/uploads/qtalk.jpg";
    $prefix2 = "/";
    if(0 === strpos($img, $prefix2)){
    $img = substr($img, strlen($prefix2));
    $path = realpath($img);
    echo "img = $img\tpath = $path<br>" . PHP_EOL;
    }