I want to know if you do the following in my code below how do you check if the file exists e.g
{
$file = Storage::disks('local')->get($file);
// then test if $file exists
}
You can check the existence of the file with the code below for your local storage,
$file_exists = Storage::disk('local')->exists($file);
You can read more about storage on Laravel Document at Laravel Filesystem.
Note: in the document you will check disk('s3'). It depends on what connection we are using defined in config/filesystem.php(the configuration file for the filesystem in laravel).
OR simply you can use check the condition by
if(Storage::disk('local')->exists($file)) {
}
It will also work for you.
There's a clear example in the docs, if you take a look.
$exists = Storage::disk('s3')->exists('file.jpg');
Related
I have some encrypted responses that I convert to a Zip file in my Laravel application. The function below downloads the API response, saves it as a Zip file, and then extracts it while I read the folder's contents. In my local environment, it works well. However, the Zip file is not getting saved to the storage folder on the live server. No error is being shown, only an empty JSON response. Please, what could be the cause?
public function downloadZipAndExtract($publication_id, $client_id)
{
/* We need to make the API call first */
$url = $this->lp_store."clients/$client_id/publications/$publication_id/file";
$file = makeSecureAPICall($url, 'raw');
// Get file path. If file already exist, just return
$path = public_path('storage/'.$publication_id);
if (!File::isDirectory($path)) {
Storage::put($publication_id.'.zip', $file);
// Zip the content
$localArchivePath = storage_path('app/'.$publication_id.'.zip');
$zip = new ZipArchive();
if (!$zip->open($localArchivePath)) {
abort(500, 'Problems experienced while reading file.');
}
// make directory with the publication_id
// then extract everything to the directory
Storage::makeDirectory($publication_id);
$zip->extractTo(storage_path('app/public/'.$publication_id));
// Delete the zip file after extracting
Storage::delete($publication_id.'.zip');
}
return;
}
First thing I'd check is if the storage file is created and if it isn't created, create it. Then I'd look at your file permissions and make sure that the the groups and users permissions are correct and that you aren't persisting file permissions on creation. I've had many instances where the process that's creating files(or trying) is not in the proper group and there is a sticky permission on the file structure.
I have tried to setup an upload script in Laravel and have followed the instructions in the docs.
I created a Symlink using the Laravel script and it looks like the following
storage -> /Users/username/Sites/switch/storage/app/public
The problem arrives when I go to upload the image and then get result of the image url in return. As you can see to match the symlink I set the folder to be public below.
$path = $request->file('manufacturer_image_name')->store('public');
echo asset($path);
and this returns
http://127.0.0.1:8000/public/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
the problem is this doesn't work and throws a 404 but if I manually change the url from "public" to "storage" it will find the image.
http://127.0.0.1:8000/storage/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
Shouldn't
echo asset($path);
be returning a url containing storage instead of public?
assett($path) is for generating a URL for assets that are just in the public folder, things like the Mix generated CSS and JS files. If you user Laravel Storage to save the file, you also have to use Laravel storage to generate the file URL.
Storage::url('file.jpg');
Well, there are a lot of ways to do that, pick anyone which fits you best.
// using storage_path helper
storage_path('public/' . $filename);
// you could make a double-check with File::exist() method
$path = storage_path('public/' . $filename);
if (!File::exists($path)) {
abort(404);
}
// using asset helper
asset('storage/your_folder/image.png');
// using url helper
url('storage/your_folder/image.png');
// using Storage facade
Storage::url($photoLink)
Here is the simplest and exact thing for your issue
if(!empty($request->file('manufacturer_image_name'))){
$path = storage_path('public/image/');
$image_path = Storage::disk('public')->put('manufacturer_image_name', $request->file('manufacturer_image_name'));
//Assuming you have a model called Manufacturer and created $manufacturer = new Manufacturer()
$manufacturer->manufacturer_image_name = isset($image_path) ? "storage/".$image_path : "";
}
Thanks for the help, I discovered this answer the fits nearly perfectly what I am after. Laravel: Storage not putting file inside public folder
This was what I ended up with.
if($request->file('manufacturer_image_name')){
$path = Storage::disk('public')->put('logo', $request->file('manufacturer_image_name'));
echo $path;
}
$path now returns "logo/filename.ext" instead of "public/ or storage/" so I can store this directly in the db.
I am trying to import a csv file with Laravel 5.5 from the local file location. For some reason it can't find the file on my computer however the path is correct.
$fileLocation = $request->file('file')->store('csv');
$importFile = File::file(storage_path('app/' . $fileLocation));
You can use the Storage facade for this. Here is an example:
if (Storage::disk($disk)->exists($filename)) {
return Storage::disk($disk)->get($filename);
}
throw new FileNotFoundException(sprintf('File not found: %s', $filename), 404);
If you don't wanna use Storage facade, you can use below code to get the file
$importFile = Illuminate\Support\Facades\File::get(storage_path('app/' . $fileLocation));
but when you store a file with csv as store argument, it will save in storage/csv folder, and you just need call storage_path($fileLocation).
be sure that your storage path is correct and for more information read here
In phalcon you can get uploaded file with this piece of code
//Check if the user has uploaded files
if ($this->request->hasFiles() == true) {
//Print the real file names and their sizes
foreach ($this->request->getUploadedFiles() as $file){
echo $file->getName(), " ", $file->getSize(), "\n";
}
}
each $file is an Phalcon\Http\Request\File instance.
But what if I want to create an file instance from an existing file on the server, how can I do that?
What I tried is this:
new Phalcon\Http\Request\File(array($fileDir));
But it returns an instance with empty properties.
any help would be appreciated :D
As per the documentation of that class I think the constructor does not expect an array. So just leave out the array( ) that you are passing to the constructor and you should be fine. Disclaimer: I did not check out the code and rely on the documentation being proper here.
Code example:
new Phalcon\Http\Request\File($fileDir);
But what if I want to create an file instance from an existing file on the server, how can I do that?
This class is designed to work with $_FILES superglobal, so as for me you are using wrong tool. I would create your own wrapper for using files that are already on server, or go for SplFileObject.
Anyway, how should parameter array look like you may be able to anderstand from here starting at line 74+ and it pretty reflects $_FILES structure.
I'm relatively new to PHP and I'm trying to write my own plugin. Upon plugin activation it will run the following function:
function kb_create_uploadfolder () {
global $wpdp;
$upload_dir = wp_upload_dir();
$upload_dir = $upload_dir['basedir'] . "/plugin_uploads";
$upload_dircheck = wp_mkdir_p($upload_dir);
}
I didn't bother to check whether the directory already exists before creating it since I figured it won't overwrite anything or delete the contents if it does. Correct me if I'm wrong.
The thing is however, I would like to check if the creation of the directory was succesful or not but I can't figure out how to get this information.
Use is_dir():
if(is_dir($upload_dircheck))
{
echo "It is a dir";
}
else
{
echo "Sorry, non-existent or not a dir";
}
Also, mkdir() doesn't delete or overwrite existing contents, it just creates a directory if it does not yet exist.
If you're using PHP 4 or newer then you can use the is_dir() function.
Try is_dir().