PHP FTP SIMPLE SYSTEM


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. < ?php
  2.  
  3.  
  4. function formataLista($type,$name,$dir){
  5. $type = substr($type,0,1);
  6. if ($type == "d") {
  7. return "<a href='?dir=$dir$name'>$name";
  8. } else {
  9. return "<a href='?dir=".substr($dir,0,strlen($dir)-1)."&amp;get=$dir$name&amp;name=$name' target='_blank'>$name</a>";
  10. }
  11. }
  12.  
  13. // set up basic connection
  14.  
  15. $ftp_server = "ftp.tata.com.br";
  16. $ftp_user_name = "ftp";
  17. $ftp_user_pass = "ftp";
  18.  
  19. $conn_id = ftp_connect($ftp_server);
  20.  
  21. // login with username and password
  22. $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
  23.  
  24. // check connection
  25. if ((!$conn_id) || (!$login_result)) {
  26. echo "FTP connection has failed!";
  27. echo "Attempted to connect to $ftp_server for user $ftp_user_name";
  28. exit;
  29. }
  30.  
  31. // specif the folder to usage
  32. //$user = "/piralatopes";
  33.  
  34. $dirCurrent = $_GET['dir']."/";
  35.  
  36. if ($dirCurrent == "" or $dirCurrent == "//") {
  37. $dirCurrent = "/";
  38. }
  39.  
  40.  
  41. $dir = $user.str_replace("../","",$dirCurrent);
  42.  
  43. $tmpSearch = substr($dirCurrent,0,strlen($dirCurrent)-1);
  44. $dirPast = substr($dirCurrent,0,strrpos($tmpSearch, "/"));
  45. $tmpSearch = "";
  46.  
  47. if (ftp_chdir($conn_id, $dir)) {
  48.  
  49. if($_GET['get'] != "" and $_GET['name'] != "") {
  50. $destination_file .= ".".$user;
  51. $destination_file .= basename($_GET['get']);
  52. if (ftp_get($conn_id, $destination_file, basename($_GET['get']), FTP_BINARY)) {
  53. header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  54. header ("Content-Length: " . filesize($destination_file));
  55. header ("Content-Disposition: attachment; filename=".$_GET['name']);
  56. readfile($destination_file);
  57. unlink($destination_file);
  58. }
  59. exit();
  60. }
  61.  
  62.  
  63. ?>
  64. < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  65. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  66. <html>
  67. <head>
  68. <title>FTP</title>
  69. <style type="text/css" media="screen">
  70. body {
  71. font-family: "Trebuchet MS";
  72. }
  73. </style>
  74. </head>
  75. <body>
  76.  
  77. <h2>Arquivos na Pasta: < ?= $dirCurrent?></h2>
  78. < ?
  79.  
  80. if($_FILES['upload_file']['name'] != '') {
  81.  
  82.  
  83. $source_file = $_FILES['upload_file']['tmp_name'];
  84. $destination_file = $_FILES['upload_file']['name'];
  85. // upload the file
  86. $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
  87.  
  88. // check upload status
  89. if (!$upload) {
  90. echo "FTP upload has failed!<br />";
  91. } else {
  92. echo "Uploaded $source_file to $ftp_server as $destination_file<br />";
  93. }
  94.  
  95. }
  96.  
  97. if ($_POST['new_dir'] != "") {
  98. $newDir = $dir.$_POST['new_dir'];
  99. if (@ftp_mkdir($conn_id, $newDir)) {
  100. echo "successfully created $newDir<br />";
  101. } else {
  102. echo "There was a problem while creating $newDir<br />";
  103. }
  104. }
  105.  
  106.  
  107. $listHard = ftp_rawlist($conn_id, $dir);
  108. $listEasy = ftp_nlist($conn_id, $dir);
  109.  
  110. echo "<ul>";
  111. if ($dirCurrent != "/" and $dirCurrent != "") {
  112. echo "<li><a href='?dir=$dirPast'>..</a></li>";
  113. }
  114. $i = 0;
  115. foreach($listHard as $list) {
  116. echo "<li>".formataLista($list,$listEasy[$i],$dirCurrent)."</li>";
  117. $i++;
  118. }
  119. echo "</ul>";
  120.  
  121. ?>
  122.  
  123. <h3>Enviar Arquivos</h3>
  124.  
  125. <form action="?dir=<?= $dirCurrent?>" enctype="multipart/form-data" method="post">
  126. <input type="file" name="upload_file" id="upload_file"/>
  127. <p><input type="submit" value="Enviar &rarr;"/></p>
  128. </form>
  129.  
  130. <h3>Criar Pasta</h3>
  131.  
  132. <form action="?dir=<?= $dirCurrent?>" method="post">
  133. <input type="text" name="new_dir" id="new_dir"/>
  134. <input type="submit" value="Enviar &rarr;"/>
  135. </form>
  136.  
  137. </body>
  138. < ?
  139.  
  140. } else {
  141. header("location:?dir=/");
  142. }
  143.  
  144. // close the FTP stream
  145. ftp_close($conn_id);
  146. ?>

URL: http://renatoelias.art.br/blog/2007/04/12/sistema-simples-de-ftp-em-php-simple-system-of-ftp-in-php/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.