View Single Post
Old 02-07-2022, 02:11 AM  
ZTT
Confirmed User
 
ZTT's Avatar
 
Industry Role:
Join Date: Apr 2019
Posts: 657
Quote:
Originally Posted by Publisher Bucks View Post
Thats not an option unfortunately.
Why? If you're having to rename files if they exist, it can't be that important for them to strictly match the original name.

The files are effectively renamed when you upload anyway, when they're in the tmp directory, and when you move them from tmp you're 'renaming' them back to the original name, so instead you just pick a new unique name that you control that won't clash.

Anyway, if there's still some reason you can't do that, here's a basic code illustrating renaming of an uploaded file only if it exists. Save it as a PHP file and try it. Obviously change $up_dir to whatever you're using.


Code:
<?php
    $up_dir = 'upload/';
    $tmp_name = $_FILES["image"]["tmp_name"];
    $name = basename($_FILES["image"]["name"]);

    if (file_exists($up_dir.$name)) {
        // rename file however you like, eg:
        $name = $name.'_'.time();
    }

    move_uploaded_file($tmp_name, $up_dir.$name);
?>

<form enctype="multipart/form-data" action="" method="post">
    <input type="file" name="image">
    <input type="submit" value="Upload">
</form>
__________________
__________________
ZTT is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote