• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Help with ftp upload

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Shakka

Member
Joined
Jul 15, 2007
Location
St. Clair Shores, Michigan
To start, i have gotten an ftp server up and running via xp pro's IIS utility. In addition to this server i would like a few integrated within it, which i have no clue on how to go about doing.

1) How do i make it so users can connect to the ftp server through a DNS name i believe it is called? (ex. ftp://blahblah/ instead of ftp://123.123.1.1/)

2) Is there a way to add an upload box so a user can upload a file to the ftp server without a 3rd party program such as filezilla or flashfxp? (sort of like the upload box on a site such as rapidshare)
 
To start, i have gotten an ftp server up and running via xp pro's IIS utility. In addition to this server i would like a few integrated within it, which i have no clue on how to go about doing.

1) How do i make it so users can connect to the ftp server through a DNS name i believe it is called? (ex. ftp://blahblah/ instead of ftp://123.123.1.1/)

2) Is there a way to add an upload box so a user can upload a file to the ftp server without a 3rd party program such as filezilla or flashfxp? (sort of like the upload box on a site such as rapidshare)

1. Use a service such as dyndns.org. You'll choose a hostname, username,
and password, and can update your IP through a web browser interface.
My dlink router handles this for me.

2. Yes, that is possible, you'll need a website that can run PHP scripts.
 
Alright thanks. If you know how, could you possibly guide me through setting up a site with a php script to do this? Its running off my other machine so there are no limit to access. Its running Windows xp professional 64 bit if that helps at all. Thanks.
 
Alright thanks. If you know how, could you possibly guide me through setting up a site with a php script to do this? Its running off my other machine so there are no limit to access. Its running Windows xp professional 64 bit if that helps at all. Thanks.

I thought you'd never ask. :)
Give me a day or so to modify some existing code and test it.
Then I will post it here.
 
Last edited:
Alright thanks. If you know how, could you possibly guide me through setting up a site with a php script to do this? Its running off my other machine so there are no limit to access. Its running Windows xp professional 64 bit if that helps at all. Thanks.

First you'll have to install an http server (ie. Apache), then you'll have to install php. There's a guide here: http://www.ocforums.com/showthread.php?t=272080

I wrote a small ajax server console (works in FF, dunno about anything else) that lets me upload files and execute php code remotely (all secured by a password). It's two files:

console.php
Code:
<?php
session_start();
$error = "";

if ( array_key_exists("log", $_REQUEST) && $_REQUEST["log"] == 1 )
{
  $_SESSION = array();

  if ( isset($_COOKIE[session_name()]) )
    setcookie(session_name(), '', time()-42000, '/');

  session_destroy();
  header('Location: console.php');
}
function formatSize($size,$suff){
    switch (true){
    case ($size > 1099511627776):
        $size /= 1099511627776;
        $suffix = 'TB';
    break;
    case ($size > 1073741824):
        $size /= 1073741824;
        $suffix = 'GB';
    break;
    case ($size > 1048576):
        $size /= 1048576;
        $suffix = 'MB';   
    break;
    case ($size > 1024):
        $size /= 1024;
        $suffix = 'KB';
        break;
    default:
        $suffix = 'B';
    }
    return $suff ? round($size, 2).$suffix : round($size, 2);
}
$space = formatSize(diskfreespace("C:"),false) . "/" . formatSize(disk_total_space("C:"),true) . " free";
?>

<html>
<head>
<title>Server Console</title>
<script language="JavaScript" type="text/javascript">
function togglevis(obj_tog)
{
 if ( obj_tog.style.display == "none" )
   obj_tog.style.display = "block";
 else
   obj_tog.style.display = "none";
}

function php_eval(str_code,obj_result)
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4 || xmlHttp.readyState==3)
      {
      obj_result.innerHTML=xmlHttp.responseText;
      }
    }

  str_fcode = "code=" + encodeURIComponent(str_code);

  xmlHttp.open("POST","console_processor.php",true);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
  xmlHttp.setRequestHeader("Content-length", str_fcode.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(str_fcode);

}
function validateupload()
{
  if ( document.upload.fileloc.value == "" )
  {
    alert("Please select a file")
  }  
  else
  {
    document.upload.submit()
  }
}
</script>

</head>

<body>

<!--Upload box-->
<div id="uploadbox" style="display:none; position:absolute; left:25px; top:33px; background:white; border-style: solid; border-width:1px; border-color: black; padding: 5px">
  Upload:<br />
  <form action="console.php?upl=1" method="POST" name="upload" enctype="multipart/form-data">
  <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
  <input type="file" name="fileloc"> <input type="button" value="Upload!" onClick="validateupload()">
  </form>
</div>

<table border=0 width=100% cellpadding=0 cellspacing=0>
  <tr>
    <td align=left width=50%><a href="javascript:togglevis(document.getElementById('uploadbox'))"><?php echo $space; ?></a></td>
    <td align=right width=50%><a href="console.php?log=1">Logout</a></td>
  </tr>
  <tr>
    <td align=center colspan=2 width=100%><strong style="font-size:24pt">Server Console</strong></td>
  </tr>
</table>

<br />

<div align="center">
<?php
if ( array_key_exists("pw", $_REQUEST) )
{
  //Login
  if ( md5($_REQUEST["pw"]) == "<INSERT AN MD5'd PASSWORD HERE>" )
    $_SESSION["pw"] = "<INSERT AN MD5'd PASSWORD HERE>";

  else
    $error = "Incorrect Password<br />";

}


if ( !isset($_SESSION["pw"]) )
{

  echo $error;

?>

  <form method="post" action="console.php">
    <input type="password" name="pw">
    <input type="submit" value="Login">
  </form>

<?

}

else
{

  if ( $_SESSION["pw"] != "<INSERT AN MD5'd PASSWORD HERE>" )
    die("Invalid Session");

  if ( array_key_exists("upl", $_REQUEST) && $_REQUEST["upl"] == 1 )
  {

    $uploads_dir = 'files';
 
    $uplsuc = false;
    if ($_FILES["fileloc"]["error"] == UPLOAD_ERR_OK && $_FILES["fileloc"]["size"] < 10000000)
    {

        $tmp_name = $_FILES["fileloc"]["tmp_name"];
        $name = $_FILES["fileloc"]["name"];
        $uplsuc = move_uploaded_file($tmp_name, "$uploads_dir\\$name") ? true : false;

    }

    if ( !$uplsuc )
      echo "Upload failed<br />";

  }

?>
<a href="javascript:togglevis(document.getElementById('codebox'))">Show/Hide Code</a>
<table border=0 width=100% height=500>
  <tr align=center>
   <td align=left valign=top>
    <div id="codebox" style="display: block">
    <form onSubmit="php_eval(this.code.value,document.getElementById('result')); return false" action="console.php#" method="post">
    <strong>PHP:</strong>
    <br />	
    <textarea name="code" cols=50 rows=20></textarea>
    <input type="hidden" name="codetype" value="php">
    <br />
    <input type="Submit" value="Go!">
    </form>
    </div>
   </td>

   <td valign=top width=100%>
     <div id="result"></div>
    </td>
  </tr>
</table>

<?php
}
?>

</div>
</body>
</html>

The only thing you'd have to change is MD5-ing a password and put it where noted. The second file just runs the code:

console_processor.php
Code:
<?php
session_start();

if ( array_key_exists("code", $_REQUEST) )
{
  if ( !array_key_exists("pw", $_SESSION) || $_SESSION["pw"] != "<INSERT MD5'd PASSWORD HERE>" )
    die("Bad session");
  else
    eval($_REQUEST["code"]);
}

?>

The console shows the free space, total space on the system drive ( C: ), if you click that it opens an upload box, and there's a spot to run php code.

If you just want the upload part, the code you need is here:
Code:
<html>

<head>
<script type="text/javascript" language="JavaScript">
function validateupload()
{
  if ( document.upload.fileloc.value == "" )
  {
    alert("Please select a file")
  }  
  else
  {
    document.upload.submit()
  }
}
</script>

</head>

<?php

  if ( array_key_exists("upl", $_REQUEST) && $_REQUEST["upl"] == 1 )
  {

    //This sets the upload directory relative to the location of this file
    $uploads_dir = 'files';
 
    $uplsuc = false;
    if ($_FILES["fileloc"]["error"] == UPLOAD_ERR_OK && $_FILES["fileloc"]["size"] < 10000000)
    {

        $tmp_name = $_FILES["fileloc"]["tmp_name"];
        $name = $_FILES["fileloc"]["name"];
        $uplsuc = move_uploaded_file($tmp_name, "$uploads_dir\\$name") ? true : false;

    }

    if ( !$uplsuc )
      echo "Upload failed<br />";

  }

?>

<body>
  Upload:<br />
  <form action="console.php?upl=1" method="POST" name="upload" enctype="multipart/form-data">
  <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
  <input type="file" name="fileloc"> <input type="button" value="Upload!" onClick="validateupload()">
  </form>
</body>

</html>

The problem with the second example is anyone can upload any file, so there's a security issue, but if you don't give out your ip/domain it might not be a problem. I still vote the first one :)

It's been a long time since I've coded anything so there's probably room for improvement. Constructive criticism is welcomed.
 
upload.html
Code:
<html>
<head>
<title>Example Upload HTML</title>
<meta http-equiv="Content-Type" content="text/html; 
charset=iso-8859-1">
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>User name </td>
<td><input name="username" type="text" id="username"></td>
<td> </td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="password" id="password"></td>
<td> </td>
</tr>
<tr>
<td>File name </td>
<td><input type="file" name="file"></td>
<td> </td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Upload"></td>
<td> </td>
<td> </td>
</tr>
</table> 
</form>
</body>
</html>

upload.php
Code:
<html>
<head>
<title>Example Upload PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$ftp_server = "your FTP server address here";
$ftp_user_name=$_POST['username'];
$ftp_user_pass=$_POST['password'];

$uploaddir = 'path to an uploads folder here';
// example '/home/smartcam/public_html/UPLOAD/'
// where UPLOAD is a folder I had to create in my www home directory

$uploadfile = $uploaddir . basename($_FILES['file']['name']);

echo "Receiving file as $uploadfile <BR>";

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
     echo "File received successfully<BR>";
}
else
{
     echo "Error! No file was received<BR>";
     exit(0);
}

$destination_file=basename($uploadfile);
echo "Uploading file as $destination_file <BR>";

//make a connection to the ftp server 
$conn_id = ftp_connect($ftp_server);
if($conn_id)
  echo "Connected to $ftp_server<BR>";

// login with username and password 
$login_result = ftp_login($conn_id , $ftp_user_name , $ftp_user_pass); 
if($login_result)
  echo "Logged into $ftp_server as $ftp_user_name <BR>";

// check connection 
if((!$conn_id)||(!$login_result))
{ 
	echo "FTP connection has failed!<BR>" ; 
	echo "Attempted to connect to $ftp_server for user $ftp_user_name<BR>" ; 
	unlink($uploadfile);
	exit(0); 
}

// upload the file 
//$upload = ftp_put($conn_id,$destination_file,$uploadfile,FTP_ASCII ); 
$upload = ftp_put($conn_id,$destination_file,$uploadfile,FTP_BINARY ); 

// check upload status 
if(!$upload)
{ 
	echo "FTP upload has failed!<BR>" ; 
}
else
{ 
	echo "Uploaded $uploadfile to $ftp_server as $destination_file<BR>" ; 
}

// close the FTP stream 
ftp_close($conn_id);
echo "Closed<BR>";

unlink($uploadfile);

?> 
</body>
</html>

Yields the example output:

Receiving file as /home/smartcam/public_html/UPLOAD/Test.txt
File received successfully
Uploading file as Test.txt
Connected to ftp.smart-cam-mv.com
Logged into ftp.smart-cam-mv.com as [email protected]
Uploaded /home/smartcam/public_html/UPLOAD/Test.txt to ftp.smart-cam-mv.com as Test.txt
Closed
 
upload.html
Code:
<html>
<head>
<title>Example Upload HTML</title>
<meta http-equiv="Content-Type" content="text/html; 
charset=iso-8859-1">
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>User name </td>
<td><input name="username" type="text" id="username"></td>
<td> </td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="password" id="password"></td>
<td> </td>
</tr>
<tr>
<td>File name </td>
<td><input type="file" name="file"></td>
<td> </td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Upload"></td>
<td> </td>
<td> </td>
</tr>
</table> 
</form>
</body>
</html>

upload.php
Code:
<html>
<head>
<title>Example Upload PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$ftp_server = "your FTP server address here";
$ftp_user_name=$_POST['username'];
$ftp_user_pass=$_POST['password'];

$uploaddir = 'path to an uploads folder here';
// example '/home/smartcam/public_html/UPLOAD/'
// where UPLOAD is a folder I had to create in my www home directory

$uploadfile = $uploaddir . basename($_FILES['file']['name']);

echo "Receiving file as $uploadfile <BR>";

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
     echo "File received successfully<BR>";
}
else
{
     echo "Error! No file was received<BR>";
     exit(0);
}

$destination_file=basename($uploadfile);
echo "Uploading file as $destination_file <BR>";

//make a connection to the ftp server 
$conn_id = ftp_connect($ftp_server);
if($conn_id)
  echo "Connected to $ftp_server<BR>";

// login with username and password 
$login_result = ftp_login($conn_id , $ftp_user_name , $ftp_user_pass); 
if($login_result)
  echo "Logged into $ftp_server as $ftp_user_name <BR>";

// check connection 
if((!$conn_id)||(!$login_result))
{ 
	echo "FTP connection has failed!<BR>" ; 
	echo "Attempted to connect to $ftp_server for user $ftp_user_name<BR>" ; 
	unlink($uploadfile);
	exit(0); 
}

// upload the file 
//$upload = ftp_put($conn_id,$destination_file,$uploadfile,FTP_ASCII ); 
$upload = ftp_put($conn_id,$destination_file,$uploadfile,FTP_BINARY ); 

// check upload status 
if(!$upload)
{ 
	echo "FTP upload has failed!<BR>" ; 
}
else
{ 
	echo "Uploaded $uploadfile to $ftp_server as $destination_file<BR>" ; 
}

// close the FTP stream 
ftp_close($conn_id);
echo "Closed<BR>";

unlink($uploadfile);

?> 
</body>
</html>

Yields the example output:

Receiving file as /home/smartcam/public_html/UPLOAD/Test.txt
File received successfully
Uploading file as Test.txt
Connected to ftp.smart-cam-mv.com
Logged into ftp.smart-cam-mv.com as [email protected]
Uploaded /home/smartcam/public_html/UPLOAD/Test.txt to ftp.smart-cam-mv.com as Test.txt
Closed
That's really nifty. I didn't know you can use php to connect to an ftp server. Although if your ftp server is on the same machine as your php/http server, it's a little redundant.
 
That's really nifty. I didn't know you can use php to connect to an ftp server. Although if your ftp server is on the same machine as your php/http server, it's a little redundant.

Yes, that's true.

If the file could be put into it's proper place directly by the call
to move_uploaded_file that would definitely be more efficient.

The nice thing about using ftp though is that it retains the security
associated with the user and their home directory structure, ensuring
that any files uploaded are not allowed to be stored somewhere else,
where they are not supposed to be :) or overwriting any other user's
files.
 
Some more questions (probably expected). :)

After using no-ip the site works when i use the format site.blahblah.com, but not with www.site.blahbah.com. Anyway to make it work with www. attached?

Also, how do you make it add a timestamp to the end of each file that is uploaded?

When uploading a file, small files such as txt document or picture will work fine, but when i try to upload a 33 mb audio file, it times out. How can i fix this?
 
Last edited:
Some more questions (probably expected). :)

After using no-ip the site works when i use the format site.blahblah.com, but not with www.site.blahbah.com. Anyway to make it work with www. attached?

Also, how do you make it add a timestamp to the end of each file that is uploaded?

When uploading a file, small files such as txt document or picture will work fine, but when i try to upload a 33 mb audio file, it times out. How can i fix this?

I think because your domain is a subdomain of the free hosting service, you can't attach the www. You can always buy a domain and get the www. They're relatively cheap (check godaddy.com).

For a timestamp, how do you want it added? You could prepend it to the file name by using the date() function and adding it to the filename:

Code:
<?php
//Check  http://us.php.net/date for the syntax for the function

// set the default timezone to use. Available since PHP 5.1
//set this to whatever timezone you're in. I believe if you don't do this it will default to the system's time zone so you can probably leave it out
date_default_timezone_set('UTC');

//This is something like 03-25-09-13:44
$ts = date('m-j-y-G:i');

//Now just add the $ts timestamp to the beginning of the file name in the move_uploaded_file function. "." is the concatenation operator

//In my code:
move_uploaded_file($tmp_name, "$uploads_dir\\$ts" . $name)

//In bz2klag's code, you'd modify the $uploadfile variable which is passed to the move_uploaded_file function two lines down
$uploadfile = $uploaddir . $ts . basename($_FILES['file']['name']);

?>
(If you want the time-stamp at the end, you can use the substr() function to extract just the file extension then do $filename . $ts . $fileextension)

For uploads timing out, I believe php has a maximum file-size limit set in the .ini config file. This may be your problem. You can set a limit in the html form, but there's also a hard-coded php config setting that overrides everything.

If it's not timing out at the php part, it might be an issue with your ftp server (if you're using php's ftp connection). If you have an ftp server where you can look at a console, see if it has any problems once the ftp upload starts. I recommend FileZilla ftp server. It's open source and has a lot of features (including a log).
 
Back