How can I upload a file and overwrite old file?

Issues related to hyperlinks and web site navigation.
Forum rules
PLEASE READ THE FORUM RULES BEFORE YOU POST:
viewtopic.php?f=12&t=1901

MUST READ:
http://www.wysiwygwebbuilder.com/links.html
Post Reply
jmibarcodes
 
 
Posts: 7
Joined: Sun Jul 20, 2014 8:52 pm

How can I upload a file and overwrite old file?

Post by jmibarcodes » Tue Oct 09, 2018 2:20 pm

I am using the instructions from here:

https://wysiwygwebbuilder.com/forum/viewtopic.php?t=635

I could not figure out how to get the built-in form processor to do what I wanted so I am pasting in the code from the original post. The code in the original post renames any old file of the same name so that it is not deleted or overwritten. However, I actually WANT to overwrite the old file. What can I do to this code to force it to overwrite the old file instead of renaming it?

Is it just as simple as commenting out the 4 lines beginning with "$t = 1" or would that break something else?

Here is the code that I started from:

Code: Select all

<?php
$uploaddir = './uploads/';
$allowed   = array('jpg','jpeg','gif','pdf');
$max_size  = 1024 * 1024;

# No edits beyond this line 
if (isset($_FILES['userfile'])) {
  foreach ($_FILES['userfile']['error'] as $i => $error) {
    if ($error == 0 && $_FILES['userfile']['size'][$i] <= $max_size) {
      $file_ext  = pathinfo($_FILES['userfile']['name'][$i],PATHINFO_EXTENSION);
      $file_name = basename($_FILES['userfile']['name'][$i],'.'.$file_ext);
      if (in_array(strtolower($file_ext),$allowed)) {
        $new_base = $_FILES['userfile']['name'][$i];
        $t = 1;
        while (file_exists($uploaddir.$new_base)) {
          $new_base = $file_name.'['.$t.'].'.$file_ext;
          $t++;
        }
        if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$uploaddir.$new_base)) {
          chmod($uploaddir.$new_base, 0644);
          echo 'Successful upload: '.$_FILES['userfile']['name'][$i].'<br>'."\n";
        }
      }
    }
  }
}
?>

User avatar
Pablo
 
Posts: 14573
Joined: Tue Mar 28, 2006 12:00 pm
Location: Europe
Contact:

Re: How can I upload a file and overwrite old file?

Post by Pablo » Tue Oct 09, 2018 2:50 pm

I think you will have the remove this code:

Code: Select all

$t = 1;
        while (file_exists($uploaddir.$new_base)) {
          $new_base = $file_name.'['.$t.'].'.$file_ext;
          $t++;
        }
Please note that we cannot provide programming support.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests