The move_uploaded_file function is indispensable for uploading files. This function checks and ensures that the file specified by the file is a valid file to be uploaded (that is, the file is uploaded through the http post Upload mechanism in the php Tutorial ). If the file is valid, it is moved to the file specified by newloc.
If the file is not a valid Upload file, no operation is performed. move_uploaded_file () returns false.
If the file is valid but cannot be moved for some reason, no operation will occur. move_uploaded_file () will return false, and a warning will be issued.
First, let's take a look at the settings of the form on the Upload
<Html>
<Head>
<Title> a simple file upload form </title>
</Head>
<Body>
<Form enctype = "multipart/form-data"
Action = "<? Print $ _ server ['php _ self ']?> "Method =" post ">
<P>
<Input type = "hidden" name = "max_file_size" value = "102400"/>
<Input type = "file" name = "fupload"/> <br/>
<Input type = "submit" value = "upload! "/>
</P>
</Form>
</Body>
</Html>
In this way, the maximum size of uploaded files set by the master cannot exceed 102400 bytes.
To upload files in php, the following code is simple:
If ($ _ files ['fupload'] ['type'] = "image/gif "){
$ Source = $ _ files ['fupload'] ['tmp _ name'];
$ Target = "upload/". $ _ files ['fupload'] ['name'];
Move_uploaded_file ($ source, $ target); // or die ("couldn't copy ");
$ Size = getimagesize ($ target );
$ Imgstr = "<p> $ Imgstr. = "src =" $ target "alt =" uploaded image "/> </p> ";
Print $ imgstr;
}
In this case, ko is used. Next we will summarize the examples.
Php
<Html>
<Form enctype = "multipart/form-data" action = "" method = "post">
<Input type = "hidden" name = "max_file_size" value = "6000000"/> <! -- Set the maximum number of bytes allowed to submit a form -->
File upload: <input name = "file" type = "file"/>
<Input type = "submit" value = "upload"/>
</Form>
</Html>
<?
// Processing program
Function extend ($ file_name) // defines the function for obtaining the file extension
{$ Extend = explode (".", $ file_name );
$ Va = count ($ extend)-1;
Return strtolower ($ extend [$ va]);}
$ Dirname = ".. /"; // whether to enable the previous path. Format: $ dirname = ".. /"; or $ dirname =" http://www.cnblogs.com/"; and so on in combination with $ dir, be sure not to overflow the root path
$ Dir = 'upload/aa/bb '; // sets the upload directory, which is combined with the above $ dirname
$ File = $ _ files ['file']; // Obtain the file from the file domain form
$ Filename = $ file ['name']; // obtain the full name of the file.
$ C_filesize = $ file ['size']; // Obtain the local file size
$ Extendname = extend ($ filename); // get the file extension
If ($ c_filesize> 200000000000) die ("The file is too large"); // restrict the size of the uploaded file, in bytes
// If ($ extendname! = "Jpg") die ("only images in jpg format are allowed to be uploaded"); // restrict the format of the uploaded file. Remove the two slashes at the beginning of the statement to take effect.
If (! File_exists ($ dir); // check whether the directory folder exists. If it does not exist, create a new folder.
{
$ V = split ('[/.-]', $ dir );
For ($ I = 0; $ I <count ($ v); $ I ++)
{$ Dirname = $ dirname. $ v [$ I];
If (! File_exists ($ dirname) mkdir ($ dirname );
$ Dirname = $ dirname ."/";}
} // Directory created
?>
<?
$ Dest = $ dirname. date ("ymdhis", time ()). rand (100000,999999 ). ". ". $ extendname; // Set the file name to date plus a random number and extension from 100000 to 999999
If (file_exists ($ dest) die ("This file already exists ");
If (move_uploaded_file ($ file ['tmp _ name'], $ dest) // call the file upload function
{$ S_filesize = filesize ($ dest); // Obtain the file size of the server.
Echo "the file is uploaded successfully. <a href =". $ dest. "> View the file address </a> ";
Echo "<br> Local File name:". $ filename;
Echo "<br> Remote File name:". $ dest;
Echo "<br> large and small:". ceil ($ s_filesize/1024). "kb ";
Echo "<br> extension:". $ extendname;
Echo "<br>". $ c_filesize. "byte ";}
Else
{Echo "file upload not completed ";}
?>
Summarizes the error code for file upload.
Upload_err_ OK: no error.
The file uploaded by upload_err_ini_size exceeds the maximum value specified in the file.
The max_file_size hidden part of the file uploaded by upload_err_form_size exceeds the maximum value.
The upload_err_partial file upload is canceled, and only some files are uploaded.
Upload_err_nofile is not uploaded.