Strict Standards: "Only variables should be passed by reference" - php

In the php function below I have on line 31 the error "Strict Standards: Only variables should be passed by reference"
As I read the answers on the forum I must change the explode(..). But I don’t have any idea how..
function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'png'))
{
if (substr($directory, -1) == 'uploads/') {
$directory = substr($directory, 0, -1);
}
$html = '';
if (
is_readable($directory)
&& (file_exists($directory) || is_dir($directory))
) {
$directoryList = opendir($directory);
while($file = readdir($directoryList)) {
if ($file != '.' && $file != '..') {
$path = $directory . '/' . $file;
if (is_readable($path)) {
if (is_dir($path)) {
return scanDirectoryImages($path, $exts);
}
if (
is_file($path) && in_array(end(explode('.', end(explode('/', $path)))), $exts) // Line 31
) {
$html .= '<a href="' . $path . '"><img src="' . $path
. '" style="max-height:100px;max-width:100px" /></a>';
}
}
}
}
closedir($directoryList);
}
return $html;
}

According to the PHP Manual for end:
This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.
Parameter should be sent as an array variable only.
ToDo:
Break this statement inside if:
is_file($path) && in_array(end(explode('.', end(explode('/', $path)))), $exts)
to something like this:
$path1 = explode('/', $path);
$path2 = end($path1);
$path3 = explode('.', $path1);
$path4 = end($path3);
is_file($path) && in_array($path4, $exts)
Alternatively,
Since you are getting extension from the path, you can use pathinfo:
pathinfo($path)['extension']

You can try this code:
<?php
<?php
function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'png'))
{
if (substr($directory, -1) == 'uploads/') {
$directory = substr($directory, 0, -1);
}
$html = '';
if (
is_readable($directory)
&& (file_exists($directory) || is_dir($directory))
) {
$directoryList = opendir($directory);
while($file = readdir($directoryList)) {
if ($file != '.' && $file != '..') {
$path = $directory . '/' . $file;
if (is_readable($path)) {
if (is_dir($path)) {
return scanDirectoryImages($path, $exts);
}
$path_info = pathinfo($path);
$ext = strtolower($path_info['extension']);
if (is_file($path) && in_array($ext, $exts)) {
$html .= '<a href="' . $path . '"><img src="' . $path
. '" style="max-height:100px;max-width:100px" /></a>';
}
}
}
}
closedir($directoryList);
}
return $html;
}

try to separate your code in the if statement like this, this should work :
// other code
if (is_dir($path)) {
return scanDirectoryImages($path, $exts);
}
$explode_1 = explode('/', $path);
$explode_2 = explode('.', end($explode_1));
if (is_file($path) && in_array(end($explode_2), $exts)) {
// the rest of the code

Related

recursively copy a specific file extension php

I am writing a function to recursively copy a specific file type from one folder to the other, but the function copies all the files in the folder.
function recurse_copy($src, $dst) {
$dir = opendir($src);
#mkdir($dst);
while (false !== ( $file = readdir($dir))) {
if (( $file != '.' ) && ( $file != '..' )) {
if (is_dir($src . '/' . $file)) {
if ($file->getExtension() == "pdf") {
recurse_copy($src . '/' . $file, $dst . '/' . $file);
}
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
} closedir($dir);
}
// if statements for
$itp = new RecursiveDirectoryIterator("foldername/", > FilesystemIterator::SKIP_DOTS);
$displayp = Array('pdf');
$i = 0;
foreach (new RecursiveIteratorIterator($itp) as $filepop) {
if (in_array(strtolower(array_pop(explode('.', $filepop))), $displayp))
if ($filepop->getExtension() == "pdf") {
echo >
recurse_copy("a", "b");
}
}
$itcopy = new RecursiveDirectoryIterator("foldername/", FilesystemIterator::SKIP_DOTS);
$displayp = Array ( 'pdf' );
foreach(new RecursiveIteratorIterator($itcopy) as $filecopy)
{
if (in_array(strtolower(array_pop(explode('.', $filecopy))), $displayp))
if ($filecopy->getExtension()=="pdf"){
copy($filecopy->getrealPath(),'pdf_folder/'.$filecopy->getFilename()) ;
}
}

PHP check if string is "." or ".."

I'm trying to eliminate directories from a statement and I can't seem to get rid of the . or .. directories while using PHP.
$album_num = $_POST['album_num'];
$album_directory = "../../galleries";
$albums = array_reverse(scandir($album_directory));
$album = $albums[$album_num];
if($album) {
if($album !== "." || $album !== "..") { //This line here
$image = glob($album_directory . "/" . $album . "/*.jpg");
$image = explode("/", $image[0]);
$image = end($image);
echo $album . ":" . $image;
}
} else {
echo "halt";
}
The line that has if($album !== "." || $album !== "..") does not seem to be working. Do I need to escape the dots?
Am I able to just delete them from the array to begin with?
Try this:
if($album) {
if($album !== "." && $album !== "..") { //This line here
$image = glob($album_directory . "/" . $album . "/*.jpg");
$image = explode("/", $image[0]);
$image = end($image);
echo $album . ":" . $image;
}
}
Change your if condition from || to &&
Use != instead of !==
I suppose u don't want that album evaluate to !==
$album_num = $_POST['album_num'];
$album_directory = "../../galleries";
$albums = array_reverse(scandir($album_directory));
$album = $albums[$album_num];
if($album) {
if($album != "." || $album != "..") { //This line here
$image = glob($album_directory . "/" . $album . "/*.jpg");
$image = explode("/", $image[0]);
$image = end($image);
echo $album . ":" . $image;
}
} else {
echo "halt";
}

Insert Images from folder and subfolder php

Im trying to insert into html all images that are on the folder "assets/imagens/" and on any subfolders. I also need help to validate not to echo the subfolders.
Im using the code below:
$path = "assets/imagens/";
$diretorio = dir($path);
while($arquivo = $diretorio -> read()){
if($arquivo != '.' && $arquivo != '..'){
echo '<div href="#" class="list-group-item">';
echo '<span class="close-button" secao="imagens">x</span>';
echo "<img class='max-width' src='".base_url().$path.$arquivo."' />";
echo '</div>';
}
}
$diretorio -> close();
$path = "assets/imagens/";
echo_directory_images($path);
function echo_directory_images($path)
{
$diretorio = dir($path);
while($arquivo = $diretorio -> read()){
if($arquivo != '.' && $arquivo != '..'){
if( is_dir($path.$arquivo))
{
//if subfolder
echo_directory_images($path.$arquivo.'/')
}
else{
echo '<div href="#" class="list-group-item">';
echo '<span class="close-button" secao="imagens">x</span>';
echo "<img class='max-width' src='".base_url().$path.$arquivo."' />";
echo '</div>';
}
}
}
}
You may want to give a try to this function. I doubt if this will work exactly but this will surely give an idea about how to recursively call folders and subfolders.
Try this :
function listDir( $path ) {
global $startDir;
$handle = opendir( $path );
while (false !== ($file = readdir($handle))) {
if( substr( $file, 0, 1 ) != '.' ) {
if( is_dir( $path.'/'.$file ) ) {
listDir( $path.'/'.$file );
}
else {
if( #getimagesize( $path.'/'.$file ) ) {
/*
// Uncomment if using with the below "pic.php" script to
// encode the filename and protect from direct linking.
$url = 'http://domain.tld/images/pic.php?pic='
.urlencode( str_rot13( substr( $path, strlen( $startDir )+1 ).'/'.$file ) );
*/
$url='http://localhost/'.$path.'/'.$file;
substr( $path, strlen( $startDir )+1 ).'/'.$file;
// You can customize the output to use img tag instead.
echo "<a href='".$url."'>".$url."</a><br>";
echo "<img class='max-width' src='".$url."' />";
}
}
}
}
closedir( $handle );
} // End listDir function
$startDir = "assets/imagens/";
listDir( $startDir );
You can use RecursiveDirectoryIterator.
For example:
$directory = new RecursiveDirectoryIterator('assets/imagens');
$iterator = new RecursiveIteratorIterator($directory);
foreach ($entities as $name => $entity) {
if ($entity->isDir()) {
continue;
}
$extension = pathinfo($name, PATHINFO_EXTENSION);
if ($extension == 'jpg' || $extension == 'png' || $extension == 'gif') {
echo '<img src="' . $entity->getPathname() . '" />';
}
}

opening two folders and displaying images php

I've got the problem of trying to display two albums of photos from a db using php. Currently the following code works but for just one album. Basically I need it to display photos from 'mount-everest-part-2' aswell.
<?php
$path = "images/galleries/";
$album = 'mount-everest-part-1';
if ($handle = opendir($path.$album.'/thumbs/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') {
$files[] = $file;
}
}
closedir($handle);
}
asort($files);
foreach($files as $file) {
echo '<li><img src="../' . $path . $album . '/thumbs/' . $file . '" /></li>';
}
?>
How can I use this code to open two files and spit the files out using the same foreach loop?
This sounds like one of those things that OOP would suit nicely. Here's an example:
<?php
class Album_Picture_File {
private $fileName;
private $path;
private $album;
public function __construct($fileName, $path, $album) {
$this->fileName = $fileName;
$this->path = $path;
$this->album = $album;
}
private function getAlbumPath() {
return '../' . $this->path . $this->album;
}
public function getPicturePath() {
return $this->getAlbumPath() . '/images/' . $this->fileName;
}
public function getThumbnailPath() {
return $this->getAlbumPath() . '/thumbs/' . $this->fileName;
}
}
function fetchFiles($path, $album) {
$files = array();
if ($handle = opendir($path.$album.'/thumbs/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') {
$fullPath = $path . $album . '/thumbs/' . $file;
$files[$fullPath] = new Album_Picture_File($file, $path, $album);
}
}
closedir($handle);
}
ksort($files); //sort after key (out file path)
return $files;
}
$files = array_merge(
fetchFiles('images/galleries/', 'mount-everest-part-1'),
fetchFiles('images/galleries/', 'mount-everest-part-2')
);
foreach($files as $file) {
echo '<li><img src="' . $file->getThumbnailPath() . '" /></li>';
}
?>
Note that instead of pushing $files with strings, we push it with Album_Picture_File objects.
Create a global array :
$all = array();
Then, make 2 loops and push the global array (create a function that reads the directory for example)
$files_dir_one = getFiles("your_dir");
$files_dir_two = getFiles("your_dir2");
$all = array_merge($files_dir_one, $files_dir_two);
function getFiles($directory) {
$files = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') {
$files[] = $file;
}
}
closedir($handle);
}
//asort($files);
return $files;
}
And then, the last step : populate the view
How can I use this code to open two files and spit the files out using the same foreach loop?
Taking your question literally it works like this. First of all extract a function with the two input variables:
/**
* #param string $path
* #param string $album
*/
function list_images($path, $album) {
$files = [];
if ($handle = opendir($path . $album . '/thumbs/'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && substr($file, 0, 2) != '._')
{
$files[] = $file;
}
}
closedir($handle);
}
asort($files);
foreach ($files as $file)
{
echo '<li><img src="../' . $path . $album . '/thumbs/' . $file . '" /></li>';
}
}
Then you can just iterate over your two albums and output both:
$path = "images/galleries/";
$albums = ['mount-everest-part-1', 'mount-everest-part-2'];
foreach ($albums as $album)
{
list_images($path, $album);
}

PHP dynamic require

The main issue it's still the same:
This is my proyect
/
| index.php
|-test1
| test.php
The test.php
<?php
echo $variable;
?>
The index.php
<?php
$variable = "<br>index";
echo 'test<br>';
$full_path = dirname(__FILE__)."/test1";
$adm_extension = array( "php");
if (is_dir($full_path)) {
if (($handle = opendir($full_path))) {
while ($file = readdir($handle)) {
if (is_file($full_path . '/' . $file)) {
$item_path = $full_path . '/' . $file;
$extension = pathinfo($item_path, PATHINFO_EXTENSION);
if (in_array($extension, $adm_extension)) {
require $item_path;
}
}
}
}
}
And it's works like a charm, the output is:
test
index
If i want to encapsulate this functionality in a function like this:
index1.php
$variable = "<br>index";
echo 'test<br>';
$full_path = dirname(__FILE__)."/test1";
$adm_extension = array( "php" );
function rTest($full_path, $adm_extension){
if (is_dir($full_path)) {
if (($handle = opendir($full_path))) {
while ($file = readdir($handle)) {
if (is_file($full_path . '/' . $file)) {
$item_path = $full_path . '/' . $file;
$extension = pathinfo($item_path, PATHINFO_EXTENSION);
if (in_array($extension, $adm_extension)) {
require $item_path;
}
}
}
}
}
}
rTest($full_path, $adm_extension);
I got this:
( ! ) Notice: Undefined variable: variable in C:\wamp\www\sandbox\test1\test.php on line 3
Any clue??
your script probably, when going through files, opens test.php before index1.php so $variable isn't defined yet
test it:
function rTest($full_path, $adm_extension){
if (is_dir($full_path)) {
if (($handle = opendir($full_path))) {
while ($file = readdir($handle)) {
if (is_file($full_path . '/' . $file)) {
$item_path = $full_path . '/' . $file;
$extension = pathinfo($item_path, PATHINFO_EXTENSION);
if (in_array($extension, $adm_extension)) {
echo $item_path.'<br />';
require $item_path;
}
}
}
}
}
}
Ok, a friend help me to solve it out.
It's was a really dumb error. The problem was when I made the require, it was made in the function scope.
For my solution I do something like this:
function rTest($full_path, $adm_extension){
$paths = array();
if (is_dir($full_path)) {
if (($handle = opendir($full_path))) {
while ($file = readdir($handle)) {
if (is_file($full_path . '/' . $file)) {
$item_path = $full_path . '/' . $file;
$extension = pathinfo($item_path, PATHINFO_EXTENSION);
if (in_array($extension, $adm_extension)) {
$paths[] = $item_path;
}
}
}
}
}
return $paths;
}
foreach(rTest($full_path, $adm_extension) as $path){
require $path;
}

Categories