I am running php 4.2.4, and having trouble with the rename function. The function works great with the file or files I select but, then goes on to delete all the rest of the files that I do not alter. Any thoughts why this might be happening?
I am displaying a minimum of 50 files per page that the admin can alter.
If there is info in the phpinfo page that would help to solve this pease let me know.
You would need to post your code to get specific help with what it is doing and give an example of an old file name and a new file name you renamed it to, along with a sample of a few file names that it deleted.
This is most likely a logic problem in a loop. Something like - all the files got renamed into one, along with the intended one(s), leaving you with only the intended one(s).
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Mab, that makes a lot of sence now that you say that. below is my code for this function. Here is also a curve ball the exzact same code works on my practice server but not the live. thank you for the quick responces.
PHP Code:
<?php function documentMoveName_change() { //The table we are choosing to connect to $tbl_name = "table"; // find out how many records there are to update $documentMoveName_size = count($_POST['document']); // start a loop in order to update each record $i = 0; while ($i < $documentMoveName_size) { // define each variable $id = $_POST['id'][$i]; $document= $_POST['document'][$i]; $subfolder = $_POST['title'][$i]; $directory = $_POST['filetype'][$i]; $queryDocumentMove = "SELECT DISTINCT document, filetype, title FROM $tbl_name WHERE id='$id' LIMIT 1"; $resultDocumentMove = mysql_query($queryDocumentMove) or die("Invalid query: $queryDocumentMove"); while($row = mysql_fetch_array($resultDocumentMove)) { $oldDocument = $row['document']; $oldSubfolder = $row['title']; $oldDirectory= $row['filetype']; } rename("sys:apache2\htdocs\usa\docs/$oldDirectory/$oldSubfolder/$oldDocument", "sys:apache2\htdocs\usa\docs/$directory/$subfolder/$document"); ++$i; } } ?>
Any chance that $_POST['document'] contains an entry for each file, even those that you are not modifying?
Could you browse to your form, do a "view source", and copy and paste the actual HTML form code?
Beyond this, you would need to do some data debugging by displaying all the $_POST data to make sure it is what you expect and to display the actual value in variables that are getting used inside of the loops...
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
= only the first file that is in the list of files. Could you browse to your form, do a "view source", and copy and paste the actual HTML form code?
see attachment
Since this works on one server, that probably leaves operation system/data differences. If the data is known to be the same, that would leave operating system differences? Are they the same or different operating systems?
I played with your form and the php code a little. About the only thing I see is that it unconditionally renames all 147 files, even if nothing changed.
It is possible that the query is returning no rows, in which case the $old... variables will either be undefined or retain their last values. This could result in the rename syntax having a source file that is something like -
"sys:apache2\htdocs\usa\docs///" You would hope this would result in a error and the rename(...) would not modify the destination file. But this is what I think is deleting your files.
It is possible too that the rename() function contains a bug (that is perhaps version specific) that if you rename a file to itself, that the file is locked, the rename() fails, but the source file name gets deleted... Edit: or the destination file directory entry gets deleted.
I would comment out your rename(...) statement and put in something like this -
The solution I think is some logic to only call the rename() function if there is a difference. Compare the $old.... values with the new values and only do the rename if there are any differences and skip the rename if all three old and new values are the same.
Edit2: Have you checked your web server log for errors and/or turned on full error reporting to see if any errors are being generated?
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Last edited by mab; 05-30-07 at 11:48 AM.
Reason: add some
The solution I think is some logic to only call the rename() function if there is a difference. Compare the $old.... values with the new values and only do the rename if there are any differences and skip the rename if all three old and new values are the same.
Could you offer an example for me to see on doing this.
Quote:
Edit2: Have you checked your web server log for errors and/or turned on full error reporting to see if any errors are being generated?
no errors were being reported.
This leads me to believe it is a permisions difference between the two servers. Lets try that logic of yours and see what happens though.
Replace your rename() function call with the following, untested, but should work -
PHP Code:
if($document != $oldDocument || $subfolder != $oldSubfolder || $directory != $oldDirectory)
{
rename("sys:apache2\htdocs\usa\docs/$oldDirectory/$oldSubfolder/$oldDocument", "sys:apache2\htdocs\usa\docs/$directory/$subfolder/$document");
}
++$i; // keep the existing increment after the new closing }
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???