How to move files from local filessytem to cloud filesystem In laravel 5 - laravel-5.3

I have files in my local storage public/images/users and I want to move these files to cloud storage AWS S3 using the Laravel filesystem.
Route::get('upload-local-files-to-s3', function() {
$documentFiles = Storage::disk('public')->files('images\\users\\');
foreach ($documentFiles as $documentFile){
$filesystem = new Filesystem();
$contents = $filesystem->get($documentFile);
Storage::disk('s3')->put('documents\\'. $documentFile, $contents)
echo $documentFile . ' Uploaded. '. '<br>';
}
});
This code uploads the files to S3 but it creates new folders inside the documents/images/users while I want to upload files inside the documents folder.
And how can I create File Object from above $documentFile variable name?
Because I want to use these following functions from the file object.
getClientOriginalExtension()
getClientMimeType()
getFilename()
getClientOriginalName()

Related

liip imagine_filter in Symfony Controller on non public path

Is there any way to use liip imagine_filter without copying the image source to a public path?
I can not see how resolvers/loaders have to be set up to load images from a non public file location and store them likewise.
I defined a watermark filter with a watermark image placed outside public path - which works without problems. But ONLY applied on images placed IN public path.
I am on Symfony 5 and "liip/imagine-bundle": "^2.6"
I have the same problem as you and i found this solution, of course is a little workaround but it works very well.
So I copy the file from $remoteWatermak into the server. if the $localWatermark is setted I check if the file exist.
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
if($localWatermark){
$filesystem = new Filesystem();
if(!$filesystem->exists($localWatermark)){
$contents = file_get_contents($remoteWatermark,false,
stream_context_create($arrContextOptions)); // get file
file_put_contents($localWatermark , $contents);
}
}else{
$contents = file_get_contents($remoteWatermark,false,
stream_context_create($arrContextOptions)); // get file
$file = "uploads/watermarks/" . uniqid();
file_put_contents($file , $contents);
$localWatermark = $file;
}

Where do you store user uploaded content within a Symfony 4 application?

I have a section within my site where the user can upload their own profile pictures which is stored in the output directory and tracked in the database like so:
$form = $this->createForm(ProfileUpdateForm::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$user = $this->getUser();
$firstname = $form->get('firstname')->getData();
$lastname = $form->get('lastname')->getData();
$picture = $form->get('profilepicture')->getData();
if($picture == null)
{
$user
->setFirstName($firstname)
->setLastName($lastname);
}
else
{
$originalFilename = pathinfo($picture->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$safeFilename = strtolower(str_replace(' ', '', $originalFilename));
$newFilename = $safeFilename.'-'.uniqid().'.'.$picture->guessExtension();
try {
$picture->move(
'build/images/user_profiles/',
$newFilename
);
} catch (FileException $e) {
$this->addFlash("error", "Something happened with the file upload, try again.");
return $this->redirect($request->getUri());
}
// updates the 'picture' property to store the image file name
// instead of its contents
$user
->setProfilePicture($newFilename)
->setFirstName($firstname)
->setLastName($lastname);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash("success", "Your profile was updated!");
return $this->redirectToRoute('account');
}
return $this->render('account/profile.html.twig', [
'profileform' => $form->createView()
]);
That issue I've found is that every time I compile my (local) project, the image is then deleted (because the public/build directory gets built by deleting and creating again).
If I'm not mistaken, isn't that how deployments work too? And if so, is that the right way to upload an image? What's the right way of going about this?
I'm not sure why, but your public/ directory shouldn't be deleted.
If you're using Webpack Encore, then public/build/ content is deleted and created again when you compile assets. But not public/ itself.
For uploads, we create public/upload/ directory.
Then, most of the time, we set some globals, which allow us to save the file name only.
Globals for Twig in config/packages/twig.yaml which "root" will be in your public/ directory
twig:
globals:
app_ul_avatar: '/upload/avatar/'
app_ul_document: '/upload/document/'
And globals for your controllers, repositories, etc in config/services.yaml
parameters:
app_ul_avatar: '%kernel.root_dir%/../public/upload/avatar/'
app_ul_document: '%kernel.root_dir%/../public/upload/document/'
It's handy because, as I just said, you only get to save the file name in the database.
Which mean that, if you got a public/upload/img/ folder, and want to also generates thumbnails, you can then create public/upload/img/thumbnail/ and nothing will change in your database, nor do you have to save an extra path.
Just create a new global app_ul_img_thumbnail, and you're set.
Then all you have to do is call your globals when you need them, and contact with the file name:
In Twig:
{{ app_ul_avatar~dbResult.filename }}
Or in Controller:
$this->getParameter('app_ul_avatar').$dbResult->getFilename();

batch file to delete all files in a folder with special character

i need a batch file that can delete all files in a folder that have a dash ('-') in them. Note they are image files so they can be be png or jpeg, etc.. any ideas?
Thanks!
<?php
$dir = "img";//folder url which consist file to delete. you can use wordpress file location using wordpress function
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if (strpos($filename,'_') !== false)//check whether it conist of '_'
{
unlink('img/'.$filename);//remove file from folder
}
}
?>
Edit the following code to your preferences.
#echo off
cd /d "LocationOfItems"
echo Deleting all items with "-"...
del *-*
echo Completed.
pause
Wildcards allow for variation on either end of the string. File type is optional. In this example, it doesn't need to be specified because they are different (jpg, png, etc).

WordPress meta box with custom upload loaction

I need a way to upload files to a custom location in stead of the upload folder from wordpress itself.
I googled somewhat but probably using the wrong words.
Can somebody give me a push in the right direction?
Note that I don't ask for the complete script to do this.
I want to understand what I am doing rather than just copy and paste :-)
M.
You can write to any location that the web server process has write permissions. If you want to save an uploaded file to a different path under /wp-content/uploads you could use the wp_upload_dir() function.
if (isset($_FILES['file']) && (!empty($_FILES['file']['tmp_name']))){
if ($_FILES['file']['error'] == UPLOAD_ERR_OK) { // no errors
// get upload directory
$upload_dir = wp_upload_dir();
// append the custom dir (and trailing slash!)
$my_upload_dir = $upload_dir . "my-dir/";
// get the filename of the upload (you might want to filter this)
$filename = $_FILES['file']['name'];
// move the file to your custom directory location and change permissions
#move_uploaded_file( $_FILES['file']['tmp_name'], $my_upload_dir . $filename );
#chmod( $my_upload_dir . $filename, 0644 );
}
}

grunt-init copyAndProcess corrupting png files

I've just started using grunt-init. I have everything working, except I'm finding that any images/*.png files in my template get corrupted in transit to the destination folder.
I suspect that the init.copyAndProcess function is corrupting them (they open in Gimp from the template folder but not the destination folder).
How can I do a copy instead of copyAndProcess for a subset of the files in my template? Preferably using a pattern like 'images/**' to identify the files.
You actually can use filesToCopy, since you pass a noprocess hash, telling to grunt-init the files it should not process.
Refer to http://gruntjs.com/project-scaffolding#copying-files (grunt-init documentation)
and an example at line 73 of this commit https://github.com/gruntjs/grunt-init-jquery/blob/ecf070d7469d610441458111bc05cd543ee5bbc0/template.js.
Well, I would have preferred something more concise, similar to what was already in the template.js:
var files = init.filesToCopy( props );
init.copyAndProcess( files, props );
But this works:
var src_path = init.srcpath( 'images/' );
var dest_path = init.destpath( ) + '/images/';
// Copy the images folder this way to prevent corrupting the files
grunt.file.recurse( src_path, function( abspath, rootdir, subdir, filename ) {
if ( subdir == undefined ) {
var dest = dest_path + filename;
} else {
var dest = dest_path + subdir + '/' + filename;
}
grunt.file.copy( abspath, dest );
});

Resources