RE: [PHP] Getting part of a string...Was protecting a file via php

2003-09-17 Thread Steve Jackson
Thanks to all so far.
However I am still having problems I have directory outside the root
with PDF's in it. The links to the PDF files are called successfully by
this function in a page I call getlinks.php

function do_non_root_links()
{
define('FILEDIR', '/home/.sites/144/site281/downloads/'); 
//display available files
$d = dir(FILEDIR); 
while($f = $d-read()) 
{ 
   //skip 'hidden' files 
   if($f{0} != '.') 
   {
echo trtd; 
echo a href=\get.php?file=$f\ class=\greenlinks\
target=\_blank\$f/a; 
echo /td/tr;
} 
} 
$d-close(); 
}

The URL displayed is then to be processed by get.php. My problem *I
think* is losing get.php?file= from the URL which I managed to do by
doing $pfile = str_replace('get.php?file=','','$file');
This however then becomes confusing because $pfile I assumed would then
be simply the filename without the URL. I figured that if I exploded the
name and pulled the extension from the file passed that into a switch to
send the appropriate header my problem would be solved. However it
isn't. This is the code and de-bugging I've done.
? 
define('FILEDIR', '/home/.sites/144/site281/downloads/'); 
$path = FILEDIR . $file; 

//check that this file exists and that it doesn't include 
//any special characters 
if(!is_file($path) OR !eregi('^[A-Z_0-9][A-Z_0-9.]*$', $file)) 
{ 
header(Location: error.php); 
exit(); 
} 

/* 
** //check that the user has permission to download file 
** if(user does not have permission) 
** { 
** //redirect to error page 
** header(Location: error.php); 
** exit(); 
** } 
*/ 


$pfile = str_replace('get.php?file=','','$file');
$p = explode('.', $file); 
$extension = $p[sizeof($p)-1];
// debug - remove the headers and test output vars.
echo Test and $extension;
echo brTest and $file;
echo brTest and $p;
echo brTest and $pfile;
/*switch ($extension)
{
// define headers depending on $extension variable.
case doc :
header(Content-type: application/msword\n);
break;
case pdf :
header(Content-type: application/pdf\n);
break;
default :
// force download dialog if no extension defined.
header(Content-type: application/octet-stream\n); 
header(Content-disposition: attachment; filename=\$file\\n);
break; 
} 

header(Content-transfer-encoding: binary\n); 
header(Content-length:  . filesize($path) . \n); 
//send file contents 
$fp=fopen($path, r); 
fpassthru($fp); 
*/
? 

Echoing the vars produces this. The exploded file ($extension) is what I
need to pass to the switch but if I send the headers as is again I get a
blank and corrupted explorer window. 

Test and pdf
Test and EventNotification_01.pdf
Test and Array
Test and $file

Any ideas how to proceed?
Cheers,

Steve.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Getting part of a string...Was protecting a file via php

2003-09-17 Thread Steve Jackson
I can now download the file as application/octet-stream but it won't
allow me to open the file inline as a PDF. The only way I managed to be
able to download the file at all was by stripping the URL from the front
of the filename (I think).
What am I doing wrong??
$pfile = str_replace('get.php?file=','','$file');
$p = explode('.', $pfile); 
$extension = $p[sizeof($p)-1];

$pfile when echoed is $file
$file when echoed is the correct name of the file which is even more
confusing.
I figured that $file would include the URL which might be why it hanged
explorer.

Steve Jackson
Web Development and Marketing Manager
Viola Systems Ltd.
http://www.violasystems.com
[EMAIL PROTECTED]
Mobile +358 50 343 5159





 -Original Message-
 From: Steve Jackson [mailto:[EMAIL PROTECTED] 
 Sent: 17. syyskuuta 2003 13:53
 To: 'PHP General'; 'Jon Haworth'
 Subject: RE: [PHP] Getting part of a string...Was protecting 
 a file via php
 
 
 Thanks to all so far.
 However I am still having problems I have directory outside 
 the root with PDF's in it. The links to the PDF files are 
 called successfully by this function in a page I call getlinks.php
 
 function do_non_root_links()
 {
 define('FILEDIR', '/home/.sites/144/site281/downloads/'); 
 //display available files
 $d = dir(FILEDIR); 
 while($f = $d-read()) 
 { 
//skip 'hidden' files 
if($f{0} != '.') 
{
   echo trtd; 
 echo a href=\get.php?file=$f\ 
 class=\greenlinks\ target=\_blank\$f/a; 
 echo /td/tr;
   } 
 } 
 $d-close(); 
 }
 
 The URL displayed is then to be processed by get.php. My problem *I
 think* is losing get.php?file= from the URL which I managed 
 to do by doing $pfile = str_replace('get.php?file=','','$file');
 This however then becomes confusing because $pfile I assumed 
 would then be simply the filename without the URL. I figured 
 that if I exploded the name and pulled the extension from the 
 file passed that into a switch to send the appropriate header 
 my problem would be solved. However it isn't. This is the 
 code and de-bugging I've done. ? 
 define('FILEDIR', '/home/.sites/144/site281/downloads/'); 
 $path = FILEDIR . $file; 
 
 //check that this file exists and that it doesn't include 
 //any special characters 
 if(!is_file($path) OR !eregi('^[A-Z_0-9][A-Z_0-9.]*$', $file)) 
 { 
 header(Location: error.php); 
 exit(); 
 } 
 
 /* 
 ** //check that the user has permission to download file 
 ** if(user does not have permission) 
 ** { 
 ** //redirect to error page 
 ** header(Location: error.php); 
 ** exit(); 
 ** } 
 */ 
 
 
   $pfile = str_replace('get.php?file=','','$file');
   $p = explode('.', $file); 
   $extension = $p[sizeof($p)-1];
   // debug - remove the headers and test output vars.
   echo Test and $extension;
   echo brTest and $file;
   echo brTest and $p;
   echo brTest and $pfile;
 /*switch ($extension)
   {
   // define headers depending on $extension variable.
   case doc :
   header(Content-type: application/msword\n);
   break;
   case pdf :
   header(Content-type: application/pdf\n);
   break;
   default :
   // force download dialog if no extension defined.
   header(Content-type: application/octet-stream\n); 
 header(Content-disposition: attachment; filename=\$file\\n);
   break; 
   } 
 
   header(Content-transfer-encoding: binary\n); 
 header(Content-length:  . filesize($path) . \n); 
   //send file contents 
 $fp=fopen($path, r); 
 fpassthru($fp); 
   */
 ? 
 
 Echoing the vars produces this. The exploded file 
 ($extension) is what I need to pass to the switch but if I 
 send the headers as is again I get a blank and corrupted 
 explorer window. 
 
 Test and pdf
 Test and EventNotification_01.pdf
 Test and Array
 Test and $file
 
 Any ideas how to proceed?
 Cheers,
 
 Steve.
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Getting part of a string...Was protecting a file via php

2003-09-16 Thread Jon Haworth
Hi Steve,

 $pfile = str_replace('get.php?file=','','$file');
 $p = explode('.', $pfile);
 // How do I say look at the extension and put it into the switch?
 $extension = $p;
 // end of the bit I'm stuck on.

At this point $p is an array rather than a variable, so you'll need to get
the last element of $p.Try something like this:

$p = explode('.', $pfile);
$extension = $p[sizeof($p)-1];

Cheers
Jon

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Getting part of a string...Was protecting a file via php

2003-09-16 Thread Jurgens du Toit
If you use pathinfo($pfile), you'll get an array with the dirname, 
basename and extension...

Jrgns
--

[EMAIL PROTECTED]
083 511 7932
For those who believe no evidence is necessary.
For those who don't, no evidence would suffice.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Getting part of a string...Was protecting a file via php

2003-09-16 Thread Jason Wong
On Tuesday 16 September 2003 21:24, Jon Haworth wrote:

 At this point $p is an array rather than a variable, so you'll need to get
 the last element of $p.Try something like this:

 $p = explode('.', $pfile);
 $extension = $p[sizeof($p)-1];

Better still, use array_pop().

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
Your car horn will always get stuck when your're behind a group of Hell's 
Angels
-- Murphy's Driving Laws n„1¤719
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Getting part of a string...Was protecting a file via php

2003-09-16 Thread Curt Zirzow
* Thus wrote Jon Haworth ([EMAIL PROTECTED]):
 Hi Steve,
 
  $pfile = str_replace('get.php?file=','','$file');
  $p = explode('.', $pfile);
  // How do I say look at the extension and put it into the switch?
  $extension = $p;
  // end of the bit I'm stuck on.
 
 At this point $p is an array rather than a variable, so you'll need to get
 the last element of $p.Try something like this:
 
 $p = explode('.', $pfile);
 $extension = $p[sizeof($p)-1];

I always consider explode to be overkill when a simple:

 $extension = substr($pfile, strrpos($pfile, '.')+1);

would do.


Curt
-- 
I used to think I was indecisive, but now I'm not so sure.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php