PHP FTP_PUT上传到目录

前端之家收集整理的这篇文章主要介绍了PHP FTP_PUT上传到目录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我自己从一本名为“ PHP完整参考 – PHP5.2”的书中学习PHP
我目前在第11章FTP,上传,删除,makedir等,但遇到了本书未涵盖的几个问题:

根据我的教科书,这是一个上传到服务器的简单代码

  1. $connect=ftp_connect("johnsite.com");
  2. $result=ftp_login($connect,"john@johnsite","johnnyWalker");
  3. if(!$result){
  4. echo'Could not connect to Server';
  5. }
  6. $result=ftp_put($connect,'myFile.PHP',FTP_ASCII);
  7. echo'UPLOADING FILE......';
  8. if($result){
  9. echo'File Uploaded!';
  10. }

我的问题:

>上传到哪个目录,如果我要上传到目录,我将如何修改代码说public_html / images / myFile.jpg
>在示例中myFile.PHP是硬编码的,如果我想要用户选择要上传文件怎么办?我假设你可以做这样的事情是正确的:

  1. <input type="file" name="myFile" value="upload a file" />
  2. <input type="submit" name="upload" />
  3.  
  4. if(isset($_POST['upload'])){
  5. $fileName=$_POST['myFile']; //file is now assigned to var name
  6. $result=ftp_put($connect,$fileName,FTP_ASCII); //file linked to var name being uploaded
  7. }

这是最有效的安全方式吗?

谢谢你的阅读

由于@Bonner表示Fabien的答案不正确,因为您正在寻找一个脚本来将文件从您网站上的页面上传到服务器.

首先要记住的是,ftp_put()功能将永远覆盖现有的文件.相反,我建议您查看PHP move_uploaded_file

这是表格.在action属性中,我们指定一个文件,它将处理和处理所有文件.您需要使用表单的enctype属性的multipart / form-data值.

我几乎无处不在,更好的理解.

  1. <form action="upload.PHP" method="post" enctype="multipart/form-data">
  2. File: <input type="file" name="upload-file" size="30" />
  3. <input type="submit" name="submit" value="Upload file" />
  4. </form>

upload.PHP

  1. <?PHP
  2. // Used to determinated if the upload file is really a valid file
  3. $isValid = true;
  4. // The maximum allowed file upload size
  5. $maxFileSize = 1024000;
  6. //Allowed file extensions
  7. $extensions = array('gif','jpg','jpeg','png');
  8.  
  9. // See if the Upload file button was pressed.
  10. if(isset($_POST['submit'])) {
  11. // See if there is a file waiting to be uploaded
  12. if(!empty($_FILES['upload-file']['name'])) {
  13.  
  14. // Check for errors
  15. if(!$_FILES['upload-file']['error']) {
  16. // Renamed the file
  17. $renamedFile = strtolower($_FILES['upload-file']['tmp_name']);
  18.  
  19. // Get the file extension
  20. $fileInfo = pathinfo($_FILES['upload-file']['name']);
  21.  
  22. // Now vaidate it
  23. if (!in_array($fileInfo['extension'],$extensions)) {
  24. $isValid = false;
  25. echo "This file extension is not allowed";
  26. }
  27.  
  28. // Validate that the file is not bigger than 1MB
  29. if($_FILES['upload-file']['size'] > $maxFileSize) {
  30. $isValid = false;
  31. echo "Your file's size is to large. The file should not be bigger than 1MB";
  32. }
  33.  
  34. // If the file has passed all tests
  35. if($isValid)
  36. {
  37. // Move it to where we want it to be
  38. move_uploaded_file($_FILES['upload-file']['tmp_name'],'uploads/'.$renamedFile);
  39. echo 'File was successfully uploaded!';
  40. }
  41. }
  42. // If there is an error show it
  43. else {
  44. echo 'There was an error file trying to upload the file: '.$_FILES['upload-file']['error'];
  45. }
  46. }
  47. }

猜你在找的PHP相关文章