PHP FTP_PUT 上传到目录

标签 php ajax file-upload

我是从一本名为“PHP the Complete Reference - PHP5.2”的书上自学PHP的 我目前在第 11 章 FTP、上传、删除、makedir 等,但遇到了一些本书未涵盖的问题:

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

$connect=ftp_connect("johnsite.com");
$result=ftp_login($connect,"john@johnsite","johnnyWalker");
if(!$result){
echo'Could not connect to Server';  
}
$result=ftp_put($connect,'myFile.php',FTP_ASCII);
echo'UPLOADING FILE......';
if($result){
    echo'File Uploaded!';
}

我的问题:

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

    <input type="file" name="myFile" value="upload a file" />
    <input type="submit" name="upload" />
    
    if(isset($_POST['upload'])){
        $fileName=$_POST['myFile']; //file is now assigned to var name
        $result=ftp_put($connect,$fileName,FTP_ASCII); //file linked to var name being uploaded
    }
    
  3. 这是最有效的安全方式吗?

感谢阅读

最佳答案

正如@Bonner 所说,Fabien 的回答不正确,因为您正在寻找一个脚本来将文件从您网站上的页面上传到服务器。

首先要记住的是 ftp_put()函数将始终覆盖现有文件。相反,我建议您查看 PHP move_uploaded_file

代码

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

为了更好地理解,我几乎在所有地方都添加了注释。

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

upload.php

<?php
    // Used to determinated if the upload file is really a valid file
    $isValid = true;
    // The maximum allowed file upload size
    $maxFileSize = 1024000;
    //Allowed file extensions
    $extensions = array('gif', 'jpg', 'jpeg', 'png');

    // See if the Upload file button was pressed.
    if(isset($_POST['submit'])) {
        // See if there is a file waiting to be uploaded
        if(!empty($_FILES['upload-file']['name'])) {

            // Check for errors
            if(!$_FILES['upload-file']['error']) {
                // Renamed the file
                $renamedFile = strtolower($_FILES['upload-file']['tmp_name']);

                // Get the file extension
                $fileInfo = pathinfo($_FILES['upload-file']['name']);

                // Now vaidate it
                if (!in_array($fileInfo['extension'], $extensions)) {
                    $isValid = false;
                    echo "This file extension is not allowed";
                }

                // Validate that the file is not bigger than 1MB
                if($_FILES['upload-file']['size'] > $maxFileSize) {
                    $isValid = false;
                    echo "Your file's size is to large. The file should not be bigger than 1MB";
                }

                // If the file has passed all tests
                if($isValid)
                {
                    // Move it to where we want it to be
                    move_uploaded_file($_FILES['upload-file']['tmp_name'], 'uploads/'.$renamedFile);
                    echo 'File was successfully uploaded!';
                }
            }
            // If there is an error show it
            else {
                echo 'There was an error file trying to upload the file:  '.$_FILES['upload-file']['error'];
            }
        }
    }

关于PHP FTP_PUT 上传到目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31045233/

相关文章:

java - Play Framework AJAX 在 badRequest() 处返回对象

php - jQuery getJSON 到外部 PHP 页面

javascript - 我可以使用 Google 应用程序脚本上传多个文件吗?

javascript - &lt;input type ="file"> 创建什么,FileUpload 对象还是 FileList?

php - 当数据库上没有图像时尝试上传图像时出错

php - 使用 PHP 从 MYSQL 表生成 Excel

php - 如何使用导出选项在数据表中显示 100k 条记录?

PHP openssl 加密解密错误?没有有效的公钥/私钥?

php - Laravel 覆盖组中间件

javascript - jquery 占位符,而 ajax 加载下拉菜单