Jump to content

[SOLVED] saving in different directory


dmikester1

Recommended Posts

One my site I have a file/folder directory listing.  I have my home directory called "home".  I have a link to "create new file" which opens up a popup to create a file, name it, and save it to the current working directory.  That popup page is pulled from the "home" directory.  Now when I click on a folder from the "home" directory it goes in and lists the current files.  When i click on "create new file", and save out a file it still saves out to the "home" directory.  I am correctly passing the current directory name because I echo it out in the "create new file" window.  Can someone give me basic instructions on how to get it to save the file to the new directory instead of "home"?

Thanks!

Mike

Link to comment
Share on other sites

I am working on an online control panel.  I found a nice php script posted online here: http://www.evoluted.net/community/code/directorylisting.php.  I added a "create new file" link to the script and that link pops up a smaller popup called "MikesFileEditor.php" and am currently stuck on this problem.

Here is some of the code on the "MikesFileEditor.php" page:

<div id="center">Name of file: <input type="text" name="filename" id="filename" /><br /><div id="hidden"></div>
<input type="submit" name="save" id="save" value="Save Changes" onclick="return checkIfEmpty(document.getElementById('filename').value)">
<?php
if (isset($_POST['save'])) {  //if the "save" button was clicked
$file=$_POST['filename'];
$file = $cwd.$file;
if (is_writable($file)) {
$data_to_save = $_POST["updatedfile"];
$data_to_save = eregi_replace("<END-TA-DO-NOT-EDIT>", "</textarea>", $data_to_save);

$file2save = fopen($file, "w+");
if (fwrite($file2save,$data_to_save))  {
echo "<meta http-equiv='refresh' content='0;URL=MikesFileEditor.php'>";
echo "file saved!";
//Close file
fclose($file2save);
}
else {
//If write fails show failure page
echo "file not saved!";
//Close file
fclose($file2save);
}
}
else {
echo "not writable!";
}
}
}
?>
</input>
</div>

Link to comment
Share on other sites

Here is more of the code:

<?php
$filedir = $_GET["value"];
$cwd = getcwd();
if($filedir == "") {
$filedir = ".";
}
else {
$cwd = $cwd."/".$filedir."/";
$cwd = substr($cwd, 0, -1);
}
echo $cwd;

if(is_readable($filedir)) {
?>
<h1>Currently in <span class="red"><?php echo $cwd; ?></span></h1>
<form action="<?=$PHP_SELF?>" method="post">
<div align="center">
<textarea rows="30" cols="80" style="border: 1px solid #666666;" name="updatedfile">
</textarea></div>
<br />

<div id="center">Name of file: <input type="text" name="filename" id="filename" /><br /><div id="hidden"></div>
<input type="submit" name="save" id="save" value="Save Changes" onclick="return checkIfEmpty(document.getElementById('filename').value)">
<?php
if (isset($_POST['save'])) {  //if the "save" button was clicked
$file=$_POST['filename'];
$file = $cwd.$file;
chdir($cwd);
if (is_writable($file)) {
$data_to_save = $_POST["updatedfile"];
$data_to_save = eregi_replace("<END-TA-DO-NOT-EDIT>", "</textarea>", $data_to_save);

$file2save = fopen($file, "w+");
if (fwrite($file2save,$data_to_save))  {
//echo "<meta http-equiv='refresh' content='0;URL=MikesFileEditor.php'>";
echo "file saved!";
//Close file
fclose($file2save);
}
else {
//If write fails show failure page
echo "file not saved!";
//Close file
fclose($file2save);
}
}
else {
echo "not writable!";
}
}
}
?>

 

Thanks

Mike

Link to comment
Share on other sites

Ok, here is where $cwd is being set earlier in that file:

$cwd = getcwd();
if($filedir == "") {
$filedir = ".";
echo "in ".$cwd;
}
else {
$cwd = $cwd."/".$filedir."/";
$cwd = substr($cwd, 0, -1);
}

 

Then later I do a

$file = $cwd.$file; chdir($cwd);

and

$file2save = fopen($file, "w+");

 

Thanks

Mike

 

Link to comment
Share on other sites

When dealing with directories on the server, you need to use \ instead of /.

$cwd = $cwd."/".$filedir."/";

should be

$cwd = $cwd."\\".$filedir."\\";

 

Also,

<form action="<?=$PHP_SELF?>" method="post">

should be (unless your server supports short tags):

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

 

EDIT:

Your filedir isn't being passed to the second page.  You can pass it via a hidden input in your form:

<input type="hidden" name="filedir" value="<?php echo $filedir;?>">

or add it as a GET:

<form action="<?php echo $_SERVER['PHP_SELF']."?value=$filedir";?>" method="post">

Link to comment
Share on other sites

Okay let me make a few comments in your code

<?php
$filedir = $_GET["value"];
$cwd = getcwd();
if($filedir == "")
{
$filedir = ".";
}else{
$cwd = $cwd."/".$filedir."/"; // concat paths
$cwd = substr($cwd, 0, -1); // remove the last /
}
echo $cwd;

if(is_readable($filedir)) //check the get "value" is readable ??????
{
?>
<h1>Currently in <span class="red"><?php echo $cwd; ?></span></h1>
<form action="<?=$PHP_SELF?>" method="post">
<div align="center">
<textarea rows="30" cols="80" style="border: 1px solid #666666;" name="updatedfile">
</textarea></div>
<br />

<div id="center">Name of file: <input type="text" name="filename" id="filename" /><br /><div id="hidden"></div>
<input type="submit" name="save" id="save" value="Save Changes" onclick="return checkIfEmpty(document.getElementById('filename').value)">
<?php
if (isset($_POST['save']))
{  //if the "save" button was clicked
	$file=$_POST['filename'];
	$file = $cwd.$file;  //so now were missing the trailling / and appending a name!
	chdir($cwd);// this dir is unlikely yo exist!!
	if (is_writable($file))
	{
		$data_to_save = $_POST["updatedfile"];
		$data_to_save = eregi_replace("<END-TA-DO-NOT-EDIT>", "</textarea>", $data_to_save);

		$file2save = fopen($file, "w+");
		if (fwrite($file2save,$data_to_save))
		{
			//echo "<meta http-equiv='refresh' content='0;URL=MikesFileEditor.php'>";
			echo "file saved!";
			//Close file
			fclose($file2save);
		}else {
			//If write fails show failure page
			echo "file not saved!";
			//Close file
			fclose($file2save);
		}
	}else {
		echo "not writable!";
	}
}
}
?>

 

 

When dealing with directories on the server, you need to use \ instead of /.

$cwd = $cwd."/".$filedir."/";

should be

$cwd = $cwd."\\".$filedir."\\";

Erm.. WHAT! thats wrong..

infact you should use /

On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).

 

your thinking of ONLY windows servers,

Link to comment
Share on other sites

Thanks MadTechie.  Now the reason I have that line to remove the last slash is because when I didn't have it, I got this for output: "Currently in ....../http_site2007/images//"

I knew two slashes wasn't going to work, so I removed the last one and when I spit out the directory in the top of the window, it looks correct: "....../http_site2007/images/"

Link to comment
Share on other sites

 

When dealing with directories on the server, you need to use \ instead of /.

$cwd = $cwd."/".$filedir."/";

should be

$cwd = $cwd."\\".$filedir."\\";

Erm.. WHAT! thats wrong..

infact you should use /

On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).

 

your thinking of ONLY windows servers,

Link to comment
Share on other sites

 

When dealing with directories on the server, you need to use \ instead of /.

$cwd = $cwd."/".$filedir."/";

should be

$cwd = $cwd."\\".$filedir."\\";

Erm.. WHAT! thats wrong..

infact you should use /

On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).

 

your thinking of ONLY windows servers,

 

OK, I got it to not do two slashes and the slashes are going the right way.  Now when I hit save, the page refreshes, it echoes out "not writable!" and at the top where I am echoing the current directory, it switches to the home directory.  Any idea why this would be happening?

Link to comment
Share on other sites

Is it possible my javascript is tripping it up?

<input type="submit" name="save" id="save" value="Save Changes" onclick="return checkIfEmpty(document.getElementById('filename').value)">

 

checkifEmpty:

function checkIfEmpty(filename) {
var result = true;
var message = "";
if(filename == "") {
message = "Please type a name for the file(including extension)";
popupText(message);
result = false;
}
else {
message = filename;
popupText(result);
}
return result;
}

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.