Writing temp files - image

I am currently trying to display the sharepoint thumbnail in a picturebox when i click a button on the form. What appears to be happening is the file is locked and will not let me replace the file or anything. I even created a counter so the file name is always different. When I run the first time everything works, after that I believe it cant write over the file. Am I doing something wrong is there a better method??
$User=GET-ADUser $UserName –properties thumbnailphoto
$Filename='C:\Support\Export'+$Counterz+'.jpg'
#$img = $Filename.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
[System.Io.File]::WriteAllBytes($Filename, $User.Thumbnailphoto)
$Picture = (get-item ($Filename))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox.Width = $img.Size.Width
$pictureBox.Height = $img.Size.Height
$pictureBox.Image = $img
$picturebox.dispose($Filename)
Remove-Item $Filename

You should be able to do this without creating a temporary file.
Just create $img like:
$img = [System.Drawing.Image]::FromStream([System.IO.MemoryStream]::new($User.thumbnailPhoto))
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
Don't forget to remove the form from memory after closing with $form.Dispose()
If you insist on using a temporary file, then be aware that the $img object keeps a reference to the file untill it is disposed of.
Something like:
# get a temporary file name
$Filename = [System.IO.Path]::GetTempFileName()
[System.IO.File]::WriteAllBytes($Filename, $User.thumbnailPhoto)
# get an Image object using the data from the temporary file
$img = [System.Drawing.Image]::FromFile($Filename)
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
$form.Controls.Add($pictureBox)
$form.ShowDialog()
# here, when all is done and the form is no longer needed, you can
# get rid of the $img object that still has a reference to the
# temporary file and then delete that file.
$img.Dispose()
Remove-Item $Filename
# clean up the form aswell
$form.Dispose()

Related

How to get the uploaded image name from the store method

When I store an image in Laravel by doing:
$path = $request->file('myImage')->store('public/src/');
It returns the full path, but how do I get only the filename it was given?
This is an example of the returned path:
public/src/ltX4COwEmvxVqX4Lol81qfJZuPTrQO6S2jsicuyp.png
Here, you can try this one.
$fileNameWithExt = $request->file('myImage')->getClientOriginalName();
$fileNameWithExt = str_replace(" ", "_", $fileNameWithExt);
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$filename = preg_replace("/[^a-zA-Z0-9\s]/", "", $filename);
$filename = urlencode($filename);
$extension = $request->file('myImage')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$path = $request->file('myImage')->storeAs('public/src/',$fileNameToStore);
return $fileNameToStore;
You will get your stored filename in $fileNameToStore.
Also, all the spaces will be replaced with "_" and you will get your stored filename with current time attached with it, which will help you differentiate between two files with the same name.
Since $path returns the full path to the saved file, it contains its generated name.
You have just to parse this string :
$extension = explode('/', $path);
$filename = end($extension)
which will give you ltX4COwEmvxVqX4Lol81qfJZuPTrQO6S2jsicuyp.png
In Laravel, the store() method generates the name dynamically .. so you can't get it from the store() method.
But you can use storeAs() method. Basically the store() method is calling the storeAs() method. So:
$path = $request->file('myImage')->store('public/src');
What Laravel is doing is calling ->storeAs('public/src', $request->file('myImage')->hashName()); .. you see the hashName() method? that is what generates the name.
So you can call hashName() first and know your name before the storing happens .. here is an example:
$uploadFile = $request->file('myImage');
$file_name = $uploadFile->hashName();
$path = $uploadFile->storeAs('public/src', $file_name);
Now you have $file_name and $path.
See:
https://laravel.com/docs/6.x/filesystem#file-uploads .. Specifying A File Name
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Http/UploadedFile.php#L33
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Http/FileHelpers.php#L42

How can I upload image using Storage::put on the laravel?

My code to upload image like this :
$file = $file->move($path, $fileName);
The code works
But I want to change it using Storage::put like this reference :
https://laravel.com/docs/5.6/filesystem#storing-files
I try like this :
Storage::put($fileName, $path);
It does not works
I'm confused, where I must put $file on the code
How can I solve this problem?
Update :
$file = file of image
$path = storage_path('/app/public/product/')
$fileName = chelsea.jpg
So I want to save the file with name chelsea.jpg on the /app/public/product/
Easy Method
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
This will automatically store the files in your default configuration.
This is another example
Storage::put($fileName, $path);
Hope this helps

How do I set the individual upload file names?

I have a Laravel 5.3 app that has a form which users can upload multiple files using multiple file fields. The form work in that the files can be uploaded and moed to the destinationPath as I expect but I can't seem to change each of the files 'filename' values. It keeps saving the filename value as the php**.tmp.
Here is the foreach in my controller;
$files = $request->files;
foreach($files as $file){
$destinationPath = 'images/forms'; // upload path
$filename = $file->getClientOriginalName(); // get image name
$file->move($destinationPath, $filename); // uploading file to given path
$file->filename = $filename;
}
If I dd($filename) and dd($file->filename) within the foreach I do get the value (original name) I am looking for but if I dd($files) outside that foreach, the filename is set as the temp php value.
What am I missing? Thanks.
EDIT
The file object looks like this;
-test: false
-originalName: "sample_header_1280.png"
-mimeType: "image/png"
-size: 51038
-error: 0
path: "C:\xampp\tmp"
filename: "php7240.tmp"
basename: "php7240.tmp"
pathname: "C:\xampp\tmp\php7240.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\php7240.tmp"
I am trying to save the originalName to the db but it seems to default to saving the filename.
Turns out using a foreach for Input::file is not he approach here. If uploading multiple files from the same field - then you'd use a foreach to loop, move and save.
To upload files from multiple file inputs on the same form all you need to do is treat each input individually - as you might with any other form.
In my example I did this in my controller;
$data['image1'] = Input::file('image1')->getClientOriginalName();
Input::file('image1')->move($destinationPath, $data['image1']);
$data['image2'] = Input::file('image2')->getClientOriginalName();
Input::file('image2')->move($destinationPath, $data['image2']);
Not sure this is the best approach (there's always another way) but it worked for me.

How to compare age of local file with file on FTP server and download if remote copy is newer in PowerShell

I'm in the process of writing a PowerShell script to help in the process of setting up new PC's for my work. This will hopefully be used by more than just me so I'm trying to think of everything.
I have offline installers (java, flash, reader, etc) saved on our FTP server that the script downloads if a local copy hasn't already been saved in the Apps directory that gets created. Periodically the files on the FTP server will get updated as new versions of the programs are released. I want the script to have an option of checking for newer versions of the installers in case someone likes to carry around the local copies and forgets to check the server every now and then. It also will need to work in Windows 7 without any need to import additional modules unless there's an easy way to do that on multiple PC's at a time. I know about the import command, but the experiences I've had needed me to copy the module files into multiple places on the PC before it'd work.
Right now I haven't had much luck finding any solutions. I've found code that checks for modified dates on local files, or files on a local server, but nothing that deals with FTP other than uploading\downloading files.
Here's the last thing I tried. I tried a combination of what I found for local files with FTP. Didn't work too well.
I'm new to PowerShell, but I've been pretty good at piecing this whole thing together so far. However, this idea is becoming troublesome.
Thank you for the help.
$ftpsite = "ftp://ftpsite.com/folder/"
$firefox = (Get-Item $dir\Apps\install_firefox.exe).LastWriteTime.toString("MM/dd/yyyy")
if ($firefoxftp = (Get-ChildItem $ftpsite/install_firefox.exe | Where{$_.LastWriteTime -gt $firefox})) {
$File = "$dir\Apps\install_firefox.exe"
$ftp = "ftp://ftpsite.com/folder/install_firefox.exe"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
$webclient.DownloadFile($uri, $File)
}
UPDATE:
Here's what I have after Martin's help. It kind of works. It downloads the file from FTP, but it's not comparing the remote and local correctly. The remote file returns 20150709140505 and the local file returns 07/09/2015 2:05:05 PM. How do I format one to look like the other before the comparison, and is "-gt" the correct comparison to use?
Thanks!
function update {
$ftprequest = [System.Net.FtpWebRequest]::Create("ftp://ftpsite.com/Script_Apps/install_firefox.exe")
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::GetDateTimestamp
$response = $ftprequest.GetResponse().StatusDescription
$tokens = $response.Split(" ")
$code = $tokens[0]
$localfile = (Get-Item "$dir\Apps\install_firefox.exe").LastWriteTimeUtc
if ($tokens -gt $localfile) {
write-host "Updating Firefox Installer..."
$File = "$dir\Apps\install_firefox.exe"
$ftp = "ftp://ftpsite.com/Script_Apps/install_firefox.exe"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
$webclient.DownloadFile($uri, $File)
"Updated Firefox" >> $global:logfile
mainmenu
}
else {
Write-Host "Local Copy is Newer."
sleep 3
mainmenu
}
}
UPDATE 2:
Seems to be working! Here's the code. Thanks for the help!
function update {
$ftprequest = [System.Net.FtpWebRequest]::Create("ftp://ftpserver.com/Script_Apps/install_firefox.exe")
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::GetDateTimestamp
$response = $ftprequest.GetResponse().StatusDescription
$tokens = $response.Split(" ")
$code = $tokens[0]
$localtime = (Get-Item "$dir\Apps\install_firefox.exe").LastWriteTimeUtc
if ($code -eq 213) {
$tokens = $tokens[1]
$localtime = "{0:yyyymmddHHmmss}" -f [datetime]$localtime
}
if ($tokens -gt $localtime) {
write-host "Updating Firefox Installer..."
$File = "$dir\Apps\install_firefox.exe"
$ftp = "ftp://ftpserver.com/Script_Apps/install_firefox.exe"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
$webclient.DownloadFile($uri, $File)
"Updated Firefox" >> $global:logfile
mainmenu
}
else {
Write-Host "Local Copy is Newer."
sleep 3
mainmenu
}
}
You cannot use the WebClient class to check remote file timestamp.
You can use the FtpWebRequest class with its GetDateTimestamp FTP "method" and parse the UTC timestamp string it returns. The format is specified by RFC 3659 to be YYYYMMDDHHMMSS[.sss].
That would work only if the FTP server supports MDTM command that the method uses under the cover (most servers do, but not all).
$url = "ftp://ftpsite.com/folder/install_firefox.exe"
$ftprequest = [System.Net.FtpWebRequest]::Create($url)
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::GetDateTimestamp
$response = $ftprequest.GetResponse().StatusDescription
$tokens = $response.Split(" ")
$code = $tokens[0]
if ($code -eq 213)
{
Write-Host "Timestamp is" $tokens[1]
}
else
{
Write-Host "Error" $response
}
It would output something like:
Timestamp is 20150709065036
Now you parse it, and compare against a UTC timestamp of a local file:
(Get-Item "install_firefox.exe").LastWriteTimeUtc
Or save yourself some time and use an FTP library/tool that can do this for you.
For example with WinSCP .NET assembly, you can synchronize whole remote folder with installers with a local copy with one call to the Session.SynchronizeDirectories. Or your can limit the synchronization to a single file only.
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
$sessionOptions.HostName = "ftpsite.com"
$session = New-Object WinSCP.Session
# Connect
$session.Open($sessionOptions)
$transferOptions = New-Object WinSCP.TransferOptions
# Synchronize only this one file.
# If you remove the file mask, all files in the folder are synchronized:
$transferOptions.FileMask = "install_firefox.exe"
$session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, "$dir\Apps", "/folder",
$False, $False, [WinSCP.SynchronizationCriteria]::Time,
$transferOptions).Check()
To use the assembly, just extract a contents of .NET assembly package to your script folder. No other installation is needed.
The assembly supports not only the MDTM, but also other alternative methods to retrieve the timestamp.
See also a related Powershell example that shows both the above code and other techniques.
(I'm the author of WinSCP)

Saving Intervention Image In Owners Folder in Laravel 5

I can change my code to save the uploaded image in the public dir but not when I want to their uploaded image in a folder as their company's name. For example of what works:
/public/company_img/<filename>.jpg
If the user's company name is Foo, I want this when they save save their uploaded image:
/public/company_img/foo/<filename>.jpg
This is in my controller:
$image = Input::file('company_logo');
$filename = $image->getClientOriginalName();
$path = public_path('company_img/' . Auth::user()->company_name . '/' . $filename);
// I am saying to create the dir if it's not there.
File::exists($path) or File::makeDirectory($path); // this seems to be the issue
// saving the file
Image::make($image->getRealPath())->resize('280', '200')->save($path);
Just looking at that you can easily see what it's doing. My logs shows nothing and the browser goes blank after I hit the update button. Any ideas
File::exists($path) or File::makeDirectory($path);
This line does not make sense, as you check if a file exists and if not you want to attempt to create a folder ( in your $path variable you saved a path to a file not to a directory )
I would do something like that:
// directory name relative to public_path()
$dir = public_path("company_img/username"); // set your own directory name there
$filename = "test.jpg"; // get your own filename here
$path = $dir."/".$filename;
// check if $folder is a directory
if( ! \File::isDirectory($dir) ) {
// Params:
// $dir = name of new directory
//
// 493 = $mode of mkdir() function that is used file File::makeDirectory (493 is used by default in \File::makeDirectory
//
// true -> this says, that folders are created recursively here! Example:
// you want to create a directory in company_img/username and the folder company_img does not
// exist. This function will fail without setting the 3rd param to true
// http://php.net/mkdir is used by this function
\File::makeDirectory($dir, 493, true);
}
// now save your image to your $path
But i really can't say your behaviour has something to do with that... Without error messages, we can only guess.

Resources