Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique identifier for filenames #194

Open
dev opened this issue May 17, 2017 · 1 comment
Open

Unique identifier for filenames #194

dev opened this issue May 17, 2017 · 1 comment

Comments

@dev
Copy link

dev commented May 17, 2017

Hi,
Is it possible to add a unique identifier to filenames when trying to save them?
If someone sent me "image.jpeg", and I get a new email with "image.jpeg", the old image.jpeg would be overwritten.
This is my code:

                    $name = $attachment->getFileName();
                    $data = $attachment->getData();
                    $mime = $attachment->getMimeType();
                    $size = $attachment->getSize();
                    $saved = $attachment->saveToDirectory($path);

I would like it to be for example: image37ad2d8e.jpeg

@narvinwilliams
Copy link

narvinwilliams commented May 26, 2017

Here's a quick solution, some of these codes can be merged together. However, I wrote it out for simplicity when reading.

  // Information about the file and where to save it
$saveToDirectory = getcwd() . "\\attachments\\"; // Get's the current directory and folder attachments
$attachmentName = $attachment->getFileName();
$pathToSave = $saveToDirectory . $attachmentName;

$attachment->saveToDirectory($saveToDirectory); // Save the file here

// Renaming the file to a unqiue id name
$fileExt = (new SplFileInfo($fileName))->getExtension(); // Get the file extention
$newFileName =  uniqid() . "." . $fileExt; // Create a name for the file

$renameFilePath = $tmpdir . $newFileName;// Path to save the file

rename($pathToSave, $renameFilePath);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants