1

I have a simple PHP script that scans an entire directory and uploads all files. But I want it to upload the folders aswell. (and keep the folder structure) How do I do that?

My code:

$dir = 'Test/';

$di = new RecursiveDirectoryIterator($dir);

$ch = curl_init('https://website.com');
curl_setopt($ch, CURLOPT_URL,'https://website.com');
curl_setopt($ch, CURLOPT_POST,1);

foreach (new RecursiveIteratorIterator($di) as $filename => $file) {

    echo $filename;
    $cFile = curl_file_create($filename);
    $post = array('file'=> $cFile);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $result=curl_exec ($ch);
}

curl_close ($ch);

1 Answer 1

1

You cannot upload just a directory, because it is not an information type that you can upload anywhere. However, there are few simplest approaches.

Approach 1

Send a directory name via cURL. For example:

$post = array('dir'=> $someDirectoryName);

On the other side just create a directory using PHP function mkdir

Remember to pass a path with your file to preserve the directory structure:

$post = array('file'=> $cFile, 'path' => $somePath);

Approach 2

Just pass a file path with your file data:

$post = array('file'=> $cFile, 'path' => 'dir1/subdir2/filename.ext');

On the other side parse 'path' and create all required directories.

Approach 3

Create an archive of your directory (for example, tar.gz) and upload that archive. On the other side just unpack the archive to the target directory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.