Zend - The PHP Company




Miscellaneous

Add Code


Simple FTP Upload  

Type: code fragment
Added by: fath
Entered: 25/04/2001
Last modified: 04/12/2000
Rating: *** (4 votes)
Views: 12330
Simple script to upload files via FTP. You can define destination directories by scroll-down menu, maximum file size and allowed file extensions. Originally written to allow clerks uploading zip files into certain restriced areas of my website.


/*
There are two files: upload.php and test.php
The former contains code to manage file upload, the latter is just an HTML demo.
*/

// upload.php

<?
/* This code is released with NO WARRANTY, use at your own
risk!
*/

function putFiles ($login,$password) {
  global 
$filename$dir$MAX_FILE_SIZE;
  
$backstr "<br><a href="javascript:history.back(-1)">Back</a>";

// An array with allowed file extensions
  
$allExt = array("zip","Zip","ZIP");
  
$fileSize filesize($filename);
  if (
$dir == "notset") {
    die(
"ERROR - Choose a file type.$backstr");
  }
  if (
$fileSize $MAX_FILE_SIZE) {
    die(
"File size exceeds $MAX_FILE_SIZE bytes.$backstr");
  }
/* This is to isolate filename from path. Chosen separator
"" is intended for Windows clients. Change in "/" if you're
running on Unix-like operating systems.
*/

  
$chunk_file explode("\", $filename);
  
$numChunkFile = count($chunk_file);
  
$dstFile = $chunk_file[$numChunkFile - 1];

// Now let's get file extension
  
$ext = explode(".",$dstFile);
  
$numChunkExt = count($ext);

/* If file extension matches elements in array 
$allExt
FTP connection is extablished and file is uploaded.
*/
  if (in_array(
$ext[$numChunkExt - 1],$allExt)) {
    
$ftp_id = ftp_connect("192.168.0.254");
    
$ftp_login = ftp_login($ftp_id,$login,$password);
    if (! 
$ftp_login) {
      ftp_quit(
$ftp_id);
      die("
Connection failedcheck your login and password.$backstr");
    }
    if (ftp_chdir(
$ftp_id,$dir)) {
      
$ftp_put_file = ftp_put($ftp_id,$dstFile,$filename,FTP_BINARY);
      if (! 
$ftp_put_file) {
        echo "
Upload failed.$backstr";
      }
      else {
        echo "
Upload $dstFile was successful.n";
      }
    }
  }
  else {
    echo "
ERROR Allowed file extensions are:<br>n";
    for (
$i = 0; $i < count($allExt); $i++) {
      echo "
<b>$allExt[$i]</b><br>n";
    }
    echo 
$backstr;
  }
  ftp_quit(
$ftp_id);
}

?>


// test.php

<html>
<head></head>

<body bgcolor="
#ffffff" text="#000000">

<form action="<? echo $PHP_SELF; ?>" method="POST">
  <
input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<
input type="hidden" name="action" value="upload">
  <
br>
<!-- 
FTP Server username and password -->
  
Login:
  <
input type="text" name="login" size="20" maxlength="8">
  <
br>
  
Password:
  <
input type="password" name="password" size="20" maxlength="8">
  <
br>

<!-- 
File typeto choose destination directory -->

  
File type:
  <
select name="dir">
    <
option value="notset" selected>...</option>
    <
option value="soci/circolari">Circolari (soci)</option>
    <
option value="soci/guide">Guide (soci)</option>
    <
option value="soci/files">Files generici (soci)</option>
    <
option value="circolari">Circolari (pubbliche)</option>
    <
option value="files">Files generici (pubblici)</option>
  </
select>
  <
br>
  
Filename :
  <
input type="file" name="filename">
<
br><input type="submit" value="Invia">
</
form>

<?

if (isset(
$action) and $action == "upload") {

include(
"upload.php");
putFiles($login,$password);

}
?>

</body></html>


Usage Example


Look at test.php


Rate This Script





Search



This Category All Categories