0

I am trying to read a file's content, while doing something like this

$con="HDdeltin";
$fp = fopen($_SERVER['DOCUMENT_ROOT']. "/HDdeltin/Users/$con", "r");
echo $fp;

It doesn't return anything. What am I doing wrong?

user2226755
  • 12,494
  • 5
  • 50
  • 73
user287210
  • 41
  • 4
  • fopen() returns a resource ID, not content. – John Conde Jul 16 '14 at 13:45
  • Start with [the docs on `fopen()`](http://us3.php.net/manual/en/function.fopen.php). It returns a file handle from which you must `fread()` or `fgets()`. More likely, you wan t something simpler like [`file_get_contents()`](http://php.net/manual/en/function.file-get-contents.php). – Michael Berkowski Jul 16 '14 at 13:45

4 Answers4

0

fopen return a resource, not the text

Use file_get_contents($_SERVER['DOCUMENT_ROOT']. "/HDdeltin/Users/$con") instead

Sal00m
  • 2,938
  • 3
  • 22
  • 33
0

you can read by using this function

echo file_get_contents($_SERVER['DOCUMENT_ROOT']. "/HDdeltin/Users/$con");
Mubo
  • 1,078
  • 8
  • 16
0

You will likely need to use file_get_contents. http://php.net/manual/en/function.file-get-contents.php

Here is the example they provide:

// Read 14 characters starting from the 21st character
$section = file_get_contents('./people.txt', NULL, NULL, 20, 14);
var_dump($section);

So you will likely need to use

$fp = file_get_contents($_SERVER['DOCUMENT_ROOT']. "/HDdeltin/Users/$con", true);
echo $fp;

There are some differences between PHP versions it seems

<?php
// <= PHP 5
$file = file_get_contents('./people.txt', true);
// > PHP 5
$file = file_get_contents('./people.txt', FILE_USE_INCLUDE_PATH);
?>

So be sure to check out the first link provided.

The Humble Rat
  • 4,586
  • 6
  • 39
  • 73
0

Fast solution :

file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/HDdeltin/Users/HDdeltin");

Use file_get_contents(), and you can prevent bad url with realpath().

$con = "HDdeltin";
$path = realpath($_SERVER['DOCUMENT_ROOT'] . "/HDdeltin/Users/$con");
echo file_get_contents($path);

To get current root, you can also use :

See more :

Community
  • 1
  • 1
user2226755
  • 12,494
  • 5
  • 50
  • 73