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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FTP

Status
Not open for further replies.

KolNedra

Programmer
Oct 5, 2001
5
NL
Is it possible to upload files to a FTP server with PHP ?
 
Here is a file that I found. It is comprehensive, and should work:

[tt]
<?

// QuickUploader
// Nagendra, nagendra@letterbox.com
// 10th Oct 2000 @ Mysore , India
// homepage : // Released under GNU GENERAL PUBLIC LICENSE //
// Directions to start cooking :)
//
// 1. start @ beginning of u r code and define a variable called $path ( ex : $path = &quot;/home/httpd/html/suitesonline.com/&quot;; )
// 2 .include the file upster.php ( ex : include ('upster.php'); )
// 3. print out a variable called $upl wherever u want to place QuickUploader code ( ex : echo &quot;$upl&quot;;
//

// Change the Below $path to point to the dir which is the file area

// $path = &quot;/var/tmp/&quot;;
// $path = &quot;C:/temp/&quot;;
$the_path = $path;

if (!$path) {
echo &quot;Path not defined or You are not authorised.&quot;;
exit;
}


if ($getty) {

// Code to Handle File Download
// Nagendra, nagendra@letterbox.com
// 20th Sept 2000 @ Mysore , India


$type=&quot;Content-type: application/cln&quot;;

// get the file &amp; download to visitor

$filename = &quot;$path$getty&quot;;
$fd = fopen ($filename, &quot;r&quot;);
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
header(&quot;$type&quot;);
echo &quot;$contents&quot;;
}

// Max File Size to be allowed...change this depending on users upload.. 100k~images/jpgs/gif/docs/etc
$my_max_file_size = &quot;102400&quot;; # in bytes

function validate_upload($the_file) {

global $my_max_file_size, $image_max_width, $image_max_height,$allowed_types,$the_file_type,$registered_types;

$start_error = &quot;\n<b>Error:</b>\n<ul>&quot;;

if ($the_file == &quot;none&quot;) {

$error .= &quot;\n<li>You did not upload anything!</li>&quot;;

} else {

list($foo,$width,$bar,$height) = explode(&quot;\&quot;&quot;,$size[3]);

if ($error) {
$error = $start_error . $error . &quot;\n</ul>&quot;;
return $error;
} else {
return false;
}
}
} # END validate_upload

function list_files() {

global $the_path;

$handle = dir($the_path);
}
function upload($the_file) {

global $the_path,$the_file_name;

$error = validate_upload($the_file);

if ($error) {
form($error);
} else { # or we can continue

// check if file exists, if then delete the old one before upload

$ffile = $the_path . '' . $the_file;
if ( file_exists(&quot;$ffile&quot;) ){
unlink (&quot;$ffile&quot;);
};
// upload the actuall file here after all checks
if (!@copy($the_file, $the_path . &quot;/&quot; . $the_file_name)) {
form(&quot;\n<b>Something barfed, check u r path &quot;);
}
}
} # END upload

// HTML upload Form Gen.

$upl = <<<close
<form name=&quot;uplink&quot; ENCTYPE=&quot;multipart/form-data&quot; action=&quot;$PHP_SELF&quot; method=&quot;post&quot;>
<b><font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;2&quot;>Quick Uploader</font></b><Br>
<INPUT NAME=&quot;the_file&quot; TYPE=&quot;file&quot; SIZE=&quot;5&quot;>
<INPUT TYPE=&quot;hidden&quot; name=&quot;task&quot; value=&quot;upload&quot;>
<input type=&quot;Submit&quot; Value=&quot;Upload&quot;>
</form>
close;

// Delete File

if ($d)
{
unlink (&quot;$path$d&quot;);
}

// Determine if file needs to be uploaded/downloaded/deleted

if ($task) {
upload($the_file);
}

if (!$title) {
$title = &quot;Files List:&quot;;
}

$upl = $upl.&quot;\n<b>$title</b><br>&quot;;
$handle = dir($path);
$upl = $upl.&quot;<table width=\&quot;28%\&quot; border=\&quot;0\&quot;>&quot;;
while ($file = $handle->read()) {
if (($file != &quot;.&quot;) &amp;&amp; ($file != &quot;..&quot;)) {
$fsize = filesize ($path.$file);
$con = <<<giveup
<tr><td width=&quot;13%&quot; height=&quot;2&quot;><a href=&quot;$PHP_SELF?getty=$file&quot; onMouseOver=&quot;drs('Size : $fsize Bytes'); return true;&quot; onMouseOut=&quot;nd(); return true;&quot;>$file</a>
</td><td width=&quot;87%&quot; height=&quot;2&quot;>
<a href=&quot;$PHP_SELF?d=$file&quot;>DELETE </a></td></tr>
giveup;
$upl = $upl.&quot;$con&quot;;
}
}
$upl = $upl.&quot;</table>&quot;;

?>
[/tt]

Hope this helps,

-Vic vic cherubini
malice365@hotmail.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top