Jump to content

fopen() help


kev@num

Recommended Posts

I'm having trouble connecting through fopen using variables as the login details, but with file_get_content it works fine?!

 

These are my variables:-

 

$ftpaddy="ftp.mydomain.com";
$username="me";
$password="password";

$editingdir="/public_html/edit/";
$editfile="editme.txt";


$myftppath = "ftp://" . $username . ":" . $password . "@" . $ftpaddy . $editingdir . $editfile; //puts all variables into string

 

When I try to connect and read the file using the following method it works fine:-

 

$file = file_get_contents("$myftppath", "r");

 

but when i try and use the same string with fopen as below it doesn't work?!

 

the line i have commented out however works and when i echo the $myftppath variable below it looks the same.. i don't really get it!

 

 

	//$myftppath = "ftp://me:[email protected]/public_html/edit/editme.txt";
	$myftppath = "ftp://" . $username . ":" . $password . "@" . $ftpaddy . $editingdir . $editfile; //puts all variables into string
	$stream_options = array('ftp' => array('overwrite' => true));
	$stream_context = stream_context_create($stream_options);

	if ($fh = fopen($myftppath, 'w', 0, $stream_context)){//fopen only likes the commented out $myftppath not the one using the variables?!
		fputs($fh, $newcontents);
		fclose($fh);
		} else {
		die('Could not open file.');		
	}

 

does anyone know if I am doing the syntax wrong and how to correct it? I read I may need to put another slash after domain.com but this didn't help either.

any help would be much appreciated!

thanks in advance :)

Kev.

Link to comment
https://forums.phpfreaks.com/topic/212597-fopen-help/
Share on other sites

Hi there kev@num,

 

Have you echoed the string to see if all is as expected?

 

//$myftppath = "ftp://me:[email protected]/public_html/edit/editme.txt";
$myftppath = "ftp://" . $username . ":" . $password . "@" . $ftpaddy . $editingdir . $editfile; //puts all variables into string

 

try doing this to see if it comes the same when you echo both to screen:-

 

$one = "ftp://me:[email protected]/public_html/edit/editme.txt";
$two = "ftp://".$username.":".$password."@".$ftpaddy.$editingdir.$editfile;

echo $one;
echo "<br>";
echo $two;

 

See if they are the same, then, you should get an idea of where the error is if the second is in any way different to the first..

 

Cheers,

Rw

Link to comment
https://forums.phpfreaks.com/topic/212597-fopen-help/#findComment-1107548
Share on other sites

Hi rwwd, thanks for your advice, i have tried to echo the two but they come out exactly the same. I will try allow_url_fopen and see if that works :)

 

it's wierd because $one = "ftp://me:[email protected]/public_html/edit/editme.txt"; works

but $two = "ftp://".$username.":".$password."@".$ftpaddy.$editingdir.$editfile; doesn't!

Link to comment
https://forums.phpfreaks.com/topic/212597-fopen-help/#findComment-1107578
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.