Trekker182 Posted June 17, 2010 Share Posted June 17, 2010 Hello everyone, I'm working on my third php program and need some help. I am trying to upload a file to the server and then give the user the option to rename or delete it. I've got the code working to rename it, however, if the rename fails I want to be able to tell them. However, as soon as they click on the link and the rename.php page loads in the browser, the text in the else statement automatically shows! After they enter the new name of the file and click change, it renames the file and displays the correct message under the if. I've compared the if...else format for php to class examples as well as online and am at a loss. Can anyone help or offer some suggestions? I could just get rid of the else but that wouldn't be good programming practice plus it would still bug me. I've also taken the else out of brackets since its just one line and it still did the same thing. Here is my code from the rename page...thanks. <?php //when user submits form, cd to upload directory, rename the file and echo that it completed or failed if ($_POST[action] == "change") { $newName = $_POST[newName]; chdir($upload_directory); rename ($userfile_name,$newName); echo "The file has been renamed.<br>"; //destroy the session so the user can go back and upload another file session_destroy(); } else { echo "The file rename failed.<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 17, 2010 Share Posted June 17, 2010 I could just get rid of the else but that wouldn't be good programming practice ... No it wouldn't; it is perfectly acceptable to have an IF statement without an ELSE. In this case, you don't want an ELSE unless there is a problem with the rename process. But, your ELSE is currently running whenever the POST value isn't set to a certain value. Instead you should be checking the response of rename() to see if the process succeeds or not. <?php //when user submits form, cd to upload directory, rename the file and echo that it completed or failed if ($_POST['action']=="change") { $newName = trim($_POST['newName']); chdir($upload_directory); $success = rename($userfile_name, $newName); if($success) { echo "The file has been renamed.<br>"; //destroy the session so the user can go back and upload another file session_destroy(); } else { echo "The file rename failed.<br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
Trekker182 Posted June 18, 2010 Author Share Posted June 18, 2010 Thanks a lot that did it! I sometimes forget that I have to be more specific when doing if else statements. Thanks again. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 18, 2010 Share Posted June 18, 2010 I just realized I added code that was unnecessary. Instead of this: $success = rename($userfile_name, $newName); if($success) I should have just done this: if(rename($userfile_name, $newName)) Quote Link to comment 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.