Image rename upon upload of duplicate file name?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1332

    #1

    Tech Image rename upon upload of duplicate file name?

    // Check if file already exists
    if (file_exists($target_file)) {
    //echo "Sorry, file already exists.";
    //$uploadOk = 0;
    rename($target_file, 'your_new_filename');
    }
    This is working however, its not creating a clickable image when I browse the image upload directory, could anyone point me in the right direction about how to get it to rename AND be a valid clickable image please when viewed in the directory itself?

    I've tried this, which i thought would work but didnt:

    [quote]
    $imageFileType rename(2.'$imageFileType');
    [quote]

    Extreme Link List - v1.0
  • Klen
    • Aug 2006
    • 32235

    #2
    Originally posted by Publisher Bucks
    This is working however, its not creating a clickable image when I browse the image upload directory, could anyone point me in the right direction about how to get it to rename AND be a valid clickable image please when viewed in the directory itself?

    I've tried this, which i thought would work but didnt:



    No clue, i would simply rather exec linux command instead using PHP replacement for it, as those replacement would not work always, plus linux commands give far more options. Could be some permission problem.

    Comment

    • ZTT
      Confirmed User
      • Apr 2019
      • 659

      #3
      1. Rename all files on upload for security, control and so you don't get dupes in the first place.

      2. No idea about the clickable thing, since as usual you don't explain things properly.

      How are you viewing it, in a directory index listing, hrefs, img src, something else?
      __________________

      Comment

      • Publisher Bucks
        Confirmed User
        • Oct 2018
        • 1332

        #4
        Originally posted by ZTT
        1. Rename all files on upload for security, control and so you don't get dupes in the first place.
        Thats not an option unfortunately.

        2. No idea about the clickable thing, since as usual you don't explain things properly.
        Its a directory index list, you click on a filename, it doesnt work as there is no file extension on the renamed image, my bad.
        Extreme Link List - v1.0

        Comment

        • k0nr4d
          Confirmed User
          • Aug 2006
          • 9231

          #5
          What about just prepending uniqid() to the start or end of the existing filename?
          Mechanical Bunny Media
          Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

          Comment

          • ZTT
            Confirmed User
            • Apr 2019
            • 659

            #6
            Originally posted by Publisher Bucks
            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>
            __________________

            Comment

            Working...