hunter
hunter

Reputation: 1101

change the image name in php

hello i have the following code to change the image name of upload picture

$TARGET_PATH="pics/";
$TARGET_PATH =$TARGET_PATH . basename( $_FILES['photo']).'.jpg';
while (file_exists($TARGET_PATH))
    {
$TARGET_PATH =$TARGET_PATH . basename( $_FILES['photo']) . uniqid() . '.jpg';
}

This change the name of image or file but dont sho the extention it just show dot(.) at end of name i.e 1=>file.
2=>filefile9534803.

Upvotes: 0

Views: 1311

Answers (1)

deceze
deceze

Reputation: 522099

// $original = '/tmp/file.jpg';

$dir = 'foo' . DIRECTORY_SEPARATOR;
$file = pathinfo($original);

do {
    $target = $dir . $file['filename'] . uniqid() . '.' . $file['extension'];
} while (file_exists($target));

echo $target; // foo/file4b3403665fea6.jpg

Upvotes: 1

Related Questions