Cutting down a variable

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
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Cutting down a variable

Post by crazycaddy »

ok, basically I need a way to turn this:

Code: Select all

$file = C:/bla/bla/egg.zip
Into this:

Code: Select all

$folder = C:/bla/bla/
$filename = egg.zip
How would I go about doing this, this has to work for file names that are any length.
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

This may not invlove php (i'm not sure) but is it possible to open for example my documents from a webpage? Ive tried a few ways but no luck, it either does nothing at all or just shows a page cannot be found message.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

find the last occurence of '/' using regex and find the length of the string.
If you have the position of the last occurence of '/', then you can read each character from then to the length of the string - 1
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

this idea seems to be easier

Code: Select all

<?
$path = "/home/httpd/html/index.php";
$tempArray = explode("/", $path);
echo "filename:".$tempArray[count($tempArray) - 1];

?>
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

raghavan20 wrote:this idea seems to be easier

Code: Select all

<?
$path = "/home/httpd/html/index.php";
$tempArray = explode("/", $path);
echo "filename:".$tempArray[count($tempArray) - 1];

?>
Works brilliant cheers, but unfortunatley its basically useless unless you can popup a local folder from a link or automatically.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

not really understood wot you meant.
this is used to get the directory of a file path.

Code: Select all

<?
$path = "/home/httpd/html/index.php";
$tempArray = explode("/", $path);
echo "filename:".$tempArray[count($tempArray) - 1]."<br />";

for($i = 1; $i < count($tempArray) - 1; $i++)
	$dirPath .= "/".$tempArray[$i];
echo $dirPath;//prints directory path



?>
if you want the path of a currently running doc, use $_SERVER['DOCUMENT_ROOT']
look at $_SERVER globals for more options
http://uk.php.net/reserved.variables
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

basically what I want is too very simply link to a folder on the users machine, eg if the user browses to the folder C:\stuff\ and submits then the script pops it open, I need to know how to open users folder from a webpage.
Post Reply