GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech Image rename upon upload of duplicate file name? (https://gfy.com/showthread.php?t=1352009)

Publisher Bucks 02-06-2022 11:24 AM

Image rename upon upload of duplicate file name?
 
Quote:

// 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]

:helpme

Klen 02-06-2022 12:01 PM

Quote:

Originally Posted by Publisher Bucks (Post 22962200)
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:



:helpme

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.

ZTT 02-06-2022 12:19 PM

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?

Publisher Bucks 02-06-2022 05:14 PM

Quote:

Originally Posted by ZTT (Post 22962219)
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.

Quote:

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.

k0nr4d 02-07-2022 01:56 AM

What about just prepending uniqid() to the start or end of the existing filename?

ZTT 02-07-2022 02:11 AM

Quote:

Originally Posted by Publisher Bucks (Post 22962343)
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>



All times are GMT -7. The time now is 12:33 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123