...

View Full Version : Error in accessing remote file. Please help.



ramki067
02-20-2008, 07:50 AM
Hi,
I'm using the below code for accessing a remote file.



$filelocation = "\\fs-blr-03\str_audio1\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt";
$fp = fsockopen($filelocation, 80, $errno, $errstr, 30);
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: ".$filelocation."\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp);
print $result;

But it giving the following error:


Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Program Files\Apache Group\Apache2\htdocs\selected_stream.php on line 75

Warning: fsockopen() [function.fsockopen]: unable to connect to \\fs-blr-03\str_audio1\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt:80 (Unknown error) in C:\Program Files\Apache Group\Apache2\htdocs\selected_stream.php on line 75


Please help.

Thanks,
Ramki.

deamonlizard
02-20-2008, 08:35 AM
Hi

Nowhere in your code do you tell fsockopen what the address of the server is.

You code needs to look something like this:



$remoteServerAddress = 'www.yourserver.com';
$filelocation = "\\fs-blr-03\str_audio1\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt";
$fp = fsockopen($remoteServerAddress, 80, $errno, $errstr, 30);
$out = "GET $filelocation HTTP/1.1\r\n";
$out .= "Host: ".$remoteServerAddress."\r\n";
$out .= "Connection: Close\r\n\r\n";



Another problem is that you are using backslashes

fs-blr-03\str_audio1\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt

should be fs-blr-03/str_audio1/Automation/Data_files/playback_automation_datafile_WMA_48kHz_full.txt

deamonlizard
02-20-2008, 08:38 AM
Wait? Are you trying to access a network share?

If that is the case it is better to map a drive locally and read the file as if that file was on the machine's harddrive itself.

ie map z: to \\fs-blr-03\str_audio1 (if on a windows machine)

Then read the file like you would any other file



$filename = "z:\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

ramki067
02-20-2008, 08:42 AM
yes... its on the network share. Even after mapping i'm getting the following error,



Warning: fopen(x:\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt) [function.fopen]: failed to open stream: No such file or directory in C:\Program Files\Apache Group\Apache2\htdocs\selected_stream.php on line 92

Warning: filesize() [function.filesize]: stat failed for x:\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt in C:\Program Files\Apache Group\Apache2\htdocs\selected_stream.php on line 93

But when the same file is copied on to my local machine the your code works! I'm not able to figure out whats the problem!!!

Thanks,
Ramki.

deamonlizard
02-20-2008, 08:55 AM
Then your box cannot see the file

Do this

Click Start
Click Run
x:\Automation\Data_files\

A new explorer window will open - if not the share isn't set up correctly. If that is the case just type "x:\" in the Run box and check what is going on

If it opens rightclick that playback_automation... file and select rename - but don't type anything

Copy the file name.

Paste the name of that file into php

That way you ensure that you didn't make any typing mistakes. Always cut and paste long names (whether it is a long file name or a long database field - it just helps you avoid those silly typing mistakes that can sometimes take ages to debug)

Let me know if it worked

ramki067
02-20-2008, 09:03 AM
I did what u said. But when i typed x: in run, a window opened directly to fs-blr-03\str_audio1 folder from their i was able to go the particular file and open it.

I copied and pasted the complete path from their and still i'm getting the below error!


Warning: fopen(X:\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt) [function.fopen]: failed to open stream: No such file or directory in C:\Program Files\Apache Group\Apache2\htdocs\selected_stream.php on line 93
File cannot be opened!


My code is as below:



$filename = "X:\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt";
$handle = fopen($filename, "r");

if(!$handle){
echo "File cannot be opened!";
// exit;
}

else{
$contents = fread($handle, filesize($filename));
print $contents;

}
fclose($handle);



But when the same file is copied on to my local machine the your code works! I'm not able to figure out whats the problem!!!


Thanks,
RAmki.

deamonlizard
02-20-2008, 09:08 AM
OK, next test

Click Start->Run

Paste the entire file string into run, exactly like this

X:\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt

Notepad should immediately open the file. If not just type X: in the Run box and make 100% sure that the directory is called Automation and that the next directory is called Data_files

ramki067
02-20-2008, 09:14 AM
done and the path you mentioned is perfectly working as its opening the file in wordpad.

I'm using the local file temporarily. can you please help me on how to find the tab space in file contents? I need to extract the contents from just after first tab space to the second tab space.

Thanks,
Ramki.

mlse
02-20-2008, 10:48 AM
Hi,

Firstly, extracting data between first two tab spaces:



preg_match("/\t([\S| ]+)\t/", //Regexp pattern.
$string, //String containing the data.
$matches); //The match will be put into $matches.


Secondly, what is the result of calling stat() on the remote file?

ramki067
02-20-2008, 11:05 AM
Thanks for the reply mlse.

I added your code to my code as below:



$filename ="C:\Program Files\Apache Group\Apache2\htdocs\playback_automation_datafile_WMA_48kHz_full.txt";
$handle = fopen($filename, "r");


if(!$handle){
echo "File cannot be opened!";
// exit;
}

else{

while(!feof($handle)){
$contents = fread($handle, 1024);
preg_match("/\t([\S| ]+)\t/",$handle,$matches);
echo " $matches <br>";
}
}
fclose($handle);



and i'm getting a series of :

Warning: preg_match() expects parameter 2 to be string, resource given in C:\Program Files\Apache Group\Apache2\htdocs\selected_stream.php on line 105

What is this behaviour? What does it signifies?

Thanks,
RAmki.

mlse
02-20-2008, 12:21 PM
preg_match works on strings, not resources: http://uk3.php.net/manual/en/function.preg-match.php (hint - look at what you are actually passing in as the second parameter of preg_match).

ramki067
02-20-2008, 12:29 PM
Thanks.. can u please answer my other thread thus posted.

Ramki.

mlse
02-20-2008, 01:01 PM
Ok, what is the result of calling stat() on the remote file?

ramki067
02-21-2008, 04:15 AM
Ok, what is the result of calling stat() on the remote file?

How do i use this? I've used it as below,



$filelocation = "\\\fs-blr-03\str_audio1\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt";
print_r(stat($filelocation));

$fp = fopen($filelocation,'r');

if(!($fp)){
echo "<b>Cannot access file,$array[0]</b><br>";
}

else{
$lines = file($fp);
$i=0;
foreach($lines as $line_num => $line)
{
preg_match('/\t([-\w]+)[^\t]*\t([-\w]+)/', $line, $matches);
echo "$matches[1]<br />\n";
}
}



but getting error as below:




Warning: stat() [function.stat]: stat failed for \\fs-blr-03\str_audio1\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt in C:\Program Files\Apache Group\Apache2\htdocs\selected_stream.php on line 116
Warning: fopen(\\fs-blr-03\str_audio1\Automation\Data_files\playback_automation_datafile_WMA_48kHz_full.txt) [function.fopen]: failed to open stream: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\selected_stream.php on line 116

Notice: Undefined variable: filename in C:\Program Files\Apache Group\Apache2\htdocs\selected_stream.php on line 119
Cannot access file,

mlse
02-22-2008, 01:26 PM
Ok, try the same thing with the locally mapped path to the shared file, what happens then?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum