adam291086 Posted May 18, 2008 Share Posted May 18, 2008 hello, i am trying to read a directory and list all the files. I keep getting the error messages Warning: opendir(http://www.bcyorkshire.co.uk/admin/entry_form/form/) [function.opendir]: failed to open dir: not implemented in /homepages/30/d227915861/htdocs/admin/rte/view_uploaded_items/view_entry_forms.php on line 60 this is my code <?php error_reporting(E_ALL); $dir ="http://www.bcyorkshire.co.uk/admin/entry_form/form/"; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { ?> I know the directory existing any thoughts? Link to comment https://forums.phpfreaks.com/topic/106157-solved-problem-reading-dir/ Share on other sites More sharing options...
Orio Posted May 18, 2008 Share Posted May 18, 2008 You can't receive a file list from a remote server via http... You can use the file transfer protocol (ftp) using opendir(). Or just use the FTP functions. Of course you need ftp access... Orio. Link to comment https://forums.phpfreaks.com/topic/106157-solved-problem-reading-dir/#findComment-544129 Share on other sites More sharing options...
adam291086 Posted May 18, 2008 Author Share Posted May 18, 2008 ok i have implemented some ftp stuff. $c = @ftp_connect($ftpServer); $l = @ftp_login($c, $ftpUser, $ftpPass); $currDir = ftp_pwd($c); $contents = ftp_nlist($c, "admin/entry_form/form/"); //loop through array of files and folders for($i = 0; $i < sizeof($contents); $i++) { // We will remove the parent directory (if any) // from the name of the file that gets displayed $trimFile = str_replace("$currDir", "", $contents[$i]); // Remove any forward slash at front of name $trimFile = str_replace("^/", "", $trimFile); ?> <a href ="http://www.bcyorkshire.co.uk/admin/entry_form/form/<?php echo $trimFile?>"><?php echo $trimFile?> </a> <br /> <br /> <div class = "maintext"> Link for the document: http://www.bcyorkshire.co.uk/admin/entry_form/form/<?php echo $contents[$i]?> </div> <br /> <br /> <br /> <?php } ftp_close($c); ?> this works fine. But i am trying to get the file name in its own. I thought using ftp_pwd would do it but that only remove bcyorkshire.co.uk and leave behine adminentry_formform/filename. It removes the /'s but not everything else. Also how do i remove the . and .. files Link to comment https://forums.phpfreaks.com/topic/106157-solved-problem-reading-dir/#findComment-544155 Share on other sites More sharing options...
adam291086 Posted May 18, 2008 Author Share Posted May 18, 2008 scrap the first question about getting the file name as i have just spotted an error. But how do i remove the . and .. Link to comment https://forums.phpfreaks.com/topic/106157-solved-problem-reading-dir/#findComment-544159 Share on other sites More sharing options...
adam291086 Posted May 18, 2008 Author Share Posted May 18, 2008 this is what i have got to remove the . and .. but it doesn't work. I don't get any errors but i still get the . and .. links appearing $c = @ftp_connect($ftpServer); $l = @ftp_login($c, $ftpUser, $ftpPass); $currDir = ftp_pwd($c); $contents = ftp_nlist($c, "admin/entry_form/form/"); //loop through array of files and folders foreach($contents as $file) { if ($file!='.'&&$file!='..') { // We will remove the parent directory (if any) // from the name of the file that gets displayed $trimFile = str_replace("admin/entry_form/form/", "", $file); // Remove any forward slash at front of name $trimFile = str_replace("^/", "", $trimFile); ?> <a href ="http://www.bcyorkshire.co.uk/admin/entry_form/form/<?php echo $trimFile?>"><?php echo $trimFile?> </a> <br /> <br /> <div class = "maintext"> Link for the document: http://www.bcyorkshire.co.uk/admin/entry_form/form/<?php echo $file?> </div> <br /> <br /> <br /> <?php } } ftp_close($c); Link to comment https://forums.phpfreaks.com/topic/106157-solved-problem-reading-dir/#findComment-544199 Share on other sites More sharing options...
Orio Posted May 18, 2008 Share Posted May 18, 2008 Try it this way. <?php $c = @ftp_connect($ftpServer); $l = @ftp_login($c, $ftpUser, $ftpPass); $currDir = ftp_pwd($c); $contents = ftp_nlist($c, "admin/entry_form/form/"); //loop through array of files and folders foreach($contents as $file) { if($file == "." || $file == "..") continue; //Skip current // We will remove the parent directory (if any) // from the name of the file that gets displayed $trimFile = str_replace("admin/entry_form/form/", "", $file); // Remove any forward slash at front of name $trimFile = str_replace("^/", "", $trimFile); ?> <a href ="http://www.bcyorkshire.co.uk/admin/entry_form/form/<?php echo $trimFile?>"><?php echo $trimFile?> </a> <br /> <br /> <div class = "maintext"> Link for the document: http://www.bcyorkshire.co.uk/admin/entry_form/form/<?php echo $file?> </div> <br /> <br /> <br /> <?php } } ftp_close($c); ?> Take also a look into basename(). Could simplify your work. Orio. Link to comment https://forums.phpfreaks.com/topic/106157-solved-problem-reading-dir/#findComment-544203 Share on other sites More sharing options...
adam291086 Posted May 18, 2008 Author Share Posted May 18, 2008 i was having a blonde moment, i should of been check $trimFile for the . and .. after i had got the file name only. Thanks for the help Link to comment https://forums.phpfreaks.com/topic/106157-solved-problem-reading-dir/#findComment-544208 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.