deerox
deerox

Reputation: 1055

uploading image to image folder with unique name

This is my image uploading page. what i want is lil bit edition. I want image name look like (folder) image/userid_time.jpg

so i want to upload image to image folder,

and i want all image have new name or we can say unique name.

any adition in this please.

<?php // upload2.php
echo <<<_END
<html><head><title>PHP Form Upload</title></head><body>
<form method='post' action='upload2.php' enctype='multipart/form-data'>
Select a JPG, GIF, PNG or TIF File:
<input type='file' name='filename' size='10' />
<input type='submit' value='Upload' /></form>
_END;
if ($_FILES) {
    $name = $_FILES['filename']['name'];
    switch ($_FILES['filename']['type']) {
        case 'image/jpeg':
            $ext = 'jpg';
            break;
        case 'image/gif':
            $ext = 'gif';
            break;
        case 'image/png':
            $ext = 'png';
            break;
        case 'image/tiff':
            $ext = 'tif';
            break;
        default:
            $ext = '';
            break;
    }
    if ($ext) {
        $n = "image.$ext";
        move_uploaded_file($_FILES['filename']['tmp_name'], $n);
        echo "Uploaded image '$name' as '$n':<br />";
        echo "<img src='$n' />";
    } else
        echo "'$name' is not an accepted image file";
} else
    echo "No image has been uploaded";
echo "</body></html>";
?>

Thanks :)

Upvotes: 0

Views: 667

Answers (4)

vkhavin
vkhavin

Reputation: 111

Assuming you can get the user id into a variable called $userId, you can change the $n variable as was previously suggested.

$timestamp = time();
$n = "image/$userId_$timestamp.$ext";

When you execute the move_uploaded_file() method, the file will be moved into the image directory relative to the location of your upload script. You might want to make sure that the image folder already exists, and is writable.

Upvotes: 1

reza ostadi
reza ostadi

Reputation: 197

you can use uploadify because:

  • Multiple File Uploads
  • Drag and Drop
  • Real-Time Progress Indicators
  • Custom Upload Restrictions
  • Extreme Customization

and ...

...$verifyToken = md5('unique_salt' . $_POST['timestamp']);...

see demo

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Just change $n. Since you're always setting it to "image.$ext" you will always get the same filename.

Instead, you will need to name it how you want it. Something like $userid."_".time().".".$ext, assuming the user's ID is in $userid.

Upvotes: 1

purpletree
purpletree

Reputation: 1943

You can have the file name and then you can append it with uniqid("{$filename}_"). So the output would be something like filename_4b3403665fea6 and then you can add the extension :)

Docs here: http://uk.php.net/manual/en/function.uniqid.php

EDIT: I apologise, I mis-read your question.

Upvotes: 1

Related Questions