dmikester1 Posted October 5, 2007 Share Posted October 5, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/ Share on other sites More sharing options...
MmmVomit Posted October 5, 2007 Share Posted October 5, 2007 Can you post some code? Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-362971 Share on other sites More sharing options...
MadTechie Posted October 5, 2007 Share Posted October 5, 2007 sounds like your using an Online Control Panel!, did you create it ? Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-362981 Share on other sites More sharing options...
dmikester1 Posted October 6, 2007 Author Share Posted October 6, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-363635 Share on other sites More sharing options...
dmikester1 Posted October 11, 2007 Author Share Posted October 11, 2007 Anyone know how to save a file in somewhere other than the home directory on my site? Thanks Mike Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366625 Share on other sites More sharing options...
MadTechie Posted October 11, 2007 Share Posted October 11, 2007 erm.. put the path you wish to save to in the fopen! ? Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366629 Share on other sites More sharing options...
Cagecrawler Posted October 11, 2007 Share Posted October 11, 2007 Where is $cwd being set? Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366630 Share on other sites More sharing options...
dmikester1 Posted October 11, 2007 Author Share Posted October 11, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366637 Share on other sites More sharing options...
MadTechie Posted October 11, 2007 Share Posted October 11, 2007 Where is $cwd being set? erm.. read the code, thats the question other will ask (because they don't have all your files!!) Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366640 Share on other sites More sharing options...
dmikester1 Posted October 11, 2007 Author Share Posted October 11, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366643 Share on other sites More sharing options...
MadTechie Posted October 11, 2007 Share Posted October 11, 2007 okay.. so erm.. put the path you wish to save to in the fopen! ? Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366646 Share on other sites More sharing options...
dmikester1 Posted October 11, 2007 Author Share Posted October 11, 2007 Well, since $cwd is my current working directory because of this line: $cwd = $cwd."/".$filedir."/"; and I'm setting $file to be in that directory with this line: $file = $cwd.$file; , I thought I was already doing that. Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366649 Share on other sites More sharing options...
Cagecrawler Posted October 11, 2007 Share Posted October 11, 2007 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"> Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366652 Share on other sites More sharing options...
MadTechie Posted October 11, 2007 Share Posted October 11, 2007 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, Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366656 Share on other sites More sharing options...
dmikester1 Posted October 11, 2007 Author Share Posted October 11, 2007 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/" Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366658 Share on other sites More sharing options...
MadTechie Posted October 11, 2007 Share Posted October 11, 2007 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, Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366660 Share on other sites More sharing options...
dmikester1 Posted October 11, 2007 Author Share Posted October 11, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366668 Share on other sites More sharing options...
dmikester1 Posted October 11, 2007 Author Share Posted October 11, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-366674 Share on other sites More sharing options...
MadTechie Posted October 11, 2007 Share Posted October 11, 2007 your check for a file before it exists.. if (is_writable($file)) it would probably be better to check that its a valid path and do you have permission to write to it ! if (is_dir($cwd) && is_writable($cwd) ) Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-367003 Share on other sites More sharing options...
dmikester1 Posted October 11, 2007 Author Share Posted October 11, 2007 Awwww, that is it! Thank you very much MadTechie! Quote Link to comment https://forums.phpfreaks.com/topic/72032-solved-saving-in-different-directory/#findComment-367437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.