Jump to content

reading remote files


jakebur01

Recommended Posts

Are there any permissions I could change or anything I can do to make the code below access files on a remote server?

 

Example: If I could some how make the line work with $startdir = "http://mysite.com/downloads/"; located on my remote server rather than having to use the local folder $startdir = "./"; which is on the local server.

    $host = "./"; // the folder where index.php is located 
    // path for folder, file, buttons(back and home) images 
    $img_back="http://www.orosandrei.ro/images/back.gif"; 
    $img_folder="http://www.orosandrei.ro/images/folder.gif"; 
    $img_file="http://www.orosandrei.ro/images/file.gif"; 
    $img_home="http://www.orosandrei.ro/images/home.gif"; 

// end of install variables 


// returns the extension of a file 
function strip_ext($name) 
{            
         $ext = substr($name, strlen($ext)-4, 4); 
           if(strpos($ext,'.') === false) // if we have a folder element 
           { 
               return "    "; // we return a string of space characters for later sort, 
                             // so that the folder items remain on the first positions 
           } 
           return $ext; // if we have a file we return the extension - .gif, .jpg, etc. 
} 



// returns the files from the $path and returns them in an array 
function getFiles($path) { 

   $files = array(); 
   $fileNames = array(); 
   $i = 0; 
   // build 
   if (is_dir($path)) { 
       if ($dh = opendir($path)) { 
           while (($file = readdir($dh)) !== false) { 
               if (($file == ".") || ($file == "..")) continue; 
               $fullpath = $path . "/" . $file; 
               //$fkey = strtolower($file); 
               $fkey = $file; 
               while (array_key_exists($fkey,$fileNames)) $fkey .= " "; 
               $a = stat($fullpath); 
               $files[$fkey]['size'] = $a['size']; 
               if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-"; 
               else if ($a['size'] > 1024 && $a['size'] <= 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K"; 
               else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb"; 
               else $files[$fkey]['sizetext'] = $a['size'] . " bytes"; 
               $files[$fkey]['name'] = $file; 
               $e = strip_ext($file); // $e is the extension - for example, .gif 
               $files[$fkey]['type'] = filetype($fullpath); // file, dir, etc 
               $k=$e.$file; // we use this string for sorting the array elements by extension and filename; 
               $fileNames[$i++] = $k; 
           } 
           closedir($dh); 
       } else die ("Cannot open directory:  $path"); 
   } else die ("Path is not a directory:  $path"); 
   sort($fileNames,SORT_STRING); // sorting 
   $sortedFiles = array(); 
   $i = 0; 
   foreach($fileNames as $f) { 
           $f = substr($f, 4, strlen($f)-4); // we remove the extension we added in front of the filename for sorting 
           if($files[$f]['name'] !='') $sortedFiles[$i++] = $files[$f];    
    }// ends the foreach where we build the final sorted array 
   return $sortedFiles; 
} 


// folder navigation code 
$startdir = "./"; 
if(isset($_GET['dir'])) { 
    $prev = $_GET['dir']; 
    $folder = $_GET['dir'];    
    echo "<a href=\"javascript:history.go(-1)\"><img src=\"$img_back\"></a>   <a href=\"$host\"><img src=\"$img_home\"></a> <br/><br/>"; 
} else { $folder = $startdir; $prev='';} 
// end folder navigation code 


$files = getFiles($folder); 

foreach ($files as $file) { 
    if(strip_ext($file[name])!='.php'){ 
        $image = $img_file; 
        if($file[type]=='dir') { 
            $image = $img_folder; 
            $cmd='?dir='.$prev.$file[name].'/'; 
        }// if the element is a directory 
        else $cmd=$prev.$file[name]; 
        echo "<a href=\"$cmd\" title=\"$file[type],  $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>"; 
    }//if strip_ext 
}//foreach 

 

Link to comment
https://forums.phpfreaks.com/topic/139865-reading-remote-files/
Share on other sites

Are there any permissions I could change or anything I can do to make the code below access files on a remote server?

 

Example: If I could some how make the line work with $startdir = "http://mysite.com/downloads/"; located on my remote server rather than having to use the local folder $startdir = "./"; which is on the local server.

PHP is server-side, meaning that it is executed on the server on which it resides.  You are not able to include a remotes PHP script into your script like it was there locally.  That's just not how it works. Sorry.

Link to comment
https://forums.phpfreaks.com/topic/139865-reading-remote-files/#findComment-731720
Share on other sites

I have been playing with this.

 

How could I tie this ftp connection:

$ftp_server = 'myip'; 
$ftp_port = '21'; 
$conn = ftp_connect($ftp_server,$ftp_port);  




$ftpUser = "myuser"; 
$ftpPass = "mypass"; 

set_time_limit(160); 



$login = ftp_login($conn, $ftpUser, $ftpPass) 
or die("Login credentials were rejected"); 

$workingDir = ftp_pwd($conn); 

echo "Files for directory: $workingDir<br><br>"; 

$fList = @ftp_nlist($conn, $workingDir); 

if(is_array($fList)) 
{ 
for($i = 0; $i < sizeof($fList); $i++) 
{ 
echo $fList[$i] . "<br>"; 
} 
} 
else 
{ 
echo "$workingDir contains no files."; 
} 

ftp_quit($conn); 



 

Into this cool script?

$host = "./"; // the folder where index.php is located 
    // path for folder, file, buttons(back and home) images 
    $img_back="http://www.orosandrei.ro/images/back.gif"; 
    $img_folder="http://www.orosandrei.ro/images/folder.gif"; 
    $img_file="http://www.orosandrei.ro/images/file.gif"; 
    $img_home="http://www.orosandrei.ro/images/home.gif"; 

// end of install variables 


// returns the extension of a file 
function strip_ext($name) 
{            
         $ext = substr($name, strlen($ext)-4, 4); 
           if(strpos($ext,'.') === false) // if we have a folder element 
           { 
               return "    "; // we return a string of space characters for later sort, 
                             // so that the folder items remain on the first positions 
           } 
           return $ext; // if we have a file we return the extension - .gif, .jpg, etc. 
} 



// returns the files from the $path and returns them in an array 
function getFiles($path) { 

   $files = array(); 
   $fileNames = array(); 
   $i = 0; 
   // build 
   if (is_dir($path)) { 
       if ($dh = opendir($path)) { 
           while (($file = readdir($dh)) !== false) { 
               if (($file == ".") || ($file == "..")) continue; 
               $fullpath = $path . "/" . $file; 
               //$fkey = strtolower($file); 
               $fkey = $file; 
               while (array_key_exists($fkey,$fileNames)) $fkey .= " "; 
               $a = stat($fullpath); 
               $files[$fkey]['size'] = $a['size']; 
               if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-"; 
               else if ($a['size'] > 1024 && $a['size'] <= 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K"; 
               else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb"; 
               else $files[$fkey]['sizetext'] = $a['size'] . " bytes"; 
               $files[$fkey]['name'] = $file; 
               $e = strip_ext($file); // $e is the extension - for example, .gif 
               $files[$fkey]['type'] = filetype($fullpath); // file, dir, etc 
               $k=$e.$file; // we use this string for sorting the array elements by extension and filename; 
               $fileNames[$i++] = $k; 
           } 
           closedir($dh); 
       } else die ("Cannot open directory:  $path"); 
   } else die ("Path is not a directory:  $path"); 
   sort($fileNames,SORT_STRING); // sorting 
   $sortedFiles = array(); 
   $i = 0; 
   foreach($fileNames as $f) { 
           $f = substr($f, 4, strlen($f)-4); // we remove the extension we added in front of the filename for sorting 
           if($files[$f]['name'] !='') $sortedFiles[$i++] = $files[$f];    
    }// ends the foreach where we build the final sorted array 
   return $sortedFiles; 
} 


// folder navigation code 
$startdir = "./"; 
if(isset($_GET['dir'])) { 
    $prev = $_GET['dir']; 
    $folder = $_GET['dir'];    
    echo "<a href=\"javascript:history.go(-1)\"><img src=\"$img_back\"></a>   <a href=\"$host\"><img src=\"$img_home\"></a> <br/><br/>"; 
} else { $folder = $startdir; $prev='';} 
// end folder navigation code 


$files = getFiles($folder); 

foreach ($files as $file) { 
    if(strip_ext($file[name])!='.php'){ 
        $image = $img_file; 
        if($file[type]=='dir') { 
            $image = $img_folder; 
            $cmd='?dir='.$prev.$file[name].'/'; 
        }// if the element is a directory 
        else $cmd=$prev.$file[name]; 
        echo "<a href=\"$cmd\" title=\"$file[type],  $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>"; 
    }//if strip_ext 
}//foreach

 

Link to comment
https://forums.phpfreaks.com/topic/139865-reading-remote-files/#findComment-731732
Share on other sites

Is there anyone that can help me get started with this? I want to implement ftp functions from here:

$ftp_server = 'myip'; 
$ftp_port = '21'; 
$conn = ftp_connect($ftp_server,$ftp_port);  




$ftpUser = "myuser"; 
$ftpPass = "mypass"; 

set_time_limit(160); 



$login = ftp_login($conn, $ftpUser, $ftpPass) 
or die("Login credentials were rejected"); 

$workingDir = ftp_pwd($conn); 

echo "Files for directory: $workingDir<br><br>"; 

$fList = @ftp_nlist($conn, $workingDir); 

if(is_array($fList)) 
{ 
for($i = 0; $i < sizeof($fList); $i++) 
{ 
echo $fList[$i] . "<br>"; 
} 
} 
else 
{ 
echo "$workingDir contains no files."; 
} 

ftp_quit($conn); 

 

into this code:

$host = "./"; // the folder where index.php is located 
    // path for folder, file, buttons(back and home) images 
    $img_back="http://www.orosandrei.ro/images/back.gif"; 
    $img_folder="http://www.orosandrei.ro/images/folder.gif"; 
    $img_file="http://www.orosandrei.ro/images/file.gif"; 
    $img_home="http://www.orosandrei.ro/images/home.gif"; 

// end of install variables 


// returns the extension of a file 
function strip_ext($name) 
{            
         $ext = substr($name, strlen($ext)-4, 4); 
           if(strpos($ext,'.') === false) // if we have a folder element 
           { 
               return "    "; // we return a string of space characters for later sort, 
                             // so that the folder items remain on the first positions 
           } 
           return $ext; // if we have a file we return the extension - .gif, .jpg, etc. 
} 



// returns the files from the $path and returns them in an array 
function getFiles($path) { 

   $files = array(); 
   $fileNames = array(); 
   $i = 0; 
   // build 
   if (is_dir($path)) { 
       if ($dh = opendir($path)) { 
           while (($file = readdir($dh)) !== false) { 
               if (($file == ".") || ($file == "..")) continue; 
               $fullpath = $path . "/" . $file; 
               //$fkey = strtolower($file); 
               $fkey = $file; 
               while (array_key_exists($fkey,$fileNames)) $fkey .= " "; 
               $a = stat($fullpath); 
               $files[$fkey]['size'] = $a['size']; 
               if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-"; 
               else if ($a['size'] > 1024 && $a['size'] <= 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K"; 
               else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb"; 
               else $files[$fkey]['sizetext'] = $a['size'] . " bytes"; 
               $files[$fkey]['name'] = $file; 
               $e = strip_ext($file); // $e is the extension - for example, .gif 
               $files[$fkey]['type'] = filetype($fullpath); // file, dir, etc 
               $k=$e.$file; // we use this string for sorting the array elements by extension and filename; 
               $fileNames[$i++] = $k; 
           } 
           closedir($dh); 
       } else die ("Cannot open directory:  $path"); 
   } else die ("Path is not a directory:  $path"); 
   sort($fileNames,SORT_STRING); // sorting 
   $sortedFiles = array(); 
   $i = 0; 
   foreach($fileNames as $f) { 
           $f = substr($f, 4, strlen($f)-4); // we remove the extension we added in front of the filename for sorting 
           if($files[$f]['name'] !='') $sortedFiles[$i++] = $files[$f];    
    }// ends the foreach where we build the final sorted array 
   return $sortedFiles; 
} 


// folder navigation code 
$startdir = "./"; 
if(isset($_GET['dir'])) { 
    $prev = $_GET['dir']; 
    $folder = $_GET['dir'];    
    echo "<a href=\"javascript:history.go(-1)\"><img src=\"$img_back\"></a>   <a href=\"$host\"><img src=\"$img_home\"></a> <br/><br/>"; 
} else { $folder = $startdir; $prev='';} 
// end folder navigation code 


$files = getFiles($folder); 

foreach ($files as $file) { 
    if(strip_ext($file[name])!='.php'){ 
        $image = $img_file; 
        if($file[type]=='dir') { 
            $image = $img_folder; 
            $cmd='?dir='.$prev.$file[name].'/'; 
        }// if the element is a directory 
        else $cmd=$prev.$file[name]; 
        echo "<a href=\"$cmd\" title=\"$file[type],  $file[sizetext]\"><img src=\"$image\" /> $file[name]</a> <br/>"; 
    }//if strip_ext 
}//foreach

 

Link to comment
https://forums.phpfreaks.com/topic/139865-reading-remote-files/#findComment-731757
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.