thomasm1948
asked on
PHP FTP Upload Help
Hi,
I am trying to create a FTP upload option for a school that utilizes a hosted content managemnt site. I tried using C# and that worked through visual studios utilizing inline coding but when I tried to use it on the CMS then it did not work. The hosting site confirmed that they do not support custom C# pages. So now I am trying to use PHP but for some reason my code just seems not to work outside of the CMS
The following is the PHP script that I would like to get working. Right now it seems to do nothing at all:
I am trying to create a FTP upload option for a school that utilizes a hosted content managemnt site. I tried using C# and that worked through visual studios utilizing inline coding but when I tried to use it on the CMS then it did not work. The hosting site confirmed that they do not support custom C# pages. So now I am trying to use PHP but for some reason my code just seems not to work outside of the CMS
The following is the PHP script that I would like to get working. Right now it seems to do nothing at all:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<?php
$ftp_server = "172.30.1.39";
$ftp_user_name = "user";
$ftp_user_pass = "password";
$destination_file = "/";
$source_file = $_FILES['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
<form action="" enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
</body>
</html>
If you want, you can also do a quick check to confirm that that's the problem. Just create a new, empty script with these two lines:
<?php
echo (function_exists("ftp_conn ect") ? "FTP support exists!" : "FTP support does not exist!");
<?php
echo (function_exists("ftp_conn
I think you want to upload a file from a client machine to the server, is that right?
ASKER
that is correct. We have an FTP site created and I am trying to see if this CMS will allow us to use PHP
ASKER
I can create a ASPX page in C# and everything works but the CMS will not let me upload C# code
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
we are using eschoolview
Did you run the test I suggested?
Have you asked their support what language their CMS is using? I can't find that detail in their web site.
http://www.eschoolview.com/School-CMS-Support.aspx
http://www.eschoolview.com/School-CMS-Support.aspx
ASKER
yes I asked, they suggested that I jus create my own upload page and then use JavaScript in their CMS to show the log in and upload information.
ASKER
I just ran this code on my Linux server and I cannot connect to the ftp server
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple FTP Upload Script using PHP by 2netlodge</title>
</head>
<body>
<?php
if(isset($_POST['ftp']))
{
$ftp = $_POST['ftp'];
$username = $_POST['username'];
$pwd = $_POST['pwd'];
$filename = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name' ];
$d = $_POST['des'];
$connect = ftp_connect($ftp)or die("Unable to connect to host");
ftp_login($connect,$userna me,$pwd)or die("Authorization Failed");
echo "Connected!<br/>";
if(!$filename)
{
echo"Please select a file";
}
else
{
ftp_put($connect,$d.'/'.$f ilename,$t mp,FTP_ASC II)or die("Unable to upload");
echo"File successfully uploaded to FTP";
}
}
?>
<form action="" method="post" enctype="multipart/form-da ta">
<input type="text" name="ftp" placeholder="FTP link"/><br/>
<input type="text" name="username" placeholder="Username"/><b r/>
<input type="password" name="pwd" placeholder="Password"/><b r/>
<input type="file" name="file" /><br/>
<input type="text" name="des" placeholder="Destination" /><br/>
<br/><input type="submit" value="Upload"/></form>
<footer>Share and get more scripts and codes like this, visit <a href="http://2netlodge.com">http://2netlodge.com</a> Forum</footer>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple FTP Upload Script using PHP by 2netlodge</title>
</head>
<body>
<?php
if(isset($_POST['ftp']))
{
$ftp = $_POST['ftp'];
$username = $_POST['username'];
$pwd = $_POST['pwd'];
$filename = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'
$d = $_POST['des'];
$connect = ftp_connect($ftp)or die("Unable to connect to host");
ftp_login($connect,$userna
echo "Connected!<br/>";
if(!$filename)
{
echo"Please select a file";
}
else
{
ftp_put($connect,$d.'/'.$f
echo"File successfully uploaded to FTP";
}
}
?>
<form action="" method="post" enctype="multipart/form-da
<input type="text" name="ftp" placeholder="FTP link"/><br/>
<input type="text" name="username" placeholder="Username"/><b
<input type="password" name="pwd" placeholder="Password"/><b
<input type="file" name="file" /><br/>
<input type="text" name="des" placeholder="Destination" /><br/>
<br/><input type="submit" value="Upload"/></form>
<footer>Share and get more scripts and codes like this, visit <a href="http://2netlodge.com">http://2netlodge.com</a> Forum</footer>
</body>
</html>
ASKER
All what I really need is for the user to put a username and password in
I figure the code should be able to point to localhost or its own ip. The FTP users that I created point to a shared home directory and they do not need to actually put a path in
I figure the code should be able to point to localhost or its own ip. The FTP users that I created point to a shared home directory and they do not need to actually put a path in
ASKER
Just running some test and I cannot connect to the ftp server from PHP. I can log in, if I go to ftp:// just fine. I would do this in .NET but I already started everything in LINUX. Please help
<?php
$un='Student';
$pw='Kilo1948';
// Connect to ftp
$conn_id = ftp_connect('localhost', 21);
// Open a session to an external ftp site
$login_result = ftp_login ($conn_id, $un, $pw);
// Check open
if (!$conn_id) {
print "FTP connection failed!";
exit();
}
if (!!$login_result) {
print "FTP login failed!";
exit();
}
if (ftp_pasv($conn_id, true) == FALSE) {
print "Passive FTP connection failed!";
exit();
}
?>
<?php
$un='Student';
$pw='Kilo1948';
// Connect to ftp
$conn_id = ftp_connect('localhost', 21);
// Open a session to an external ftp site
$login_result = ftp_login ($conn_id, $un, $pw);
// Check open
if (!$conn_id) {
print "FTP connection failed!";
exit();
}
if (!!$login_result) {
print "FTP login failed!";
exit();
}
if (ftp_pasv($conn_id, true) == FALSE) {
print "Passive FTP connection failed!";
exit();
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am not sure how to do that in centos
I ran setsebool httpd_can_network_connect= 1 and now I get passed the connection error, but the login fails even though it is correct
How can I ensure on centos 6.5 that PHP has the ftp extensions. I admit I just ran yum install php
I ran setsebool httpd_can_network_connect=
How can I ensure on centos 6.5 that PHP has the ftp extensions. I admit I just ran yum install php
ASKER
ok just ran yum install php-ftp and I got a message indicating that I am running the latest php common install
ASKER
I got it to work.
Can you say how?
ASKER
When I removed PHP, the built in PHP on centos seemed to have worked. There was some sort of confliction and I thought I had to install the PHP. Weird.... I also entered this command
httpd_can_network_connect= 1
httpd_can_network_connect=
ASKER
Thank you for all for help
You can try using a pure-PHP version of FTP, like this:
http://www.phpclasses.org/package/1743-PHP-FTP-client-in-pure-PHP.html