Hi guys, I have a simple php script to upload file. I'm having some problem in creating confirmation before overwriting the file from new file upload.

Situation:
1. confirm() works if using file_exists BUT not overwrite the file or process the move_uploaded_file

assuming some tags are correct

uploadfile.php

<form enctype="multipart/form-data" action="addfile.php" method="POST" action="javascript:alert('success!');">
<td><label for="file">Choose File to Upload:</label></td>
        <td>
            <input type="file" name="upfile" id="upfile"> <br><br>
            <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
            <input type="hidden" name="fileuploaded">
        </td>
    </tr>
    <tr>
        <td></td>
        <td> 
            <input type="submit" name="submit" value="Add" class="pure-button pure-button-success" onclick="tellme();">
            <!-- <input type="submit" name="submit" value="Add" class="confirmLink"> -->
            <a href="index.php" ><button class="pure-button pure-button-secondary">Cancel</button></a>
</form>

addfile.php

<script type="text/javascript">
function tellme()
{
 alert("New File Added Successfully.");
 window.location = 'search.php';
}

function overwrite()
{
var r=confirm("Overwrite File?")
if (r==true)
  {
  alert("You pressed OK!")
  window.location = 'search.php';
  }
else
  {
  alert("You pressed Cancel!")
  window.location = 'new_file.php';
  }
}
</script>

if(!mysqli_query($con, $sql))
{
  die('Error ' . mysqli_error());
}

if (file_exists("files/" . $_FILES['upfile']['name']))
    {
        echo "<script type='text/javascript'>
            var answer = confirm(Overwrite? Y/N)
            if (answer)
            {
                <?php move_uploaded_file($_FILES['upfile']['tmp_name'],$target); ?>
            }
            </script>";
    }   
    else 
    {
        move_uploaded_file($_FILES['upfile']['tmp_name'],$target);
        echo "<script type='text/javascript'>tellme();</script>";

Thanks in advance! cheers!

Recommended Answers

All 5 Replies

Where do you define the variable $target?

@phorce

in addfile.php, thanks.

in addition to addfile.php

    $target = "files/";
    $target = $target . basename($_FILES['upfile']['name']);

    $currentDate = date("Y-m-d");
    $vtitle = $_POST['title'];
    $vfilename = ($_FILES['upfile']['name']);
    $vfiletype = $_FILES['upfile']['type'];
    $vfilesize = ($_FILES['upfile']['size'] / 1024);

    $con = mysqli_connect("localhost", "root", "pw", "db");
    if(mysqli_connect_errno())
    {
      echo "error connection" . mysqli_connect_error();
    }

    $sql = "INSERT INTO table (date,fld_title,fld_filename,fld_filetype,fld_filesize,fld_user) 
          VALUES ('$currentDate','$vtitle','$vfilename','$vfiletype','$vfilesize','$vuser')";

    if(!mysqli_query($con, $sql))
    {
      die('Error ' . mysqli_error());
    }

Here are the simplest code for addfile.php

<?php 
 $target = 'files/'.$_FILES['upfile']['name'];
if (file_exists("files/" . $_FILES['upfile']['name']))
    {
        echo "<script type='text/javascript'>
          var answer = confirm('Overwrite? Y/N');

        if (answer == true)
        {
            alert('You pressed OK!')
           var overwrite = ".move_uploaded_file($_FILES['upfile']['tmp_name'],$target)."
            if(overwrite == 1)
            {
                alert('File Overwrite successfully');
                window.location = 'search.php';
            }else alert('File Overwrite Failed'); window.location = 'new_file.php';
        }else alert('You pressed Cancel!'); window.location = 'new_file.php';
            </script>";
    }   
    else 
    {
         $ans = move_uploaded_file($_FILES['upfile']['tmp_name'],$target); 

        if($ans == 1)
        {
            echo "<script type='text/javascript'>alert('New File Added Successfully.');
 window.location = 'search.php';</script>";
        }else  echo "<script type='text/javascript'>alert('New File Added Failed.');</script>";

    }
?>

@stevie.whalen

thank you, i will try to check this! cheers!

hi stevie, i tried your script, it works fine, but if i cancel still it executes the move upload file or still inserting the file.
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.