Get files from another shared folder

Based on ➠ this topic in the Synology forum.

The situation (and what it will be)

The code

<?php 
$d = dir("../../home/Web Pictures/");
echo "Handle: " . $d->handle . "<br>";
echo "Path: " . $d->path . "<br>";
while (($file = $d->read()) !== false){ 
  echo "filename: " . $file . "<br>"; 
} 
$d->close(); 
?>

Based on the code above, I created a solution that will work.

I make some assumptions.

Folder home is a user directory. Let's say the user is john.

The path to the directory is :

/volume1/homes/john/

The home directory has a folder WebPictures (I use no spaces).

The home directory also has a folder www for webpages.

In folder www make a symbolic link :

ln -s /volume1/homes/john/WebPictures/ webpictures

Here's how to do it in WinSCP.

In folder www create a php file with your code. Let's name it webpictures.php.

In your php code use this line :

$d = dir("webpictures/");

To display the images in the php file (webpage) add this line :

echo "<img src='webpictures/" . $file . "' /><br>";

Display the webpage as :

http://nas-ip-number/~john/webpictures.php

The page will show the filenames and the pictures.

Awesome.

Now go to the web folder and open Personal folder.

Create the same symbolic link as the one above.

In your index.php file use this php code to display the webpictures.php file :

<?php
$file = file_get_contents("http://localhost/~john/webpictures.php");
echo $file;
?>

Display the webpage :

http://nas-ip-number/Personal/index.php

The same filenames and images are now displayed with the other content that's already in the index.php file.

Note :

The php code used in webpictures.php cannot access files in other user directories. Only in its own user directory.

It will work when you want to access the photo directory that's in the same location as the web folder. Use this symbolic link :

ln -s /volume1/photo/ webpictures

PHP will not always recognize symbolic links

A problem I have not been able to solve, is that a symbolic link with PHP to the photo folder may work, but a symbolic link with PHP to the music folder may not.

A symbolic link used in HTML does work.

So, this may not work :

echo "<img src='webpictures/" . $file . "' />";

But this will work

How to do

To create symbolic links you have to connect to the NAS with telnet or ssh and login as root/adminpassword.

On a Mac you use the ➠ Terminal.app. On Windows use ➠ WinSCP or similar applications.

unix+symbolic+link+command

The php files are created with a text editor of your choice.

Change the username to the one you use.

To use other folders change the pathname in the symbolic links.

The explanation is 'as is'.

Jun 18 2023 09:57:14