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:password@ftp.mydomain.com/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
Share on other sites

Hi there kev@num,

 

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

 

//$myftppath = "ftp://me:password@ftp.mydomain.com/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:password@ftp.mydomain.com/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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.