PHP FTP_PUT上传到目录 [英] PHP FTP_PUT uploading to directory

查看:157
本文介绍了PHP FTP_PUT上传到目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林学习PHP用自己从一本名为PHP的完整参考 - PHP5.2 即时通讯目前在第11章FTP,上传,删除,MAKEDIR等,但遇到了不包括在书中的一些问题:

Im learning PHP by myself from a book called "PHP the Complete Reference - PHP5.2" Im currently at Chapter11 FTP, uploading,deleting,makedir etc but have run into a few questions not covered by the book:

根据我的书,这是一个简单的code上传到服务器:

According to my textbook this is a simple code to upload to a server:

$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. 要哪​​个目录将在上传,我将如何修改code,如果我要上传到目录中说的public_html /图片/ myFile.jpg
  2. 在这个例子myFile.php是硬codeD,如果我想用户选择要上传的文件?我是正确的假设,你可以做这样的事情:

  1. To which directory will this upload, how will I modify code if i want to upload to directory say public_html/images/myFile.jpg
  2. In the example myFile.php is hardcoded, what if I want user to select a file to upload? Am I correct in assuming you can do something like this:

<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
}

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

  • Is this the most efficient secure way?

    感谢您的阅读

    推荐答案

    由于@Bonner说法比安斯基答案不正确,因为你正在寻找上载在您的网站的服务器从一个页面文件的脚本。

    As @Bonner said Fabien answer is incorrect since you are looking for a script to upload files from a page on your website to the server.

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

    First thing to remember is that ftp_put() function will always overwrite the existing files. Instead I suggest you to have a look at the PHP move_uploaded_file

    code

    这是形式。里面的action属性,我们指定一个文件,该文件将处理和处理的所有文件。你需要使用的multipart / form-data的价值形式的ENCTYPE属性。

    This is the form. Inside the action attribute we specify a file, which will handle and process all the files. You'll need to use the multipart/form-data value for the form's enctype property.

    我有更好的理解作了评论几乎无处不在。

    I have included comments almost everywhere for a better understanding.

    <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上传到目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆