I did a loading script in PHP. How to avoid overwriting files?

I did a loading image script using a function move_uploaded_file. This function seems to overwrite any existing file with a new one. So, I need to check if the target location has a file. If so, I need to add something to the file name (before the extension so that the file name is still valid), so the file name is unique. I would like the changes to be minimal, and not something like adding a date and time if possible.

How can I do this with PHP?

+3
source share
7 answers

When uploading files, I will almost always rename them. Typically, there will be some kind of database entry for this file. I use an identifier to guarantee the uniqueness of the file. Sometimes I even store what was in the original client file name, but I will never store it or a temporary name, because there is no guarantee that the information will be good, that your OS will support it or that it is unique (that is your problem) .

So just rename it to some scheme of your own project. That is my advice.

, file_exists() , , , - , - , . .

+15

file_exists() , true ( * nix, , ), . is_file().

, , - , :

$path = "/path/to/file/" . $file; // Assuming $file is an empty value, if something failed for example
if ( true === file_exists($path) ) { echo "This returns true"; }
if ( true === is_file($path) ) { echo "You will not read this"; }

, is_file(), file_exists().

+1

.

+1

, , incomingfile :

<input type="file" id="incomingfile" name="incomingfile" />

, , "" . . , .

$new_depured_filename = strtolower(preg_replace('/[^a-zA-Z0-9_ -.]/s', '_', $_FILES["incomingfile"]["name"]));
copy($_FILES["incomingfile"]["tmp_name"], 'my_temp_directory/'.$new_depured_filename);

, , , , , . , myimage.jpg, , myimage__000.jpg. , myimage__001.jpg , .

$i=0; // A counter for the tail to append to the filename
$new_filename = $new_depured_filename;
$new_filepath='myfiles/music/'.$new_filename;
while(file_exists($new_filepath)) {
    $tail = str_pad((string) $i, 3, "0", STR_PAD_LEFT); // Converts the integer in $i to a     string of 3 characters with left zero fill.
    $fileinfos = pathinfo($new_filepath); // Gathers some infos about the file
    if($i>0) { // If we aren't at the first while cycle (where you have the filename without any added strings) then delete the tail (like "__000") from the filename to add another one later (otherwise you'd have filenames like myfile__000__001__002__003.jpg)
        $previous_tail = str_pad((string) $i-1, 3, "0", STR_PAD_LEFT);
        $new_filename = str_replace('__'.$previous_tail,"",$new_filename);
    }
    $new_filename = str_replace('.'.$fileinfos['extension'],"",$new_filename); // Deletes the extension
    $new_filename = $new_filename.'__'.$tail.'.'.$fileinfos['extension']; // Append our tail and the extension
    $new_filepath = 'myfiles/music/'.$new_filename; // Crea il nuovo percorso
    $i++;
}

copy('my_temp_directory/'.$new_depured_filename, $new_filepath); // Finally we copy the file to its destination directory

unlink('my_temp_directory/'.$new_depured_filename); // and delete the temporary one

Functions used:
strtolower
preg_replace
copy
file_exists
str_pad
pathinfo
str_replace
unlink

+1
source

To check if a file exists, you can use the function file_exists.

To cut out the file name, you can use the function pathinfo.

0
source

I use

$file_name = time() . "_" . $uploaded_file_name;
0
source

Source: https://habr.com/ru/post/1708045/


All Articles