Build an array from .INI file using PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've my .ini file as follows
languages.ini
--------------------
fr = "French"
en = "English"
Now, I'd like to read the file and keep the keys and values in the form of an array using PHP. I'm very new to PHP. I hope any one help me in achieving this. This is my following code.
public function GetLanguages()
{
$langdir = ISC_BASE_PATH.'/language';
$skip = Array (
'.',
'..',
'CVS',
'.svn',
);
$langs1 = array();
$dh = opendir($langdir);
while (($file = readdir($dh)) !== false) {
if (!is_file($langdir.'/'.$file.'/languages.ini')) {
continue;
}
$langs1[] = $file;
}
echo "FileLL:".$file;
foreach ($langs1 as $key) {
echo "Store languagesss::".$key."<br>";
}
return $langs1;
}
Thank you in advance.

You are looking for PHP's ini parse function:
parse_ini_file
Just call $arrayResult = parse_ini_file($path_to_file)

Related

Is there any way to add a line of code to all .html files? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 18 days ago.
Improve this question
I have 100 html files, and i want to add a line of code (at the end) to all of them.
How is this possible ?
Something like a script which appends that line into all html files
I tried searching but did not find anything.
You can use :
<?php
$folder = '/path/to/html/files';
$line_to_add = '<p>your line/p>';
foreach (glob("$folder/*.html") as $file) {
$contents = file_get_contents($file);
file_put_contents($file, $line_to_add . PHP_EOL . $contents);
}
Your question is quite unclear, because you didn't specify where you are going to append this lane? Here is a starting point:
<?php
$path = '/path/to/html/files';
$code_to_add = '<p>This line was added by a script.</p>';
$files = scandir($path); // get an array of all the files in the directory specified by $path.
foreach ($files as $file) { // Iterate over files
if (substr($file, -5) == '.html') { // hecking if each file has a ".html" extension.
$file_path = $path . '/' . $file;
$file_contents = file_get_contents($file_path); // read the contents
file_put_contents($file_path, $file_contents . $code_to_add); // put new content, you can modify it.
}
}
?>

upload multiple files on laravel [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a problem for many file uploads with the request structure for the file variables in an array. the following is the code snippet. i need a solution for this, thanks
$cimage = count($request->variant[$loop]['img']);
for($loops1 = 0; $loops1 < $cimage; $loops1++) {
$variantImage = $request->file($request->variant[$loop]['img'][$loops1]);
$nameVariantImage = $variant->id . '-' . date('ymdHis') . ($loops1 + 1) . '.' . $variantImage->getClientOriginalExtension();
$uploadVariant = $imageUpload->upload($variantImage, $nameVariantImage);
$variantimages = New ProductImage;
$variantimages->product_id = $product->id;
$variantimages->product_variant_id = $variant->id;
$variantimages->images = $uploadVariant;
$variantimages->save();
}
It's better using foreach for the looping, so based your request data the code will be like:
foreach ($request->variant as $varian) {
foreach ($varian['img'] as $k => $image) {
$nameVariantImage = $variant->id.'-'.date('ymdHis').($k + 1).'.'
.$image->getClientOriginalExtension();
$filePath = Storage::putFileAs(
"products/variant",
$image,
$nameVariantImage
);
$variantimages = new ProductImage;
$variantimages->product_id = $product->id;
$variantimages->product_variant_id = $variant->id;
$variantimages->images = $filePath;
$variantimages->save();
}
}

Create JSON file with folder contents (images) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am moving images from xcode to an online platform and i need to generate a JSON file from a directory structure (images only) that lists the folders, and the images in those folders, with complete URL (online) AND date they where added.
I am a COMPLETE noob with PHP web stuff, so am really lost at the moment.
I found the following code, but that does not do anything so far, and does not travels into directory's.
<?php
/*
JSON list of all images files in current directory
*/
$dir = ".";
$dh = opendir($dir);
$image_files = array();
while (($file = readdir($dh)) !== false) {
$match = preg_match("/.*\.(jpg|png|gif|jpeg|bmp)/", $file);
if ($match) {
$image_files []= $file;
}
}
echo json_encode($image_files);
closedir($dh);
?>
The following code works for me. Found it here.
I've only modified the echo, added the regex and the time. I hope this answers your question.
<?php
function getDirContents($path) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$files = array();
foreach ($rii as $file) {
if (!$file->isDir() && preg_match("/.*\.(jpg|png|gif|jpeg|bmp)/", $file)) {
$files[] = [
'path' => $file->getPathname(),
'c_time' => $file->getCTime()
];
}
}
return $files;
}
header('Content-Type: application/json');
echo json_encode([
'success' => true,
'files' => getDirContents('.')
]);

How can I create a search form that searches files in a folder? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a search feature on my website. I would like it to search through a certain folder for files on my server and display results from there. I'd rather not use databases.
Is there a way to do this?
<?php
$dir = "/your_folder_here/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == $_POST['SEARCHBOX_INPUT']){
echo(''. $file .''."\n");
}
}
closedir($dh);
}
}
?>
From php.net mostly. Obviously change your file path. Also change the $_POST command in the middle to whatever the name of your search box input is. This will only find exact matches of your search. Some changes could be made to find close matches.
$dir = "/etc/php5/";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == 'songs1.php')
//ur code here ...
}
closedir($dh);
}
}
I got this from PHP.net. I hope this helps you.
This is how I did it, although it displays all the files which are in that directory and it does not give a brief description of each file. I dont know if you can help modify it.
<?php
print "<h2>Showing results for $search</h2>";
$dirName="MYBOOKS";
$dp=opendir($dirName);
chdir($dirName);
while ($currentFile !== false) {
$currentFile = readDir($dp);
$theFiles[] = $currentFile;
}
$BookFiles= preg_grep("/pdf$|gif$|png$|jpg$|jed$/", $theFiles);
$output="";
foreach ($BookFiles as $currentFile) {
$output .= <<< Here
<ul>
<li><a href=MYBOOKS/$currentFile>$currentFile</a></li>
</ul>
Here;
}
$fp=fopen("BookIndex.htm","w");
fputs ($fp,$output);
fclose($fp);
readfile ("BookIndex.htm");
?>

How to read and write images and video with PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am creating a downloadable zip file. In that zip, I want to add php, html, and images and videos.
All of them are in other folders.
I can read and write PHP and HTML files from other files/folders.
But I don't know how to read and write (or move from one folder to anther folder) images and videos.
Please try this code
function move_files($dir)
{
if(is_dir($dir))
{
if($handle = opendir($dir))
{
while(($file = readdir($handle)) !== false)
{
if($file != "." && $file != ".." && $file != "Thumbs.db"/*pesky windows, images..*/)
{
$sourcefile = $dir.$file;
$destinationfile = 'Your New Location';
if (!copy($sourcefile, $destinationfile)) {
echo "failed to copy $file...\n";
}
}
}
closedir($handle);
}
}
}
move_files("folder/");
I you want to move files, see PHP manual: copy, unlink

Categories