Search code examples
phpscandirnetwork-drive

How to read files from a network drive in php?


I am working on php code as shown below in which I want to read a network drives as shown below :

enter image description here

$src_dir = is_dir("\\\\ADMPC02-02\\music\\incoming_folder\\");    // Line#A

var_dump($src_dir);  // bool(false)

$mp4_files = preg_grep('~\.(mp4)$~', scandir($src_dir));

$xml_files = preg_grep('~\.(xml)$~', scandir($src_dir));

print_r($mp4_files);  // it doesn't print anything

print_r($xml_files);  // it doesnt print anything

Problem Statement:

I am wondering what changes I need to make at Line#A so that I am successfully able network directory. On doing var_dump($src_dir); it prints bool(false).


Solution

  • This works for me:

    <?php
    
    $src_dir = '\\\ADMPC02-02\test';
    $is_dir = is_dir($src_dir); // returns true
    $mp4_files = preg_grep('~\.(mp4)$~', scandir($src_dir));
    $xml_files = preg_grep('~\.(xml)$~', scandir($src_dir));
    
    echo '<pre>';
    print_r($mp4_files); // lists all the mp4 files in my 'test' folder
    print_r($xml_files); // lists all the xml files in my 'test' folder
    

    You should also make sure that:

    1. Network discovery is On
    2. Files and printers sharing is On
    3. Your music folder is publicly shared will all required permissions for the current Windows user running PHP

    I hope this helps!