Current location: Hot Scripts Forums » Programming Languages » PHP » rename function


rename function

Reply
  #1 (permalink)  
Old 05-30-07, 08:42 AM
pkcidstudio's Avatar
pkcidstudio pkcidstudio is offline
Coding Addict
 
Join Date: Nov 2005
Posts: 332
Thanks: 0
Thanked 0 Times in 0 Posts
rename function

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.
__________________
learning like everyone else
Reply With Quote
  #2 (permalink)  
Old 05-30-07, 08:55 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Post your code.
Reply With Quote
  #3 (permalink)  
Old 05-30-07, 08:58 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Edit: longer version of above ^^

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???
Reply With Quote
  #4 (permalink)  
Old 05-30-07, 09:06 AM
pkcidstudio's Avatar
pkcidstudio pkcidstudio is offline
Coding Addict
 
Join Date: Nov 2005
Posts: 332
Thanks: 0
Thanked 0 Times in 0 Posts
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;
}  
}
?>
__________________
learning like everyone else
Reply With Quote
  #5 (permalink)  
Old 05-30-07, 09:22 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
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???
Reply With Quote
  #6 (permalink)  
Old 05-30-07, 09:46 AM
pkcidstudio's Avatar
pkcidstudio pkcidstudio is offline
Coding Addict
 
Join Date: Nov 2005
Posts: 332
Thanks: 0
Thanked 0 Times in 0 Posts
Any chance that $_POST['document'] contains an entry for each file, even those that you are not modifying?
PHP Code:

$documentMoveName_size count($_POST['document']); 

= 147

PHP Code:

$document$_POST['document'][$i]; 

= 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


Attached Files
File Type: zip source.zip (16.7 KB, 35 views)
__________________
learning like everyone else
Reply With Quote
  #7 (permalink)  
Old 05-30-07, 11:25 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
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 -
PHP Code:

echo "i: $i, olddir: $oldDirectory, oldfolder: $oldSubfolder, olddoc: $oldDocument, newdir: $directory, newfolder: $subfolder, newdoc: $document<br />"
to make sure what every value is.

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
Reply With Quote
  #8 (permalink)  
Old 05-30-07, 12:05 PM
pkcidstudio's Avatar
pkcidstudio pkcidstudio is offline
Coding Addict
 
Join Date: Nov 2005
Posts: 332
Thanks: 0
Thanked 0 Times in 0 Posts
I went ahead and placed the echo command you provided, worked great. Only thing is it was ouputing exzactly correct. see example
Code:
i: 128, olddir: OperationManuals, oldfolder: Simplex, olddoc: 84A, 85A, 86A, 22B, A1022, 1017,24A, 2029.pdf, newdir: OperationManuals, newfolder: Simplex, newdoc: 84A, 85A, 86A, 22B, A1022, 1017,24A, 2029.pdf
i: 129, olddir: OperationManuals, oldfolder: Simplex, olddoc: CYLINDERS_OPERATIONS_MANUAL.pdf, newdir: OperationManuals, newfolder: Simplex, newdoc: CYLINDERS_OPERATIONS_MANUAL.pdf
Quote:
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.
__________________
learning like everyone else
Reply With Quote
  #9 (permalink)  
Old 05-30-07, 01:22 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
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???
Reply With Quote
  #10 (permalink)  
Old 05-30-07, 03:00 PM
pkcidstudio's Avatar
pkcidstudio pkcidstudio is offline
Coding Addict
 
Join Date: Nov 2005
Posts: 332
Thanks: 0
Thanked 0 Times in 0 Posts
That fixed it. Thank you so much MAB, that worked great!
__________________
learning like everyone else
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing XML dodotopia PHP 1 02-08-06 03:51 PM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
PHP Error Fairnie PHP 8 06-26-04 07:15 AM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 09:50 AM


All times are GMT -5. The time now is 06:03 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.