Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOWTO: FTP Upload and browse button form

Status
Not open for further replies.

awingnut

Programmer
Feb 24, 2003
759
US
I've seen this done on some forms but would someone please point me to an annotated example of a form that does the following?

1) Create a browse button for local files and fill in the path name, of the selected file, on a form. I know how to build forms but not how to use the windows browser (windows explorer?).

2) Create an ftp upload button that will upload the above file to an anonymous ftp server. I know the tag for download but not an upload.

Thanks.
 
You'll need server-side scripting (I can get you a PHP-script for uploading files)

Quasibobo

Don't eat yellow snow!
 
Thanks. I have php, so your script will be ideal as long as it is annotated so I can understand what it is doing.

P.S. SInce you are using php yourself perhpas you will be so kind as to answer a question:

I am trying to use printf to produce an HTML href. The href string contains spaces and I cannot seem to get the % to print:
Code:
<? php
printf (&quot;<A href=\&quot;ftp://mydomain.com/my%%32directory/%s\&quot;>&quot;,filename)
?>
This creates the link:

ftp://mydomain.com/my2directory/myfilename

I don't understand why I don't get:

ftp://mydomain.com/my%32directory/myfilename
 
Try this:

echo '<A HREF=&quot;ftp://mydomain.com/', rawurlencode ('my directory'),'&quot;>Link</a>';

And the PHP-script (>= PHP4.1) for uploading files called 'upload.php' (don't forget to CHMOD the upload-directory to 777) :


<html>
<head>
<body>
<?php
if(!isset($_POST['Submit']))
{
?>
<form enctype=&quot;multipart/form-data&quot; method=&quot;post&quot; action=&quot;upload.php&quot; name=&quot;post&quot;>
<b>Upload file</b><br><input type=&quot;file&quot; name=&quot;upload_file&quot;></p>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Upload&quot;></td></tr>
</form>
<?php
exit;
}

else
{

$upload_file = $_FILES['upload_file']['name'];
$type = $_FILES['upload_file']['type'];
$size = $_FILES['upload_file']['size'];
$temp = $_FILES['upload_file']['tmp_name'];


if (!isset($upload_file))
{
$kbsize = round($size/1000);
$size_limit = &quot;500&quot;; // set size limit in bytes

if ($kbsize < $size_limit)
{
move_uploaded_file($temp,&quot;uploads/&quot;.$upload_file);
echo &quot;Uploading <b>$upload_file</b> ($kbsize kilobytes) was succesfull.<p><p>The path to this file is:</b> href=\&quot;upload.php\&quot;>Upload another file<a>&quot;;
}
else
{
echo &quot;<p>Sorry, the file is<b>$kbsize kilobytes</b>,making it bigger than the limit of <b>$size_limit kilobytes</b></p><a href=\&quot;javascript:history.back()\&quot; onmouseover=\&quot;window.status='Back'; return true;\&quot;onmouseout=\&quot;window.status=''; return true;\&quot;>Back</a>&quot;;
exit;
}
}
else
{
echo &quot;<p><b>Nothing selected</b></p><a href=\&quot;javascript:history.back()\&quot; onmouseover=\&quot;window.status='Back'; return true;\&quot;onmouseout=\&quot;window.status=''; return true;\&quot;>Back</a>&quot;;
exit;
}
}
?>
</body>
</html>

Don't eat yellow snow!
 
I use this script on regular http-servers, not on FTP..... I don't know if it's working for FTP aswell

Don't eat yellow snow!
 
I'm running OpenSA on a PC and using Cerberus FTP server. I think I follow most of this except how to specify the URL of the FTP server (if that will work). Is there not a URL of the form 'ftp://ftphos.mydomain.com/....' that will execute and anonymous ftp upload? If so, I assume it has to go on the &quot;Upload&quot; button somehow.

Thanks again.
 
Please disregard the prevous. I wasn't understanding what 'moved_uploaded_file' did until I read up on it. I don't need the ftp server for this operation since htey will always be on the same machine.

Since I'm not running UNIX, I don't think I need chmod. I need to examine OpenSA's config file as I think that is where I need to set the permissions.

However, I cannot get that far. I'd be happy if it failed with a write permission error. I appears that $upload_file is not getting passed in the post. I get 'nothing selected' when I click 'Upload'. I verified that $upload_file is set properly. Any ideas?

Thanks.
 
Problem solved. I guess great minds fumble alike. :)

It took another pair of eyes to spot it but:
Code:
if (
!
Code:
isset($upload_file))
should be:
Code:
if (isset($upload_file))
The script works fine now. Thanks.
 
My mistake! It should have been !empty($upload_file)

Don't eat yellow snow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top