Page 1 of 1

Finding if /file.php exists in dir; Breaking up a string

Posted: Fri Apr 25, 2003 10:09 pm
by phice
Let's set a person clicks on a link:
http://www.domain.com/0,8920592,311,7340429,0.php
Now, if '0,8920592,311,7340429,0.php' doesn't exists, I want to break up the file '0,8920592,311,7340429,0.php' up to where I can recieve the values of each number(s) before/after each comma.

I know that you would use .htaccess for finding if the file exists, but I'm not too sure how I would do it. I'm using a shared webhosting service, so there is no way I could edit any .conf.


Any ideas?

Posted: Fri Apr 25, 2003 10:24 pm
by McGruff

Code: Select all

<?php
$file = 'http://www.domain.com/0,8920592,311,7340429,0.php';

IF (fopen ($file, "rb")) {
    
    // file exists

} ELSE {

    $the_numbers = substr_replace($file, 22, -4); // change numbers according to domain
    $array = explode(",", $the_numbers);

}
?>
This code could go wrong for several reasons (server set up, I didn't test it :roll: etc)

Posted: Fri Apr 25, 2003 11:20 pm
by phice
Obviously I haven't explained this right. :oops: I've posted a thread similar like this one, but dealing with folders quite a long time ago. (link)


I give a link that acts like a valid file on the server, but really isn't. There is some coding inside the .htaccess of my main folder (http://www.domain.com/) that I could do this. I want to take the whole file name, send it to read_article.php?file=0,8920592,311,7340429,0.php

Keep in mind that I'm on a shared webhosting server. And, I'm the last person anyone would go to for help with .htacces/apache configuration. ;)

Posted: Sat Apr 26, 2003 6:11 am
by volka
if your provider allows custom error-pages via .htaccess

Code: Select all

ErrorDocument 404 /test/read_article.php
you might use

Code: Select all

<html><body>
<?php

// only the "filename"
$req = pathinfo($_SERVER['REQUEST_URI']);
$req = $req['basename'];

if (preg_match('![^\d,]!', $req))
	echo 'file not found: ', $_SERVER['REQUEST_URI'];
else
{ // could be an article
	if (is_file($req.'.php'))
		echo 'read article ', $req;
	else
		echo 'no such article: ', $req;
}
?>
</html></body>
as dispatcher
(my script is in /test/, but that shouldn't matter)

Posted: Sat Apr 26, 2003 3:11 pm
by phice
the 0,8920592,311,7340429,0.php isn't a real file, but I'm acting like it is, so the viewer doesn't see a ?file=8920592&section=311&aid=7340429...

I've made a little code that will explode the string into an array, then show the strings for each section.

Code: Select all

<?php
$string = "0,8920592,311,7340429,0";
echo "<B>\$string</B> = $string<P />\n\n";
$string = explode(",", $string);
for($i = 0; $i < count($string); $i++)
      echo "<B>\$string[$i]</B> = " . $string[$i] . "<BR />\n";
?>

Now, instead of $string = "0,8920592,311,7340429,0";, what would I put for $string? $HTTP_REFERER?

Posted: Sat Apr 26, 2003 3:53 pm
by volka
if you're using ErrorDocument

Code: Select all

$string = pathinfo($_SERVER['REQUEST_URI']);
$string = $req['basename'];
or similar (with pathinfo it doesn't matter for which "path" the request has been made)

Posted: Sat Apr 26, 2003 7:13 pm
by phice
My code:

Code: Select all

<?php
$req = pathinfo($_SERVER['REQUEST_URI']); 
$string = $req['basename'];
$extention = eregi_replace("(\..*)$", "", $string);
$extention = str_replace($extention, "", $string);
echo "<B>\$string</B> = $string<BR />\n";
echo "<B>\$extention</B> = $extention<P />\n\n";
$string = explode(",", $string);
for($i = 0; $i < count($string); $i++)
      echo "<B>\$string[$i]</B> = " . $string[$i] . "<BR />\n";
?>
Results:
$string = link.php
$extention = .php
$string[0] = link.php
Firstly, I'm tring to keep it from just forwarding to read_article.php (in the address bar). If this isn't possible, then I'll have to find another way of doing what I want.

Second, it shows link.php, and not the page that gave the error (0,8920592,311,7340429,0.php).