Same code with different variable name not working in PHP? - php

I am using this code to list all files inside a directory which works perfectly
<?php
$exclude = array("index.php","cssheadertop.php","cssheaderbottom.php");
$cssfiles = array_diff(glob("*.php"), $exclude);
foreach ($cssfiles as $cssfile) {
$filename = "http://example.com/lessons/css/".$cssfiles[$cssfile];
outputtags($filename,true,true);
}
?>
However, with this code nothing is shown on the webpage. I can't figure out why
<?php
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude);
foreach ($htmlfiles as $htmlfile) {
$filename = "http://example.com/lessons/html/".$htmlfiles[$htmlfile];
outputtags($filename,true,true);
}
?>

Try this:
<?php
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude);
foreach ($htmlfiles as $htmlfile) {
$filename = "http://example.com/lessons/html/".$htmlfile;
outputtags($filename,true,true);
}
?>
$htmlfiles[$htmlfile] should not be set and should not work.

You need to use $htmlfile inside foreach loop instead of $htmlfiles[$htmlfile], and it works with any other variable's name
<?php
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude);
foreach ($htmlfiles as $htmlfile) {
$filename = "http://example.com/lessons/html/".$htmlfile;
outputtags($filename,true,true);
}
?>

Related

Php random order for imported .php pages [solved]

Version with 1 result
<?php
srand();
$files = array("folder/content1.php", "folder/content2.php", "folder/content3.php", "folder/content4.php", "folder/content5.php", "folder/content6.php");
$rand = array_rand($files);
include ($files[$rand]);
?>
Version with 5 results (provided by Code Spirit)
<?php
$files = array("folder/content1.php", "folder/content2.php", "folder/content3.php", "folder/content4.php", "folder/content5.php", "folder/content6.php");
foreach (array_rand($files, 5) as $file) {
include($files[$file]);
}
?>
You need a loop.
foreach (array_rand($files, 3) as $file) {
include($files[$file]);
}

How to create a json in for loop in laravel

I have a array of 3 images
$images
for ($i=0;$i<count($image); $i++) {
array_push($imgArray, $image[i]);
$valString = implode(',', $imgArray);
$setting = $valString;
print_r(settings);
}
o/p: img1.jpg,img2.jpg,img3.jpg
But I wnat the o/p as
{'ad1':img1.jpg,'ad2':img2.jpg,'ad3':img3.jpg}
i.e like a json.
Can anyoe please suggest help.Thanks.
if your array is ['img1.jpg','img2.jpg','img3.jpg'] then you should use below code
<?php
$images = ['img1.jpg','img2.jpg','img3.jpg'];
foreach($images as $key=>$image){
$images['ad'.($key+1)] = $images[$key];
unset($images[$key]);
}
print_r(json_encode($images));
?>
live demo : https://eval.in/823702
$arr = [];
for ($i=0;$i<count($image); $i++) {
$arr['ad'.$i+1] = $image[$i];
}
$settings = json_encode($arr);
print $settings;

php copy file for each filename in array

I am trying to move all the files in my array from one directory to another.
I have done some research and are using the php Copy() function.
here is my code so far:
$filenameArray = "img1.png,img2.png,img3.png";
$sourcePath = "/source/";
$savePath = "/newDir/";
$myArray = explode(',', $filenameArray);
$finalArray = print_r($myArray);
function copyFiles($finalArray,$sourcePath,$savePath) {
for($i = 0;$i < count($finalArray);$i++){
copy($sourcePath.$finalArray[$i],$savePath.$finalArray[$i]);}
}
Anyone see where I'm going wrong?
Thanks in advance!
This is the unlink ive been attempting to use.
function copyFiles($finalArray,$sourcePath,$savePath) {
foreach ($finalArray as $file){
if (!copy($sourcePath.$file,$savePath.$file)) {
echo "Failed to move image";
}
$delete[] = $sourcePath.$file;
}
}
// Delete all successfully-copied files
foreach ( $delete as $file ) {
unlink( $sourcePath.$file );
}
My Final Working Code
the code below moves images in comma seperated array to new folder and removes them from current folder
$finalArray = explode(',', $filenameArray);
function copyFiles($finalArray,$sourcePath,$savePath) {
foreach ($finalArray as $file){
if (!copy($sourcePath.$file,$savePath.$file)) {
echo "Failed to move image";
}
}
}
copyFiles( $finalArray, $sourcePath, $savePath);
function removeFiles($finalArray,$sourcePath) {
foreach ($finalArray as $file){
if (!unlink($sourcePath.$file)) {
echo "Failed to remove image";
}
}
}
removeFiles( $finalArray, $sourcePath);
In your code you are not calling the copyFile function. Try this:
$filenameArray = "img1.png,img2.png,img3.png";
$sourcePath = "/source/";
$savePath = "/newDir/";
$finalArray = explode(',', $filenameArray);
function mvFiles($finalArray,$sourcePath,$savePath) {
foreach ($finalArray as $file){
if (!rename($sourcePath.$file,$savePath.$file)) {
echo "failed to copy $file...\n";
}
}
}
mvFiles( $finalArray, $sourcePath, $savePath);
A simple solution :
$filenameArray = "img1.png,img2.png,img3.png";
$sourcePath = "/source/";
$savePath = "/newDir/";
$myArray = explode(',', $filenameArray);
$finalArray = $myArray; //corrected this line
function copyFiles($finalArray, $sourcePath, $savePath)
{
for ($i = 0; $i < count($finalArray); $i++)
{
copy($sourcePath.$finalArray[$i],$savePath.$finalArray[$i]);
}
}
Hope you have right call to function copyFiles().
UPDATE for unlink() :
Let me try to throw some light on your work (written code):
foreach ($finalArray as $file)
{
if (!copy($sourcePath.$file,$savePath.$file))
{
echo "Failed to move image";
}
$delete[] = $sourcePath.$file;
}
Contents of $delete :
a. /source/img1.png
b. /source/img2.png
c. /source/img3.png
Now,
foreach ( $delete as $file )
{
unlink( $sourcePath.$file );
}
unlink() will be called with the following parameters:
$sourcePath.$file : /source/./source/img1.png : /source//source/img1.png => No such path exists
$sourcePath.$file : /source/./source/img2.png : /source//source/img2.png => No such path exists
$sourcePath.$file : /source/./source/img3.png : /source//source/img3.png => No such path exists
$sourcePath.$file : /source/./source/img4.png : /source//source/img4.png => No such path exists
I think for this reason, unlink is not working.
The code to be written should be like the following:
foreach ( $delete as $file )
{
unlink( $file );
}
Now, unlink() will be called with the following parameters:
a. /source/img1.png => path do exists
b. /source/img2.png => path do exists
c. /source/img3.png => path do exists
Do tell me if this does not solves the issue.
Update as per Dave Lynch's code:
$filenameArray = "img1.png,img2.png,img3.png";
$sourcePath = "/source/";
$savePath = "/newDir/";
$finalArray = explode(',', $filenameArray);
foreach ($finalArray as $file)
{
$delete[] = $sourcePath.$file;
}
foreach ( $delete as $file )
{
echo $sourcePath.$file . "</br>";
}
Output:
/source//source/img1.png
/source//source/img2.png
/source//source/img3.png
Please check.
Thanks and Regards,

Import variables in file into array

Basically, I have multiple files containing loads of PHP variables, and i'm trying to create a way to read them in to a subarray for each file with each variable being in the array.
<?php
$data = Array();
$getData = glob('siteData/*.vars.php', GLOB_BRACE);
foreach($getData as $tempData) {
$dataKey = str_replace(".vars.php", "", str_replace("siteData/", "", $tempData));
$data[$dataKey] = Array();
//foreach varaible in file
//$data[$dataKey][$varaibleName] = $variable
}
?>
The bit commented out is the bit I need help with as I have no idea how to do it.
Please help, TIA.
After a bit of help, the answering code is as follows:
<?php
$data = Array();
$getData = glob('siteData/*.vars.php', GLOB_BRACE);
foreach($getData as $tempData) {
$dataKey = str_replace(".vars.php", "", str_replace("siteData/", "", $tempData));
$data[$dataKey] = Array();
$dataFile = file_get_contents($tempData);
preg_match_all('/\$[A-Za-z0-9-_]+\s?=\s?["\'].+["\'];/', $dataFile, $dataVars);
$dataVars = $dataVars[0];
foreach($dataVars as $variable) {
$variable = explode("=", $variable);
$varaibleName = ltrim(trim($variable[0]), "$");
$variable = trim(trim(rtrim(trim($variable[1]), ";"), "'"), '"');
$data[$dataKey][$varaibleName] = $variable;
}
}
print_r($data);
?>

php array. cant get the output i desire. help me

i have this code
$dir = "img/ori/";
$dir_thumbs = "img/thumbs/";
$images = array();
$d = dir($dir);
while($name = $d->read()){
$thumb = "thumb_".$name;
$images[] = array('name' => $name,'thumb_url' => $dir_thumbs.$thumb);
}
$d->close();
$o = array('images'=>$images);
i want to output $name and $thumb_url of all the images. how to do it.? help me
<?php
foreach ($images as $image) {
echo "{$image['name']} {$image['thumb_url']}";
}
?>
Use print_r or var_dump for this:
print_r($o);
//or
var_dump($o);

Categories