php图片上传不会将图像保存到ftp

时间:2016-08-21 21:13:32

标签: php

我试图将图片上传到文件夹,图片不会上传,我尝试了几个代码,如:

tutorialspoint.com/php/php_file_uploading.htm 要么 www.w3schools.com/php/php_file_upload.asp 但它永远不会上传图片,我也已经将777权限授予了文件夹和文件本身。有谁知道可能导致它的原因?

我的代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Fds upload</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
    <form method="post">
        Enter username:
        <input type="username" placeholder="Enter username">
        Enter password:
        <input type="password" placeholder="Enter password">
        Enter design name:
        <input type="text" name="design_name" placeholder="Enter name" class="form-control">
        Select gender:
        <select name="fds_gender" id="fds_gender">
            <option value="stud">Male</option>
            <option value="babe">Female</option>
        </select>
        Select category:
        <select name="categories" id="fds_categories">
            <option value="20080687">Hairs: 25 Zcard</option>
            <option value="20080847">Shirts: 1000 Zbucks</option>
            <option value="20080836">Shirts: 25 Zcard</option>
            <option value="20080934">Jackets: 25 Zcard</option>
            <option value="20080934">Jackets: 25 Zcard</option>
            <option value="20080849">Bottoms: 25 Zcard</option>
            <option value="20080988">Shoes: 800 Zbucks</option>
            <option value="20080992">Shoes: 25 Zcard</option>
            <option value="20081034">Gloves: 25 Zcard</option>
            <option value="20081014">Belts: 500 Zbucks</option>
            <option value="20081015">Belts: 25 Zcard</option>
            <option value="20081004">Scarves: 10 Zcard</option>
            <option value="20080941">Hats: 10 Zcard</option>
            <option value="20080834">Bags: 20 Zcard</option>
            <option value="20081049">Misc: 25 Zcard</option>
            <option value="20081020">Masks: 10 Zcard</option>
        </select>
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <button type="submit">Upload design</button>
    </form>

    <?php
        if(!empty($_POST)){
            // Image upload code using the following foldername: uploads
        }
    ?>

    <script type="text/javascript">
        $(document).ready(function(){
            $("#fds_gender").change(function () {
                var gender = $(this).val();
                $.ajax({
                    type: "POST",
                    data: {
                        "gender" : gender
                    },
                    url: "ajax/fds_categories_ajax.php",
                    success: function(data){
                        $("#fds_categories").empty();
                        $.each($.parseJSON(data), function(index, element) {
                            if(gender == 'stud'){
                                if(element.p == 0){
                                    $("#fds_categories").append(
                                        $('<option></option>').val(element.id).html(element.name + ": " + element.pb + ' Zbucks')
                                    );
                                }else{
                                    $("#fds_categories").append(
                                        $('<option></option>').val(element.id).html(element.name + ": " + element.p + ' Zcard')
                                    );
                                }
                            }else{
                                if(element.p == 0){
                                    $("#fds_categories").append(
                                        $('<option></option>').val(element.id).html(element.name + ": " + element.pb + ' Zbucks')
                                    );
                                }else{
                                    $("#fds_categories").append(
                                        $('<option></option>').val(element.id).html(element.name + ": " + element.p + ' Zcard')
                                    );
                                }
                            }
                        });
                    }
                });
            });
        });
    </script>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

确保您的表单具有正确的参数,例如:enctype =“multipart / form-data”

答案 1 :(得分:0)

正如@corey指出的那样,在处理文件上传时,您需要将表单的enctype属性设置为multipart/form-data。在许多情况下,还可以并且实际上还希望添加隐藏输入以限制文件大小以防止大文件花费很长时间来处理/失败但它不是必需的。在服务器端,您需要检查$_FILES数组,而不是$_POST

<form method="post" enctype='multipart/form-data'>
    Enter username:
    <input type="username" placeholder="Enter username">
    Enter password:
    <input type="password" placeholder="Enter password">
    Enter design name:
    <input type="text" name="design_name" placeholder="Enter name" class="form-control">
    Select gender:
    <select name="fds_gender" id="fds_gender">
        <option value="stud">Male</option>
        <option value="babe">Female</option>
    </select>
    Select category:
    <select name="categories" id="fds_categories">
        <option value="20080687">Hairs: 25 Zcard</option>
        <option value="20080847">Shirts: 1000 Zbucks</option>
        <option value="20080836">Shirts: 25 Zcard</option>
        <option value="20080934">Jackets: 25 Zcard</option>
        <option value="20080934">Jackets: 25 Zcard</option>
        <option value="20080849">Bottoms: 25 Zcard</option>
        <option value="20080988">Shoes: 800 Zbucks</option>
        <option value="20080992">Shoes: 25 Zcard</option>
        <option value="20081034">Gloves: 25 Zcard</option>
        <option value="20081014">Belts: 500 Zbucks</option>
        <option value="20081015">Belts: 25 Zcard</option>
        <option value="20081004">Scarves: 10 Zcard</option>
        <option value="20080941">Hats: 10 Zcard</option>
        <option value="20080834">Bags: 20 Zcard</option>
        <option value="20081049">Misc: 25 Zcard</option>
        <option value="20081020">Masks: 10 Zcard</option>
    </select>
    Select image to upload:
    <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
    <input type="file" name="fileToUpload" id="fileToUpload">
    <button type="submit">Upload design</button>
</form>

<?php

    $field='fileToUpload';
    $dir = 'uploads';

    if( isset( $_FILES[ $field ] ) ){

        $name   =   $_FILES[ $field ]['name'];
        $size   =   $_FILES[ $field ]['size'];
        $tmp    =   $_FILES[ $field ]['tmp_name'];
        $type   =   $_FILES[ $field ]['type'];
        $ext    =   strtolower( pathinfo( $name, PATHINFO_EXTENSION ) );
        $target =   realpath( $_SERVER['DOCUMENT_ROOT'] . '/' . $dir );

        if( $target ) $result   =   move_uploaded_file( $tmp, $target . $name );
        echo $result ? 'Success' : 'Failed';
    }
?>