oliverj777 Posted July 5, 2010 Share Posted July 5, 2010 Basically, when the user submits a page, I want a file to be copied from one directory to another (into the users personal folder on the server) and stay there. But every time the user submits the page, the file is copied again (which I don't want) hence why I placed a if(file_exists). But it seems that even when the file does exist, the PHP still reads on after the 'else'. Anyway - here is my code. Sure would appreciate someone helping me <?php $filename = 'users/$session->username'; $source = "users/progress_clean.php"; $destination = "users/$session->username/progress.php"; if (file_exists($filename)) { echo "File Exists -- Display Included: <br />"; include ("users/$session->username/progress.php"); } // If the file is found - then I DON'T want the rest of the code to continue (it will over write) else { mkdir("users/$session->username/", 0700); if(copy($source, $destination)) { echo "File copied successfully. <br />", "\n"; include ("users/$session->username/progress.php"); } else { echo "The specified file could not be copied. Please try again.", "\n"; }} ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 5, 2010 Share Posted July 5, 2010 I see one glaring problem: $filename = 'users/$session->username'; You are defining the variable with single quotes which means variables int he string are not interpreted. Quote Link to comment Share on other sites More sharing options...
oliverj777 Posted July 5, 2010 Author Share Posted July 5, 2010 OMG -- i feel soooo stupid, cheers bud It works now Quote Link to comment Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 OMG -- i feel soooo stupid, cheers bud It works now Would help if you makred the thread as solved Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 5, 2010 Share Posted July 5, 2010 By the way, that logic is still flawed. It assumes that if the folder exists that the file exists as well. Why not just check for the file directly instead of just the folder? Plus, you are already defining the $destination and then hard-coding the file path in the IF condition. Way too much work and prone to errors. Use your variables. Also, you have two scenarios where the destination file is displayed. Are you planning on really echo'ing that other text? If not, then the logic can be rewoirked to be more efficient. <?php $source_file = "users/progress_clean.php"; $destination_folder = "users/{$session->username}"; $destination_file = "{$destination_folder}/progress.php"; if (!file_exists($destination_file)) { //Attempt to copy source file if(!mkdir($destination_folder, 0700)) { echo "The specified folder could not be created. Please try again.\n"; } elseif(!copy($source_file, $destination_file)) { echo "The specified file could not be copied. Please try again.\n"; } } //Only include if already present or it copy was successful if (file_exists($destination_file)) { include ($destination_file); } ?> 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.