I am trying to get content of all the files in my directory and I am getting an error that says
ErrorException in Util.php line 114:
preg_match() expects parameter 2 to be string, array given
. Below is the code that I am using.
public function store(Request $request)
{
$directory = storage_path('app/xmlentries/uploads');
$files = File::files($directory);
foreach ($files as $file)
{
$contents = Storage::get($file);
dd($contents);
}
How would i get the contents of all my files in this folder?
Try this:
$directory = storage_path('app/xmlentries/uploads/');
foreach (glob($directory . "*") as $file) {
$fileContent = file_get_contents($file);
dd($fileContent); // change this per your need
}
Please note this will display the first file and then stop!
Related
I have some files that I store at /storage/app/public/clientA/files/*.pdf. I have models that I can use to access these, and I want users to be able to download multiple files by selecting them in Nova and then using an action. Here is my code for the action:
public function handle(ActionFields $fields, Collection $models)
{
$files = array();
foreach ($models as $file) {
$path = FileHelper::getPathFromUrl($file->url);
array_push($files, $path);
}
$zip_file = 'myfiles.zip';
$zip = new \ZipArchive();
if ($zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) === true)
{
foreach ($files as $item) {
$zip->addFile(public_path('storage/' . $item), $item);
}
$zip->close();
}
return Action::download($zip_file, $zip_file);
}
Also, here is my code for the getPathFromUrl method:
public static function getPathFromUrl ($url)
{
$path = '';
$url_path = parse_url($url, PHP_URL_PATH);
return substr($url_path, strpos($url_path, "/") + 1);
} // returns the format storage/clientA/files/fileName.pdf
My issue at the moment is that its generating an empty zip file. I'm guessing that my paths are wrong when I try and reference the files, but I don't know how to fix it. I've also tried accessing these locations using Storage::get and found that it can't see the files at valid locations (and yes, I have done Storage:link).
Can anyone give me some insight into what I need to change to my addFile to ensure that these pdfs get added to the zip file?
I am trying to insert the details of an uploaded file into a database table and I am getting the following error:
Fatal error: Call to undefined method Symfony\Component\Finder\SplFileInfo::getClientOriginalName()
How would I get the getClientOriginalName(), getClientOriginalName() and getFilename() of a file in Laravel5?
Below is the code I am using.
public function add()
{
$directory = public_path('xml');
$files = File::allFiles($directory);
foreach ($files as $file) {
$entry = new Xmlentry();
$entry->mime = $file->getClientMimeType();
$entry->original_filename = $file->getClientOriginalName();
$entry->filename = $file->getFilename().'.'.$extension;
$entry->save();
}
}
I'm a bit confused why you have getClientOriginalName() in there because that's aimed at temporary file names that have been uploaded but File::allFiles() is getting files from a directory that already have fixed names.
In addition to my comments above, I wanted to add you can just use the SplFileInfo methods.
I've taken the liberty of removing original file name from the code and correcting the lack of assignment statement for the variable $extension.
To answer your question:
public function add()
{
$directory = public_path('xml');
$files = File::allFiles($directory);
foreach ($files as $file) {
$entry = new Xmlentry();
$entry->mime = $file->getType();
$entry->filename = $file->getFilename(). '.' . $file->getExtension();
$entry->save();
}
}
I have pdf files which are report cards of students.The report card names format is <student full name(which can have spaces)><space><studentID>.I need to download files.For this I have used the following code.
if(file_exists($folder_path.'/') && is_dir(folder_path)) {
$report_files = glob(folder_path.'/*'.'_*\.pdf' );
if(count($report_files)>0)
{
$result_data = '';
$result_data = rename_filenamespaces($report_files);
var_dump($result_data);//this shows the edited filename
foreach ($result_data as $file) {
if (strpos($file,$_GET['StudentID']) !== false) {
//code for showing the pdf docs to download
}
}
}
}
//function for renaming if filename has spaces
function rename_filenamespaces($location)
{
$new_location = $location;
foreach ($location as $file) {
//check file has spaces and filename has studentID
if((strpos($file," ")!==false)&& (strpos($file,$_GET['StudentID']) !== false))
{
$new_filename = str_replace(" ","-",$file);
rename($file,$new_filename);
$new_location = $new_filename;
}
}
return $new_location;
}
The variable $result_data gives me the filename without spaces,but the for each loop is showing Warning:Invalid argument supplied for foreach(). But the filename is changed in the server directory immediately after running the function. This warning shows only for first time. I am unable to solve this.
$new_location = $new_filename;
$new_location is a array
$new_filename is a string
You have to use $new_location[$index]
or try
foreach ($new_location as &$file) {
...
...
$file = $new_filename;
I am trying to copy a file from one directory to another as follows:
$files = File::allFiles($temp);
foreach ($files as $f)
{
$f->move($destination, File::name($f));
}
I am getting this error:
Call to undefined method Symfony\Component\Finder\SplFileInfo::move()
In the following line:
$f->move($destination, File::name($f));
It seems like it does not detect $f as being of type file because every time I try and use any of its functions like
getClientOriginalName()
I get an error.
I keep getting that error. It seems as its not registering $f as being a file..
Also another thing to keep in mind is that I don't know the name of the file thus why I get all of the files in the directory (only ever 1 at a time)
Try this:
File::get($f)
Let me know what happens.
Change this line of code:
$files = File::allFiles($temp);
to
$files = Input::file('files');
Try:
$files = File::files($temp);
foreach ($files as $file)
{
File::move($file, $destination . basename($file));
// [or]
// rename($file, $destination . basename($file));
}
Thanks in advance.
Getting this warning when using below code:
Warning: file_get_contents(test.php) [function.file-get-contents]: failed to open stream: No such file or directory in /path/index.php on line so-n-so.
Here's the code I am using,
<?php
// Scan directory for files
$dir = "path/";
$files = scandir($dir);
// Iterate through the list of files
foreach($files as $file)
{
// Determine info about the file
$parts = pathinfo($file);
// If the file extension == php
if ( $parts['extension'] === "php" )
{
// Read the contents of the file
$contents = file_get_contents($file);
// Find first occurrence of opening template tag
$from = strpos($contents, "{{{{{");
// Find first occurrence of ending template tag
$to = strpos($contents,"}}}}}");
// Pull out the unique name from between the template tags
$uniqueName = substr($contents, $from+5, $to);
// Print out the unique name
echo $uniqueName ."<br/>";
}
}
?>
The error message says that the file isn't found.
This is because scandir() returns only the basename of the files from your directory. It doesn't include the directory name. You could use glob() instead:
$files = glob("$dir/*.php");
This returns the path in the result list, and would also make your extension check redundant.
I would suggest that you need to exclude . and .. from the list of files picked up by scandir() DOCs.
// Iterate through the list of files
foreach($files as $file) {
if('.' == $file or '..' == $file) {
continue;
}
...
Also you need to put the path before your file name:
$contents = file_get_contents($path . $file);