jeff5656 Posted February 10, 2009 Share Posted February 10, 2009 If a user uploads a file with an extension .html, I want to change it to .htm. SO... if they upload a file called "february.html" I want to rename it to "february.htm. If the file already has htm, then no need to rename the file. How do I modify this code? <?$uploadLocation = "schedules/";?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="fileForm" id="fileForm" enctype="multipart/form-data"> <input name="upfile" type="file" size="36"> <input class="text" type="submit" name="submitBtn" value="Upload Schedule"> </form> <?php if (isset($_POST['submitBtn'])){ ?> <div id="caption">RESULT</div> <div id="icon2"> </div> <div id="result"> <table width="100%"> <?php $target_path = $uploadLocation . basename( $_FILES['upfile']['name']); if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)) { echo "The file: ". basename( $_FILES['upfile']['name']). " has been uploaded!"; } else{ echo "There was an error uploading the file, please try again!"; } ?> Thanks! Link to comment https://forums.phpfreaks.com/topic/144665-change-uploaded-file-extension/ Share on other sites More sharing options...
sdi126 Posted February 10, 2009 Share Posted February 10, 2009 <?$uploadLocation = "schedules/";?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="fileForm" id="fileForm" enctype="multipart/form-data"> <input name="upfile" type="file" size="36"> <input class="text" type="submit" name="submitBtn" value="Upload Schedule"> </form> <?php if (isset($_POST['submitBtn'])){ ?> <div id="caption">RESULT</div> <div id="icon2"> </div> <div id="result"> <table width="100%"> <?php $basename = basename($_FILES['upfile']['name']); $filename = (substr_count(".html",$basename) > 0 ) ? $ substr_replace($basename,"",-1); : $basename; $target_path = $uploadLocation .$filename; if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)) { echo "The file: ". basename( $_FILES['upfile']['name']). " has been uploaded!"; } else{ echo "There was an error uploading the file, please try again!"; } ?> Link to comment https://forums.phpfreaks.com/topic/144665-change-uploaded-file-extension/#findComment-759106 Share on other sites More sharing options...
jeff5656 Posted February 10, 2009 Author Share Posted February 10, 2009 Hi I got an unexpected t-string, but I thought it would be better to do a new thread (hope that's what I should do...): http://www.phpfreaks.com/forums/index.php/topic,238137.msg1109254.html#msg1109254 Link to comment https://forums.phpfreaks.com/topic/144665-change-uploaded-file-extension/#findComment-759219 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.