How to check whether a file contains "." (dot) operator - laravel

I have a code which asks user to upload a file. The file may be audio or image or anything. I asks user to enter file name. If he Enter file name my code adds extension to it. It is working fine. But if user enters extension say audio.mp3 then it saves as audio.mp3.mp3. So I have to check if user entered name contains dot then it should not take extension.
I used pregmatch but it is not working.
My code
$splitOptions = explode(',',$request->input('mediaName'));
$fileExtension = pathinfo($file[$i]->getClientOriginalName(),PATHINFO_EXTENSION);
$checkExtension = explode('.',$request->input('mediaName'));
if(preg_match("/[.]/", $checkExtension)){
$mediaName = $splitOptions[$i];
}
else
{
$mediaName = $splitOptions[$i]."_$fileExtension";
}

Please use laravel helper
$value = str_contains('This is my name', 'my');
// true

Related

How do create a file from ascii, then create a link to download the file with Laravel

I'm using a shipping api, that has a method that spits out a pdf in this format (ascii, I believe):
%PDF-1.3\n1 0 obj\n
<<\n
/Type /Pages\n
/Count 1\n
many lines removed here
startxref\n
982\n
%%EOF\n
How do I..
Convert this code into a downloadable .pdf file?
Create a link to download the file?
Note - I do not need to store the file.
ADDENDUM
So, this is moving the correct file to the correct place, but is giving me an error "Call to a member function move() on string".
$pdf_raw = $this->create_label2($data->label_url);
$filename = 'label'.auth()->user()->id.'.pdf';
file_put_contents($filename, $pdf_raw);
$filename->move(public_path().'/img/pdf', $filename);
How can that be?
Here is the solution:
use Illuminate\Support\Facades\Storage;
$pdf_raw = $this->create_label2($data->label_url); //this is raw ascii data
$filename = 'label'.auth()->user()->id.'.pdf'; //can be any name .pdf
Storage::disk('pdf')->put($filename,$pdf_raw); //creates file and moves to correct path
$pdf_link = "/mypath/$filename"; //url to file

Delete file in Laravel using Google Drive API

I'm using this command to upload the file to Google Drive (store function in controller):
$request->anexo->storeAs('1tD*******************-6', $file_name, 'google');
And this to get the link:
$link = Storage::disk('google')->url('1tD*******************-6/' . $file_name);
This is working fine. But when i try to delete the file i can't find it in the destroy function in the same controller. I've tried the commands below and all of them return false:
$att = OrdemAttachment::findOrFail($id);
$link = Storage::disk('google')->url('1tD*******************-6/' . $att->file_name);
// dd($link);
$exists = Storage::disk('google')->has('1tD*******************-6/' . $att->file_name);
// dd($exists);
$delete = Storage::disk('google')->delete('1tD*******************-6/' . $att->file_name);
// dd($delete);
Is there another command that must be used instead of these?
I figured it out, I was trying to use the file name and even tried to use the google drive link. But what worked was using the file id (you can find the id inside the link). So, for example, if the link to the file is https://drive.google.com/uc?id=1AgF87s1lw9TSsRhVOpGNJ2K6wq3A8FzD&export=media, to delete the file in google drive with the id would go like this:
$delete = Storage::disk('google')->delete('1tD*******************-6/1AgF87s1lw9TSsRhVOpGNJ2K6wq3A8FzD');
1tD*******************-6 is the subfolder id and 1AgF87s1lw9TSsRhVOpGNJ2K6wq3A8FzD is the id of the file extracted from the link.

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.

User input in a matlab GUI

Hey all,
I am building a gui in wich there is an edit box, waiting for the user to write a name.
Currently I force the user to give a legitimate name with this code :
NewPNUName = get(handles.nameOfNewPNU, 'String');
if ( isempty(NewPNUName) ||...
strcmp(NewPNUName,'Enter the name for the new PNU') )
errordlg('Please enter a name for the new PNU.');
elseif (~ischar(NewPNUName(1)))
errordlg('The PNU name should start with a letter.');
else
handles.NewPNUName = NewPNUName;
end
if (~isempty(handles.NewPNUName))
% Do all the things needed if there is a legit name
end
What it does is nothing if the user didn't write a legit name.
What I want it to do is to make a popup with an edit box, asking the user to input the wanted name again, until it is a legitimate name.
Thanks for the help!
EDIT:
following #woodchips advice I corrected my code to the foloowing:
NewPNUName = get(handles.nameOfNewPNU, 'String');
ValidName = ~isempty(NewPNUName) && isletter(NewPNUName(1)) &&...
~strcmp(NewPNUName,'Enter the name for the new PNU');
while (~ValidName)
if ( isempty(NewPNUName) ||...
strcmp(NewPNUName,'Enter the name for the new PNU') )
NewPNUName = char(inputdlg('Please enter a name for the new PNU.','No name entered'));
elseif (~isletter(NewPNUName(1)))
NewPNUName = char(inputdlg('The name of the new PNU should start with a letter. Please enter a new name',...
'Invalid name entered'));
else
allConds = 'are met'
end
ValidName = ~isempty(NewPNUName) && isletter(NewPNUName(1)) &&...
~strcmp(NewPNUName,'Enter the name for the new PNU');
end
So, put a while loop around a block of code, that generates an inputdlg box. Set the condition on the while loop to be that the result is a valid one.

exportDocument() 'destination folder does not exist' error

I'm trying to make a script in photoshop that will modify some layers and than export them as a PNG image. I've copied the following code from another place:
function SavePNG(saveFile){
var pngOpts = new ExportOptionsSaveForWeb;
pngOpts.format = SaveDocumentType.PNG
pngOpts.PNG8 = false;
pngOpts.transparency = true;
pngOpts.interlaced = true;
pngOpts.quality = 100;
activeDocument.exportDocument(saveFile,ExportType.SAVEFORWEB,pngOpts);
}
The function export the active document of photoshop to the file specified by the saveFile parameter.
It's working fine with simple paths like "C:\images\result.png" but when trying with different paths like "~/Desktop/" or paths with some special characters the file isn't exported, and a "destination folder does not exist" error message appears.
Any idea how can I solve it?
Well, I'm not sure why this is occur but you could try the following modification:
function SavePNG(saveFile){
var tmpFile = "./tmp.png";
tmpFile = new File(tmpFile);
var pngOpts = new ExportOptionsSaveForWeb;
pngOpts.format = SaveDocumentType.PNG
pngOpts.PNG8 = false;
pngOpts.transparency = true;
pngOpts.interlaced = true;
pngOpts.quality = 100;
activeDocument.exportDocument(tmpFile,ExportType.SAVEFORWEB,pngOpts);
tmpFile.rename (saveFile);
tmpFile.changePath(saveFile);
}
it'll export the file into a temporary file and then rename & move it to the requested path, should solve the path problem.
exportDocument expects a full file name, not a folder path.
This works:
activeDocument.exportDocument(new File("~/foo/foo.png"), ExportType.SAVEFORWEB, pngOpts);
This doesn't work and gives the 'destination folder does not exist' error message:
activeDocument.exportDocument(new File("~/foo/"), ExportType.SAVEFORWEB, pngOpts);
For people having this error and not using photoshop-script.
The error might be unbound to the destination folder, but occurs because the folder, which was used for the export step, is deleted. So either
recreate the folder, which was used during recording, or
recreate the export step

Resources