# Overwrite file on upload if file exists

I am using a multiupload component/plugin for Joomla/VM, i have already modified the code to prevent it rename files (as my product image names contain the product name and sku). However, if the file already exists in the images folder, then the file is suffixed with (1). I would like to overwrite the file if it has the same name, or deleted it before uploading. I think i have found the function in the component which does this, but i'm relatively new to this and i'm not sure what part of code i need to modify? Here is the function:

protected function handle_file_upload($uploaded_file,$name, $size,$type, $error,$index = null, $content_range = null) {$file = new stdClass();
$file->name =$this->get_file_name($name,$type, $index,$content_range);
$file->size =$this->fix_integer_overflow(intval($size));$file->type = $type; if ($this->validate($uploaded_file,$file, $error,$index)) {
$this->handle_form_data($file, $index);$upload_dir = $this->get_upload_path(); if (!is_dir($upload_dir)) {
mkdir($upload_dir,$this->options['mkdir_mode'], true);
}
$file_path =$this->get_upload_path($file->name);$append_file = $content_range && is_file($file_path) &&
$file->size >$this->get_file_size($file_path); if ($uploaded_file && is_uploaded_file($uploaded_file)) { // multipart/formdata uploads (POST method uploads) if ($append_file) {
file_put_contents(
$file_path, fopen($uploaded_file, 'r'),
FILE_APPEND
);
} else {
move_uploaded_file($uploaded_file,$file_path);
}
} else {
// Non-multipart uploads (PUT method support)
file_put_contents(
$file_path, fopen('php://input', 'r'),$append_file ? FILE_APPEND : 0
);
}
$file_size =$this->get_file_size($file_path,$append_file);
if ($file_size ===$file->size) {
$file->url =$this->get_download_url($file->name); list($img_width, $img_height) = @getimagesize($file_path);
if (is_int($img_width) && preg_match($this->options['inline_file_types'], $file->name)) {$this->handle_image_file($file_path,$file);
}
} else {
$file->size =$file_size;
if (!$content_range &&$this->options['discard_aborted_uploads']) {
unlink($file_path);$file->error = 'abort';
}
}
$this->set_additional_file_properties($file);
}
return $file; } $name contains the filename of the image to be uploaded. Any help would be most appreciated.

Best Regards Donna

Edit: Here are the 2 additional functions.

protected function get_upload_path($file_name = null,$version = null) {
$file_name =$file_name ? $file_name : ''; if (empty($version)) {
$version_path = ''; } else {$version_dir = @$this->options['image_versions'][$version]['upload_dir'];
if ($version_dir) { return$version_dir.$this->get_user_path().$file_name;
}
$version_path =$version.'/';
}
return $this->options['upload_dir'].$this->get_user_path()
.$version_path.$file_name;
}


and

 protected function validate($uploaded_file,$file, $error,$index) {
if ($error) {$file->error = $this->get_error_message($error);
return false;
}
$content_length =$this->fix_integer_overflow(intval(
$this->get_server_var('CONTENT_LENGTH') ));$post_max_size = $this->get_config_bytes(ini_get('post_max_size')); if ($post_max_size && ($content_length >$post_max_size)) {
$file->error =$this->get_error_message('post_max_size');
return false;
}
if (!preg_match($this->options['accept_file_types'],$file->name)) {
$file->error =$this->get_error_message('accept_file_types');
return false;
}
if ($uploaded_file && is_uploaded_file($uploaded_file)) {
$file_size =$this->get_file_size($uploaded_file); } else {$file_size = $content_length; } if ($this->options['max_file_size'] && (
$file_size >$this->options['max_file_size'] ||
$file->size >$this->options['max_file_size'])
) {
$file->error =$this->get_error_message('max_file_size');
return false;
}
if ($this->options['min_file_size'] &&$file_size < $this->options['min_file_size']) {$file->error = $this->get_error_message('min_file_size'); return false; } if (is_int($this->options['max_number_of_files']) && (
$this->count_file_objects() >=$this->options['max_number_of_files'])
) {
$file->error =$this->get_error_message('max_number_of_files');
return false;
}
list($img_width,$img_height) = @getimagesize($uploaded_file); if (is_int($img_width)) {
if ($this->options['max_width'] &&$img_width > $this->options['max_width']) {$file->error = $this->get_error_message('max_width'); return false; } if ($this->options['max_height'] && $img_height >$this->options['max_height']) {
$file->error =$this->get_error_message('max_height');
return false;
}
if ($this->options['min_width'] &&$img_width < $this->options['min_width']) {$file->error = $this->get_error_message('min_width'); return false; } if ($this->options['min_height'] && $img_height <$this->options['min_height']) {
$file->error =$this->get_error_message('min_height');
return false;
}
}
return true;
}


Edit 2: I have tried to add a file_exists() to the handle_file_upload function: //Check if file exists $exist_file =$file_path.$uploaded_file; if file_exists($exist_file) { unlink($exist_file); } But the files do not upload at all. I have spent hours on this and i really haven't a clue. • I have found something about setting the FILE_APPEND flag is this related?$append_file ? FILE_APPEND : 0 – Dtorr1981 Mar 17 '16 at 11:20
• I can't see anything in this code that check if the file already exists. However it foes call 2 other functions that you may want to check in the same file: get_upload_path and validate. Maybe see if the code for the check exists in either of these functions – Lodder Mar 17 '16 at 11:22
• @Looder I'll have a look, i have found this on the PHP site: If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set. FILE_APPEND is mentioned twice in this function, do you know how i can unset it? I'm not sure what that means? – Dtorr1981 Mar 17 '16 at 11:26
• I have included the additional functions – Dtorr1981 Mar 17 '16 at 11:34
• @Lodder i'f messed about with the FILE_APPEND and i don't think it is related to that, as if i remove it the file doesn't upload at all. I'll keep looking :) – Dtorr1981 Mar 17 '16 at 12:37

If I am interpreting what the developer did here correctly this may work for you. These changes should be made in the first code you posted.

jimport('joomla.filesystem.file');


Before this code:

$file = new stdClass();  And add this code: $append_file = false;


After this code:

$append_file =$content_range && is_file($file_path) &&$file->size > $this->get_file_size($file_path);


if(is_file($file_path)){ JFile::delete($file_path);
}


Before this:

move_uploaded_file($uploaded_file,$file_path);

• Thank you for your response Terry, it has been the most constructive so far. Unfortunately, it is still appending the (1) at the end. The developer is not responding to emails either so i'm really stuck, i'm honestly not sure why someone would want a random file name generated rather than the file name of the image, its not good seo. Anyway, i will add the full file code to the post and fingers crossed the community can find a solution. Thank you for your help :) – Dtorr1981 Mar 17 '16 at 20:09
• You may want to take a look at the handle_image_file() function as well. That may be where the number is being appended to the name. – Terry Carter Mar 17 '16 at 20:12
• I just posted a comment but unfortunately it does not appear here. The full code can be found at github.com/Dtorr1981/joomla.git upload.php...sorry if this isnt aloud on here. – Dtorr1981 Mar 17 '16 at 20:16
• Just for giggles try the steps I have outlined above now. They have changed a little bit. – Terry Carter Mar 17 '16 at 20:36
• I have edited the file, and this seems to break the code as the files do not upload at all. I have emailed the developer again, but if you have any more ideas i would be most appreciative. – Dtorr1981 Mar 18 '16 at 10:10