As through the form to put a file on the server

One of often questions, it as through the form to put a file on the server. It is done{made} rather easily, all about 10 lines of a code and php the script is ready. And thus of anything

Except for php it is not required to you.


For the beginning at once I shall make a reservation, this example will work only in php versions 4.0.3 and is higher. It from for uses of two new functions turns out

move_uploaded_file () and is_uploaded_file (). But believe, it of it costs{stands}, and php all taki is desirable for updating.


So the code php a script looks as follows:



<? php

/* Where we save files */

    $store_dir ='c:/temp/upload / ';


/* If to us have not passed a file we shall show the form */

    if (! $user_file) {

   ?>

    <form action = " <? php print $php_self?> " method = "post" enctype = "multipart/form-data">

    <input type = "file" name = "user_file"> <input type = "submit">

    </form>

    <? php

}

/* If have passed a file */

    else {

/* Whether we shall check up all correctly */

    if (is_uploaded_file ($user_file)) {

/* We shall move a file */

    move_uploaded_file ($user_file, $store_dir. $ user_file_name);

/* We shall say about it */

    print " Thanks for a file <br> n ";

}

/* If not it is correct */

    else {

    print " Not the correct data <br> n ";

}

}

   ?>


Let's consider who and that at us here means:


$user_file - this variable (the name undertakes from the form) contains a way to a time file which we zakachivaem on the server. By a prefix to this variable

Suffixes _name, _size, _type we can receive and other additional information. For example:

$user_file_name - an original name of a file.

$user_file_size - the size of a file in bajtakh.

$user_file_type - mime type of a file, for example "image/gif".



is_uploaded_file () - function returns true or false and is used for check of correctness of a transmitted file. So for example if a file really

It is transferred{handed} to us through http post you receive true. Well and otherwise, for example if to you have tried to pass that that not that, for example a combination from

Parameters for reception/etc/passwd - you receive that false.


move_uploaded_file () - function moves a temporary file in there where we specify. The first parameter - that we move, the second parameter - where

We move.