kev@num Posted September 5, 2010 Share Posted September 5, 2010 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 More sharing options...
nethnet Posted September 5, 2010 Share Posted September 5, 2010 Check your php.ini file for the setting of allow_url_fopen. allow_url_fopen on PHP.net Link to comment https://forums.phpfreaks.com/topic/212597-fopen-help/#findComment-1107523 Share on other sites More sharing options...
rwwd Posted September 5, 2010 Share Posted September 5, 2010 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 More sharing options...
kev@num Posted September 5, 2010 Author Share Posted September 5, 2010 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 More sharing options...
kev@num Posted September 5, 2010 Author Share Posted September 5, 2010 oO I tried echoing the variables again, but the second one seems to be coming out as: ftp://:@ with all the details missing. for some reason, it's not remembering them further down my code!? (ps allow_url_fopen is turned on) Link to comment https://forums.phpfreaks.com/topic/212597-fopen-help/#findComment-1107585 Share on other sites More sharing options...
kev@num Posted September 5, 2010 Author Share Posted September 5, 2010 sweet, thanks for your help, i have now made my variable global and the second echo now comes out ok!!! Thanks again for your time Link to comment https://forums.phpfreaks.com/topic/212597-fopen-help/#findComment-1107588 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.