Upload multiple files at the same time without a duplicate in Laravel - php

I use this code to upload multiple files together in Laravel. However, all name files get duplicated. Please guide me.
if (is_array($files) || is_object($files)) {
foreach ($files as $file) {
$name = time().'.'.$file->getClientOriginalExtension();
$file->move(public_path('uploadmusic'), $name);
PostPhoto::create([
'post_id' => $post->id,
'filename' => $name
]);
}
}
1568314601.png
1568314601.png
1568314601.png

The precision of time() is only a second - not enough time to make time() report a different value when assigning $name in your loop for each file.
Append something else, like a call to \Illuminate\Support\Str::random() to get each name to be unique.
Depending on requirements, you might consider omitting the timestamp from the filename altogether and use something like md_file() instead.
$name = implode('.', [
md5_file($file->getPathname()),
$file->getClientOriginalExtension()
]);
This can also help keep duplicate files off of your storage device.

Related

uniqid() not generating unique values within a Foreach loop - PHP

I have a foreach loop that is processing image uploads. When I add multiple images they are given a filename that consists of a unique id and a time stamp using $filebase = uniqid() . '_' . time();
When the images are processed they have the same image name, and I don't understand why the foreach loop isn't giving unique file names?
I have the $temp variable assigned to the index of the foreach loop with the following line: $temp = $_FILES['standard-upload-files']['tmp_name'][$index]; so I don't understand why it doesn't increment a new value?
The images are being moved correctly to the destination folder (albeit with the same name) so I know it isn't an issue with my HTML form, or the initial foreach loop.
if(isset($_POST['submit-images'])) {
foreach($_FILES['standard-upload-files']['name'] as $index => $filename ) {
if($_FILES['standard-upload-files']['error'][$index] === 0 ) {
$temp = $_FILES['standard-upload-files']['tmp_name'][$index];
$filebase = uniqid() . '_' . time();
move_uploaded_file($temp, "images-download/{$filebase}");
}
} // main foreach
} // isset($_POST)
I see, my first guess is using uniqid. Uniqid is generated based on the current time in microseconds which could result in the same values. As the documentation mentions, this function does not guarantee the uniqueness of the return value.
Since most systems adjust the system clock by NTP or like, system time is changed constantly. Therefore, it is possible that this function does not return a unique ID for the process/thread. Use more_entropy to increase the likelihood of uniqueness.
A [not so reliable] solution would be setting the more_entropy to true:
uniqid('', true)
but again, remember this approach is not reliable.
So use another method for generating your unique IDs (UUID for example as explained in the comments here.)

laravel | How to replace a field in form's request?

I am using laravel 5.4 and I'm trying to replace the imagePath field in my request (renaming the uploaded image).
explanation:
when the form is submitted the request field(request->imagePath) contains the temporary location of the uploaded image, I am moving that tmp image to a dir while changing its name ($name). so now as the request->imagePath still has old tmp image location I want to change request->imagePath value to have the new location and then create the user.
Like so
if($request->hasFile('imagePath'))
{
$file = Input::file('imagePath');
$name = $request->name. '-'.$request->mobile_no.'.'.$file->getClientOriginalExtension();
echo $name."<br>";
//tried this didn't work
//$request->imagePath = $name;
$file->move(public_path().'/images/collectors', $name);
$request->merge(array('imagePath' => $name));
echo $request->imagePath."<br>";
}
But Its not working, Here is the output
mahela-7829899075.jpg
C:\xampp\tmp\php286A.tmp
Please Help
I believe merge() is the correct method, it will merge the provided array with the existing array in the ParameterBag.
However, you're accessing the input variables incorrectly. Try using $request->input('PARAMETER_NAME') instead...
Therefore, your code should look like this:
if ($request->hasFile('imagePath')) {
$file = Input::file('imagePath');
$name = "{$request->input('name')}-{$request->input('mobile_no')}.{$file->getClientOriginalExtension()}";
$file->move(public_path('/images/collectors'), $name);
$request->merge(['imagePath' => $name]);
echo $request->input('imagePath')."<br>";
}
Note: You can also pass your path into public_path() and it will concatenate it for you.
References
Retrieving Input:
https://laravel.com/docs/5.4/requests#retrieving-input
$request->merge():
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Http/Request.php#L269
public_path: https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/helpers.php#L635

Random image from directory with no repeats?

I am successfully able to get random images from my 'uploads' directory with my code but the issue is that it has multiple images repeat. I will reload the page and the same image will show 2 - 15 times without changing. I thought about setting a cookie for the previous image but the execution of how to do this is frying my brain. I'll post what I have here, any help would be great.
$files = glob($dir . '/*.*');
$file = array_rand($files);
$filename = $files[$file];
$search = array_search($_COOKIE['prev'], $files);
if ($_COOKIE['prev'] == $filename) {
unset($files[$search]);
$filename = $files[$file];
setcookie('prev', $filename);
}
Similar to slicks answer, but a little more simple on the session front:
Instead of using array_rand to randomise the array, you can use a custom process that reorders based on just a rand:
$files = array_values(glob($dir . '/*.*'));
$randomFiles = array();
while(count($files) > 0) {
$randomIndex = rand(0, count($files) - 1);
$randomFiles[] = $files[$randomIndex];
unset($files[$randomIndex]);
$files = array_values($files);
}
This is useful because you can seed the rand function, meaning it will always generate the same random numbers. Just add (before you randomise the array):
if($_COOKIE['key']) {
$microtime = $_COOKIE['key'];
else {
$microtime = microtime();
setcookie('key', $microtime);
}
srand($microtime);
This does means that someone can manipulate the order of the images by manipulating the cookie, but if you're okay with that this this should work.
So you want to have no repeats per request? Use session. Best way to avoid repetitions is to have two arrays (buckets). First one will contains all available elements that your will pick from. The second array will be empty for now.
Then start picking items from first array and move them from 1st array to the second. (Remove and array_push to the second). Do this in a loop. On the next iteration first array won't have the element you picked already so you will avoid duplicates.
In general. Move items from a bucket to a bucket and you're done. Additionally you can store your results in session instead of cookies? Server side storage is better for that kind of things.

Upload Multiple Files with FuelPHP

I currently have a script that allows me to upload 1 file to my server. It works great.
Here is a portion of the code I am using to do this:
// Custom configuration for this upload
$config = array(
'path' => DOCROOT.DS.'foldername/tomove/your/images',
'randomize' => true,
'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
);
Upload::process($config);
// if a valid file is passed than the function will save, or if its not empty
if (Upload::is_valid())
{
// save them according to the config
Upload::save();
//if you want to save to tha database lets grab the file name
$value = Upload::get_files();
$article->filename = $value[0]['saved_as'];
}
I was now wondering, how do I loop through multiple files and upload these to my server?
I'm guessing using a foreach loop but I'm a little out of my depth with this I'm afraid.
Ultimately, I plan to store these filenames in a separate table on my database.
Many thanks for any help with this.
You already have the result in your code.
You already store it
$value = Upload::get_files();
so
$value = Upload::get_files();
foreach($value as $files) {
print_r($files);
}
And you will get everything what you need

Create Files Automatically using PHP script

I have a project that needs to create files using the fwrite in php. What I want to do is to make it generic, I want to make each file unique and dont overwrite on the others.
I am creating a project that will record the text from a php form and save it as html, so I want to output to have generated-file1.html and generated-file2.html, etc.. Thank you.
This will give you a count of the number of html files in a given directory
$filecount = count(glob("/Path/to/your/files/*.html"));
and then your new filename will be something like:
$generated_file_name = "generated-file".($filecount+1).".html";
and then fwrite using $generated_file_name
Although I've had to do a similar thing recently and used uniq instead. Like this:
$generated_file_name = md5(uniqid(mt_rand(), true)).".html";
I would suggest using the time as the first part of the filename (as that should then result in files being listed in chronological/alphabetic order, and then borrow from #TomcatExodus to improve the chances of the filename being unique (incase of two submissions being simultaneous).
<?php
$data = $_POST;
$md5 = md5( $data );
$time = time();
$filename_prefix = 'generated_file';
$filename_extn = 'htm';
$filename = $filename_prefix.'-'.$time.'-'.$md5.'.'.$filename_extn;
if( file_exists( $filename ) ){
# EXTREMELY UNLIKELY, unless two forms with the same content and at the same time are submitted
$filename = $filename_prefix.'-'.$time.'-'.$md5.'-'.uniqid().'.'.$filename_extn;
# IMPROBABLE that this will clash now...
}
if( file_exists( $filename ) ){
# Handle the Error Condition
}else{
file_put_contents( $filename , 'Whatever the File Content Should Be...' );
}
This would produce filenames like:
generated_file-1300080525-46ea0d5b246d2841744c26f72a86fc29.htm
generated_file-1300092315-5d350416626ab6bd2868aa84fe10f70c.htm
generated_file-1300109456-77eae508ae79df1ba5e2b2ada645e2ee.htm
If you want to make absolutely sure that you will not overwrite an existing file you could append a uniqid() to the filename. If you want it to be sequential you'll have to read existing files from your filesystem and calculate the next increment which can result in an IO overhead.
I'd go with the uniqid() method :)
If your implementation should result in unique form results every time (therefore unique files) you could hash form data into a filename, giving you unique paths, as well as the opportunity to quickly sort out duplicates;
// capture all posted form data into an array
// validate and sanitize as necessary
$data = $_POST;
// hash data for filename
$fname = md5(serialize($data));
$fpath = 'path/to/dir/' . $fname . '.html';
if(!file_exists($fpath)){
//write data to $fpath
}
Do something like this:
$i = 0;
while (file_exists("file-".$i.".html")) {
$i++;
}
$file = fopen("file-".$i.".html");

Categories