Page 1 of 1
ftpconnect weird...
Posted: Mon Jul 20, 2009 9:07 am
by johniem
I'm posting this topic because i have noticed something weird to my code and i can't trace it.
I have this code..
Code: Select all
<?php
session_start();
set_time_limit(600);
ini_set('default_socket_timeout', 5);
header('Content-Type: text/html; charset=utf-8');
$ftpstream = @ftp_connect(ftpUrl) or die('<h1>Error could not connect!!!</h1>');
$login = @ftp_login($ftpstream, user, pass) or die('<h2>Error could not login!!!</h2>');
$action = $_POST['action'];
if(action='newImg'){
upload_img();
}
function upload_img(){
///some code here//
}
?>
The function just uploads an img..The problem is that this code doesn't work (no errors no warnings nothing...just blank completed page) because the $ftpstream and $login is outside
the function..If i move them in then all is working fine..
Any ideas why is this happening??
Re: ftpconnect weird...
Posted: Mon Jul 20, 2009 11:58 am
by McInfo
If you want to see error messages, remove the
error control operator (@).
Use BBCode tags when posting.
[/code]
Edit: This post was recovered from search engine cache.
Re: ftpconnect weird...
Posted: Mon Jul 20, 2009 2:22 pm
by califdon
Is the code that you posted in an include file, perhaps? Since I don't see where you call the function. As McInfo said, the first thing to do when you are having a problem is to remove the @ character that blocks diagnostic messages.
Re: ftpconnect weird...
Posted: Tue Jul 21, 2009 2:34 am
by johniem
Sorry for not using BBCode tags..
I have tried ftp_connect without @ but i was getting the same result..
I'm calling the function right after a $_POST[] where i'm getting a passed variable
and using it to call the function depending the passed value , always in the same php script.
This is not causing me any problem but i found it weird and i couldnt trace the problem even when i remove the @
or filling the code with echos to watch the code flow.
Re: ftpconnect weird...
Posted: Tue Jul 21, 2009 11:43 am
by McInfo
In addition to removing the ampersand, add these two lines to your script to make errors visible.
Code: Select all
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
I expect this might make a "Notice: Undefined variable" error visible in your current code.
johniem wrote:because the $ftpstream and $login is outside the function..If i move them in then all is working fine..
If you declare a variable globally (outside a function), it is not automatically available within a function. Only superglobals ($_SERVER, $_POST, $GLOBALS, etc.) are automatically available to functions.
Option 1: Pass the variables as arguments.
Code: Select all
<?php
$a = 3;
$b = 4;
echo add($a, $b);
function add ($x, $y)
{
return $x + $y;
}
?>
Option 2: Allow the function to access the global variables by using the "global" statement.
Code: Select all
<?php
$a = 3;
$b = 4;
echo add();
function add ()
{
global $a, $b;
return $a + $b;
}
?>
Option 3: Use the superglobal array $GLOBALS.
Code: Select all
<?php
$a = 3;
$b = 4;
echo add();
function add ()
{
return $GLOBALS['a'] + $GLOBALS['b'];
}
?>
Option 4: Make a class so you can use $this.
Code: Select all
<?php
class C
{
var $a;
var $b;
function C ()
{
$this->a = 3;
$this->b = 4;
echo $this->add();
}
function add ()
{
return $this->a + $this->b;
}
}
new C;
?>
Edit: This post was recovered from search engine cache.
Re: ftpconnect weird...
Posted: Tue Jul 21, 2009 3:00 pm
by johniem
Very usefull infos McInfo

.. espessially the error prints....
You see i am mainly java programmer and i'm stuck with this project in php trying to figure out the differences between these two languages.
Thanks a lot...
Re: ftpconnect weird...
Posted: Tue Jul 21, 2009 6:20 pm
by califdon
Yeah, McInfo, nice collection of examples, indeed.
And, johnniem, Good luck with bridging the distance between Java and PHP. If we can help, let us try. Just remember to mention that, so we can skip the absolute-beginning-programmer comments that we often feel we have to make. Wouldn't want to offend you.
Re: ftpconnect weird...
Posted: Wed Jul 22, 2009 2:28 am
by johniem
Don't worry califdon, I can't be offended so easy:)
Php was in my plans for extending my programming knowledge and it's really nice formed and not very far from java.
I have noticed that with php i can do some routine web procedures easier than java.
Re: ftpconnect weird...
Posted: Wed Jul 22, 2009 1:52 pm
by califdon
johniem wrote:Don't worry califdon, I can't be offended so easy:)
Php was in my plans for extending my programming knowledge and it's really nice formed and not very far from java.
I have noticed that with php i can do some routine web procedures easier than java.
Definitely true. Not to say that PHP can do everything that Java can, but for many (most?) real life applications, PHP is probably a faster way to get to the finish line with a perfectly satisfactory product.