Jump to content

change uploaded file extension


jeff5656

Recommended Posts

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

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

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.