Jump to content

[SOLVED] Problem running my code when the directory name has spaces.


OM2

Recommended Posts

I created a bit of code with help from answers given to other posts.

The code takes a PHP page and gives me the PHP output source code for the resulting HTML page.

 

The code is:

<?php
    $url = "http://" . $_SERVER[ 'HTTP_HOST' ] . urldecode( $_GET[ "u" ] );
    $lines = file( $url );
?>
<?php
$wholePage = "";

    foreach( $lines as $line_num => $line ) 
{
	$line = htmlspecialchars( $line );
	$line = str_replace( "<", '<span><', $line );
	$line = str_replace( ">", '></span>', $line );
	$line = str_replace( "<!--", '<em><!--', $line );
	$line = str_replace( "-->", '--></em>', $line );

	$wholePage = $wholePage . $line;
}

echo nl2br($wholePage);
?>

 

Unfortunately, I have a problem: if the directory that my PHP file sits in has a space.

 

The way the code is being called is:

 

http://www.domain.com/dirs/users/myName/source.php?u=/dirs/users/myName/index.php

 

What am I doing wrong?

 

Thanks.

 

 

OM

Link to comment
Share on other sites

u mean, instead of:

$url = "http://" . $_SERVER[ 'HTTP_HOST' ] . urldecode( $_GET[ "u" ] );

i should use:

$url = "http://" . $_SERVER[ 'HTTP_HOST' ] . urlencode( $_GET[ "u" ] );

?

 

that's funny because, i would have thought, urldecode and urlencode are opposites.

looked both up in php.net... aparently not (though it could be me misundersatnding and misreading).

 

let me know what u think.

thanks.

Link to comment
Share on other sites

No, thats not what I meant.

 

Have you got some code that generates these links?

 

http://www.domain.com/dirs/users/myName/source.php?u=/dirs/users/myName/index.php

 

Its there that you need to run through urlencode. If you are simply accessing these pages manually you will need to use %20 in place of spaces.

Link to comment
Share on other sites

oh... i see what u mean.

yes, i have code like what u say.

i have:

<a href="source.php?u=<?= $_SERVER[ "PHP_SELF" ] ?>">View Source</a>

the only thing is, after failing to get it to work, i manually added the %20 in the url, but this didn't solve the problem.

still confused why it's not wokring. :(

Link to comment
Share on other sites

The entire problem is that *nix does not like directories of filenames to contain spaces. For instance, in the Linux shell if I wanted to cd into a directory called /this is foo I would need to use...

 

$ cd /this\ is\ foo

 

Why have your directories got spaces in there names in the first place?

Link to comment
Share on other sites

just to make sure i wasnt missing anything, i did as u suggested.

 

the error i got was:

 

"The requested URL /dirs/users/name/my was not found on this server"

 

i've quoted the error exactly as i got it.

the actual directory name was 'my dir' - this isn't printed above, it seems to be missing all of the letters after the space.

i'm scratching my head over this... cant figure it out. :(

Link to comment
Share on other sites

The entire problem is that *nix does not like directories of filenames to contain spaces. For instance, in the Linux shell if I wanted to cd into a directory called /this is foo I would need to use...

 

$ cd /this\ is\ foo

 

Why have your directories got spaces in there names in the first place?

well... i'm trying to develop a system where users can duplicate directories and give those directories ANY name they wish - with a space if necessary.

if it was for me only, then i'd be happy to just get passsed the problem and do without having spaces.

but the problem is users: i'd rather not stick in an artificial rule not allowing them to have spaces in the names.

 

let me know what you think.

thanks.

Link to comment
Share on other sites

But spaces shouldn't be in directory / file names. This is a irregularity inherited via a faulty design in windows.

 

Anyway, your going to need to explain exactly what it is your trying to do. The file() function asks for a filename, not a url. While this will work on some servers, by default most servers will have url_wrappers disabled and your code will fail.

 

What exactly is this codes intended purpose?

Link to comment
Share on other sites

hmmm... those damn spaces!!

 

what i want to do: the php produces html that u see on your browser.

i want the source of the html code (NOT the source php code).

 

after getting help from a few questions i posted, i came up with the above script.

can u think of a better way to get the source code?

thanks.

Link to comment
Share on other sites

just thought i'd let u know: i found a solution in the end.

it was much simpler than the code i had.

i've got the following code:

 

<?php 
$rawPageCode = htmlspecialchars(file_get_contents('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'])); 
$cleanPageCode = preg_replace("/<!--\s*Admin Code Start\s*-->.*?<!--\s*Admin Code End\s*-->/is", "", $rawPageCode);
echo $cleanPageCode;
?>

 

the above does exactly what i need it to do and spaces don't seem to be an issue anymore.

thanks for all the replies - much appreciated.

Link to comment
Share on other sites

hmmm... i still had problems with spaces!

(and also the code itself wasn't quite right.)

i've fixed now.  now the following works:

 

<?php 
$newURL = str_replace(" ", "%20", 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']));
$rawPageCode = htmlspecialchars(file_get_contents($newURL)); 
$cleanPageCode = preg_replace("/<!--\s*Admin Code Start\s*-->.*?<!--\s*Admin Code End\s*-->/is", "", $rawPageCode);
echo $cleanPageCode;
?>

(the above summes u r interested in the index page.  else, u can specifiy whatever page u want.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.