pr0b
pr0b

Reputation: 377

php image upload wont save image to ftp

im trying to upload a image to a folder, the image wont upload tho, i've tried several codes such as:

tutorialspoint.com/php/php_file_uploading.htm or www.w3schools.com/php/php_file_upload.asp but it would never upload the picture, also i've granted the 777 right to the folder and the file itself. Does anyone know a reason what could cause it?

My code looks like following:

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

Upvotes: 0

Views: 60

Answers (2)

Professor Abronsius
Professor Abronsius

Reputation: 33813

As pointed out by @corey you need to set the enctype property for the form to multipart/form-data when dealing with file uploads. It is possible and in fact desirable in many instances to also add a hidden input to limit the filesize to prevent large files taking a long time to process / fail but it's not essential. On the serverside it is the $_FILES array that you need to check rather than $_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';
    }
?>

Upvotes: 0

corey
corey

Reputation: 310

Be ensure that your form has properly parameter such as: enctype="multipart/form-data"

Upvotes: 2

Related Questions