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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

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

Post 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?
Image Image
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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)
Last edited by McGruff on Thu Aug 11, 2005 11:06 am, edited 1 time in total.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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. ;)
Image Image
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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?
Image Image
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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).
Image Image
Post Reply