0

This may sound like a duplicate question. That is only because, none of the others could get my code working.

My problem code

$file="blog?blogid=$_GET[blogid]";
$contents = file_get_contents(urlencode($file));

I have tried

$file=__DIR__ ."blog?blogid=$_GET[blogid]";
$contents = file_get_contents(urlencode($file));

Both return the error

Warning: file_get_contents(blog%3Fblogid%3D1): failed to open stream: No such file or directory in....

and

Warning: file_get_contents(C%3A%5CProgram+Files%5CEasyPHP-DevServer-14.1VC11%5Cdata%5Clocalweb%5Cmatrimonyblog%3Fblogid%3D1): failed to open stream: No such file or directory in C....

respectively

But they work when I remove the query parameter

?blogid=$_GET[blogid]

from the end of the file.

Just cannot understand what could be the problem

EDIT :

The purpose of using file_get_contents on a file system is to first generate output from that file using a GET parameter and then writing that output to another file.

                    $fh=fopen($page,'w+');
                    $fw=fwrite($fh,$contents);
                    header("location:$page");

Basically I am trying to create a page each for each blog post as it is submitted.

There could be better ways to to do that, But this is what came to my limited knowledge.

Thanks in advance for all help

  • What is blog here a folder or file ? If it is a folder then use / or use file extension as below if it is a php file: $file=__DIR__ ."blog.php?blogid=$_GET[blogid]"; $contents = file_get_contents(urlencode($file)); And if it a folder then there must be a file in that folder to get your parameter , create index.php in that folder and then run your code $file=__DIR__ ."index.php?blogid=$_GET[blogid]"; $contents = file_get_contents(urlencode($file)); – Mufaddal Saifee Oct 24 '18 at 12:13
  • Can you provide the absolute URL and a valid 'blogid' value for testing? – Justin T. Oct 24 '18 at 12:17
  • 1
    _Files_ don’t have URL parameters, _URLs_ do. – misorude Oct 24 '18 at 12:21
  • @MufaddalSaifee it is a file. I have tried using the .php extension too. –  Oct 24 '18 at 12:27
  • @JustinT. Any random number can be the value. The I am developing the code on my local machine –  Oct 24 '18 at 12:28
  • @misorude does that mean file_get_contents can only be called with a http or https ? –  Oct 24 '18 at 12:29
  • 1
    No, it doesn’t mean that. But you need to use an actual URL beginning with `http://` or `https://`, if you want to use _URL parameters_. As long as you are operating in the _file system_, that makes absolutely zero sense to begin with. – misorude Oct 24 '18 at 12:31
  • Please start by describing, without referring to any code or PHP functions, what it is you want to _achieve_ here in the first place. – misorude Oct 24 '18 at 12:33
  • @misorude I have added an EDIT to the question to explain the need to access a file on the system with file_get_contents –  Oct 24 '18 at 12:37
  • Well then you need to either use `http://yourdomain.com/blog?blogid=123` with file_get_contents, or (if `blog` is actually a PHP script file), you can use output buffering and include to capture the output. (In the latter case you would not supply the blogid as a parameter, the included script has access to the same variables your main script has access to automatically. If your main script was not called with `?blogid=123` to begin with, then you would need to “fake” that by assigning the value explicitly, `$_GET['blogid'] = '123';`) – misorude Oct 24 '18 at 12:42
  • I've noticed that while PHP handles basic vars inside a quote fine, I get occasionally unexpected things when referencing an array within the quote. Simply append the string(s) together to get around it - `$file="blog?blogid=".$_GET[blogid];` – ivanivan Oct 24 '18 at 12:42
  • And still, are you sure you want to load a local file and not something rendered by your webserver? Do these local files exist for all possible values of `$_GET[blogid]` (which, in itself, already triggers a warning if the constant `blogid` is not defined) – Nico Haase Oct 24 '18 at 12:42
  • @NicoHaase Actually, the idea is to create a blog page for each blog submitted. Each time it will be a new id generated during submission. –  Oct 24 '18 at 12:53
  • @ivanivan that did not work too –  Oct 24 '18 at 12:55
  • @misorude guess will have to find some work around –  Oct 24 '18 at 12:55
  • Why, if you want to capture what your web server would return when you called `http://yourdomain.com/blog?blogid=123` in your browser, then use _that_ address with file_get_contents … – misorude Oct 24 '18 at 12:56
  • Still, if you want to use `file_get_contents` on a **local** file, that file has to exist. If you want to load some file rendered by your webserver, use the proper protocol as proposed multiple times – Nico Haase Oct 24 '18 at 13:02

0 Answers0