Jump to content

Recommended Posts

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>";
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/205104-if-else/
Share on other sites

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>";
    }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/205104-if-else/#findComment-1073643
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.