Jump to content

Moving up a directory code


newbiePHP

Recommended Posts

Hey guys,

 

I have a engine that finds a file in a directory and opens it.

 

The file i want to open is in C:\temp. The PHP file is located in C:\wamp\www.

 

In the code when i set the directory to C:/temp/. The program runs fine.

 

When I set the directory to ../../temp/ it doesn't run.

 

In order to go up a directory is the command ../ and do I need to import something to make it work?

 

Thanks for any help!!

Link to comment
Share on other sites

I tested it using XAMPP, and it worked just fine for me:

HTDOC ROOT:

c:\xampp\htdocs\jobsite2\test.php

test file:

c:\Temp\test.php

CONTENTS OF jobsite2/test.php:

<?php
$server_root = "../../../temp/";
include($server_root."test.php");

?>

CONTENTS OF temp/test.php:

<?php
print "it worked!";
?>

OUTPUT RECEIVED:

it worked!

So, my test worked fine. I don't see why you would have any issues doing the same.

Link to comment
Share on other sites

If i were to change the $directory to "C:/Temp" It works fine.

 

<?php

$FindFile = "Example";

$directory = '../../Temp/'; //trailing slash

$flag = false;
$ext = array( '.jpg' , '.gif' , '.txt' );
for( $i = 0; count( $ext ) > $i; $i++ )
{

     if( file_exists( $directory . $FindFile . $ext[$i] ) )
     {
          $flag = true;
          $name = $directory . $FindFile . $ext[$i];
          break;
     }
}

if( $flag == true )
{
	 exec($name);
}
?>

Link to comment
Share on other sites

you're trying to run an image?

 

I did a slight mod of your script (changed example to test, had it go one more level up, and have it print the name of the file instead of executing it):

<?php
$FindFile = "test";
$directory = '../../../Temp/'; //trailing slash
$flag = false;
$ext = array( '.php', '.jpg' , '.gif' , '.txt' );
for( $i = 0; count( $ext ) > $i; $i++ )
{
     if( file_exists($directory.$FindFile.$ext[$i] ) )
     {
          $flag = true;
          $name = $directory . $FindFile . $ext[$i];
          break;
     }
}
if( $flag == true )
{
	 print $name;
}
?>

it worked, for what it is.  Only problem I see is you can't just

exec($name);

because going off the script you posted, you're trying to execute an image. Windows (as well as linux, and every other system in the world) can't execute an image.  You also can't execute a php file in windows, unless your system knows how to execute a .php extension.

Link to comment
Share on other sites

No example is a text file and the whole program runs fine and opens that text file when the directory is set to C:/temp

 

I just have those other extensions as options to play around with if i get .txt working. Thanks for the info about the images though.

 

I don't really see any difference between your script and mine so can't figure out why yours works and mine doesn't.

 

Do you know what this error means?

 

'..' is not recognized as an internal or external command, operable program or batch file

Link to comment
Share on other sites

PHP does not compile scripts. How are you getting the following error:

'..' is not recognized as an internal or external command, operable program or batch file

This is not an error from PHP itself but a Windows command error. Which suggests to me that you are not actually passing the code to the PHP interpreter.

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.