Downloading a directory

I don’t think it is an easy task to “download a directory” instead…

Try using the recursive script in my previous post to:

  1. find all files and directories
  2. test to see if the the item is a file or directory
  3. copy contents of file if it does not exist
  4. create directory if it does not exist
  5. echo Success or Failed

List of available file and directory functions:

Yes, it is not easy task. but a difficult task gives us much Serotonin when it is solved.

Since you gave me the good code below for displaying list of directories and files by recursive way, I thought I can get my target which is downloading a directory if I insert downloading code each on the way of turning(recursive). but I don’t have the correct downloading code of directories although I have the correct downloading code of files

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

$path = ".";


?><!doctype html><html lang="en">
<head>
<title> asdf </title>
<style>
  body {background-color: #fafafa; color: #00f;}
  .bd1 {border: solid 1px #ccc;}
  .dib {display: inline-block;}
  .fsl {font-size: large;}
  .tac {text-align: center;} .tal {text-align: left;}
</style>
</head>
<body class="bgQ">
  <h1>
    <a href="https://www.php.net/manual/en/function.glob.php#92710">
      Source 
    </a> 
  </h1>

  <div class="mga tac">
    <pre class="dib fsl fwb bgs tal bd1">
      <?php 
        $dir_iterator = new RecursiveDirectoryIterator($path);
        $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
        // could use CHILD_FIRST if you so wish
        foreach ($iterator as $file)
        {
          echo "\n" .$file;
        }
      ?>    
    </pre> 

And the code below which download a file still has a problem.

declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

$filepath = "target.php";
$filesize   = -1; // filesize($filepath);
$path_parts = pathinfo($filepath);
$filename = $path_parts[-"basename"-];
$extension = $path_parts[-"extension"-];

header("Pragma: public");
header("Expires: 0");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $filesize");

ob_clean();
flush();
readfile($filepath);

The code above download correct contents but it includes the notice below.

How can I download the correct contents only, not including the notice?

Forget about ob_clean(), etc, look at the many alternative ways there are too download and upload files in the link I supplied in the previous post.

Downloading a file works fine now with error_report.

How about downloading a directory?
I am at https://www.w3schools.com/PHP/php_ref_filesystem.asp
but I can’t find any directory downloading.

if I find the solution for a directory downloading, then I like to challenge the downloading
of the whole directories and files using the recursive way.

Hi @joon1,
If you insist on doing this task with Php code then you are going to want to do this with FTP functions. Check out the manual page. There are also many code examples that may get you on your way.

https://www.php.net/manual/en/ref.ftp.php

  1. @joon Why do you insist on doing it through PHP? It clearly isn’t working out too well …

  2. If you use FTP you first need to install an FTP server on one of the computers. Those are generally not very easy to install (depending on the OS).

The above is typo of the below

I guess The quotes below means:
(1) this task is very difficult.
(2) although I succeed it with lucky, it will not work as I expected.
(3) FTP in PHP is also very difficult

I didn’t know downloading directories is this difficult when I post this question at first.

That doesn’t mean you just have to continue you know. Don’t fall into the sunken cost falacy of I’ve done so much now I should finish it.
Accept defeat, try something else :slightly_smiling_face:

3 Likes

I have to accept your advice.
I’ve learned much during this process.
And I feel I am interacting with you more upgraded and deeply.

Look back at previous suggestions, many require a remote server. There are numerous companies that offer free servers which are excellent for practising and becoming knowledgeable in remote storage and retrieval.

Data transfer has been researched and refined. RSync is exceptionally fast because it transfers only the parts of a file that have changed and, zips while transfering the changes. Imagine trying to write that functionality! Grsync is the free graphical version available on all platforms.

Another example is using Git and Github, etc

There are also Free Graphical Ftp applications, my favourite is FileZilla making dragging and dropping directories very simple especially when only modified files are uploaded/downloaded.

Be aware there are many bad examples on the internet, look for examples with comments otherwise the author is afraid or incompetent to reply to any comments - I await your comments :slight_smile:

I think the ob_clean is not the best example to perform your task.

The solution is to use a free online remote server, make this your priority for today :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.