Php uploads image learning notes and experiences

Source: Internet
Author: User
Tags imagecopy imagejpeg

We must use the # _ FILE variable to upload FILES in php. This automatic global variable $ _ FILES is supported since PHP 4.1.0. Before that, PHP supports the $ HTTP_POST_FILES array starting with version 4.0.0. These arrays will contain all the information about the FILES you uploaded. We recommend that you use $ _ FILES.

If the PHP setting option register_globals is on, the related variable names will also exist. From PHP 4.2.0, the default value of register_globals is set to off.

Assume that the file upload field is named userfile. The name can be named at will.

$ _ FILES ['userfile'] ['name']
The original name of the client machine file.

$ _ FILES ['userfile'] ['type']
The MIME type of the file, which must be supported by the browser, for example, "image/gif ".

$ _ FILES ['userfile'] ['SIZE']
Size of the uploaded file, in bytes.

$ _ FILES ['userfile'] ['tmp _ name']
Temporary File Name stored on the server after the file is uploaded.

$ _ FILES ['userfile'] ['error']
The Error Code related to the file upload. ['Error'] is added in PHP 4.2.0.

Processing functions:

Move_uploaded_file ()
(PHP 4> = 4.0.3, PHP 5)

Move_uploaded_file -- move the uploaded file to a new location

Description
Bool move_uploaded_file (string filename, string destination)

This function checks and ensures that the file specified by filename is a valid file to be uploaded (that is, the file is uploaded through the http post upload mechanism of PHP ). If the file is valid, move it to the file specified by destination.

If filename is not a valid file to be uploaded, no operation is performed. move_uploaded_file () returns FALSE.

If filename is valid but cannot be moved for some reason, no operation will occur. move_uploaded_file () returns FALSE. A warning is also issued.

If the target file already exists, it will be overwritten.

Example:

The Code is as follows: Copy code
If (move_uploaded_file ($ _ FILES ["magfile"] ["tmp_name"], $ uploaddir ))
{
Echo "Update OK! ";
}

Copy (PHP 3, PHP 4, PHP 5) is also available)
Copy -- copy an object
Bool copy (string source, string dest)

Copy the file from source to dest. If the call succeeds, TRUE is returned. If the call fails, FALSE is returned.

Submission page:

The Code is as follows: Copy code

<Form action = "." method = "post" enctype = "multipart/form-data" name = "UL">
<! -- 'Enctype = "multipart/form-data" 'is required. -->
<Input type = "file" name = "picurl" size = "15"
Accept = "image/x-png, image/gif, image/jpeg">
<Input type = "submit" name = "upload" value = "upload">
</Form>

Processing page:

The Code is as follows: Copy code

If ($ _ FILES ['picurl'] ['SIZE']> 0 ){
If (move_uploaded_file ($ _ FILES ['picurl'] ['tmp _ name'], $ _ FILES ['picurl'] ['name']) {
Echo "image uploaded successfully ";
}
}

You can use $ _ POST ['name'] to receive other non-File forms.

Simple php Image Upload implementation

The Code is as follows: Copy code
<Html>
<Head>
<Title> simple PHP Image Upload implementation </title>
</Head>
<Body>
<? Php
If ($ _ GET ['action'] = 'upfile ')
{
$ Target_path = 'temp _ '. $ _ FILES ['photo'] ['name'];
Echo 'uploaded temporary file: '. $ _ FILES ['photo'] ['tmp _ name'].' <br/> ';
Echo 'destination File Uploaded: '. $ target_path.' <br/> ';
Echo $ _ SERVER ["SCRIPT_FILENAME"]. '<br/> ';
Echo $ _ SERVER ["OS"]. '<br/> ';
// Test function: move_uploaded_file
// You can also use the function: copy
Move_uploaded_file ($ _ FILES ['photo '] ['tmp _ name'], $ target_path );
Echo "Upload result :";
If (file_exists ($ target_path )){
If ($ _ SERVER ["OS"]! = "Windows_NT "){
@ Chmod ($ target_path, 0604 );
}
Echo '<font color = "green"> Succeed! </Font> <br/> <a href = "http ://'. $ _ SERVER ["SERVER_NAME"]. "/". $ target_path. '"> ';
} Else {
Echo '<font color = "red"> Failed! </Font> ';
}
Exit;
}
?>
<H1> Registration <Form action = "upload. php? Action = upfile "method =" post "name =" UForm "enctype =" multipart/form-data ">
<Fieldset>
<Legend> Your information </legend>
<Ul>
<Li> Your Phot <input type = "file" name = "photo"> </li>
</Ul>
</Fieldset>
<Button type = "submit"> upload </button>
</Form>
</Body>
</Html>

The above code is only applicable to learning and usage. If you want to use it on the current server, you must write the following code:

The Code is as follows: Copy code

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<? Php
/*************************************** ***************************************

Parameter description:
$ Max_file_size: size limit of the uploaded file, in bytes
$ Destination_folder: File Upload path
$ Watermark: whether to add a watermark. (1 indicates adding a watermark; others indicates not adding a watermark );

Instructions for use:
1. Remove the number before the line "extension = php_gd2.dll" in the PHP. ini file because the GD library is used;
2. Change extension_dir = to the directory where your php_gd2.dll is located;
**************************************** **************************************/

// Upload file type list
$ Uptypes = array (
'Image/jpg ',
'Image/jpeg ',
'Image/png ',
'Image/pjpeg ',
'Image/gif ',
'Image/bmp ',
'Image/x-png'
);

$ Max_file_size = 2000000; // size limit of uploaded files, in bytes
$ Destination_folder = "uploadimg/"; // File Upload path
$ Watermark = 1; // whether to add a watermark. (1 indicates adding a watermark. Otherwise, no watermark is added );
$ Watertype = 1; // watermark type (1 is text, 2 is image)
$ Waterposition = 1; // watermark position (1 indicates the lower left corner, 2 indicates the lower right corner, 3 indicates the upper left corner, 4 indicates the upper right corner, and 5 indicates the center );
$ Waterstring = "http://www.bKjia. c0m/"; // watermark string
$ Waterimg = "xplore.gif"; // watermark image
$ Imgpreview = 1; // whether to generate a preview image (1 is generated, others are not generated );
$ Imgpreviewsize = 1/2; // thumbnail Ratio
?>
<Html>
<Head>
<Title> ZwelL Image Upload Program </title>
<Style type = "text/css">
<! --
Body
{
Font-size: 9pt;
}
Input
{
Background-color: #66 CCFF;
Border: 1px inset # CCCCCC;
}
-->
</Style>
</Head>

<Body>
<Form enctype = "multipart/form-data" method = "post" name = "upform">
Upload files:
<Input name = "upfile" type = "file">
<Input type = "submit" value = "Upload"> <br>
The file type that can be uploaded is: <? = Implode (',', $ uptypes)?>
</Form>

<? Php
If ($ _ SERVER ['request _ method'] = 'post ')
{
If (! Is_uploaded_file ($ _ FILES ["upfile"] [tmp_name])
// Whether a file exists
{
Echo "the image does not exist! ";
Exit;
}

$ File = $ _ FILES ["upfile"];
If ($ max_file_size <$ file ["size"])
// Check the file size
{
Echo "the file is too large! ";
Exit;
}

If (! In_array ($ file ["type"], $ uptypes ))
// Check the file type
{
Echo "file type does not match! ". $ File [" type "];
Exit;
}

If (! File_exists ($ destination_folder ))
{
Mkdir ($ destination_folder );
}

$ Filename = $ file ["tmp_name"];
$ Image_size = getimagesize ($ filename );
$ Pinfo = pathinfo ($ file ["name"]);
$ Ftype = $ pinfo ['extension'];
$ Destination = $ destination_folder.time (). ".". $ ftype;
If (file_exists ($ destination) & $ overwrite! = True)
{
Echo "a file with the same name already exists ";
Exit;
}

If (! Move_uploaded_file ($ filename, $ destination ))
{
Echo "An error occurred while moving the file ";
Exit;
}

$ Pinfo = pathinfo ($ destination );
$ Fname = $ pinfo [basename];
Echo "<font color = red> uploaded successfully </font> <br> file name: <font color = blue> ". $ destination_folder. $ fname. "</font> <br> ";
Echo "width:". $ image_size [0];
Echo "Length:". $ image_size [1];
Echo "<br> size:". $ file ["size"]. "bytes ";

If ($ watermark = 1)
{
$ Iinfo = getimagesize ($ destination, $ iinfo );
$ Nimage = imagecreatetruecolor ($ image_size [0], $ image_size [1]);
$ White = imagecolorallocate ($ nimage, 255,255,255 );
$ Black = imagecolorallocate ($ nimage, 0, 0 );
$ Red = imagecolorallocate ($ nimage, 255, 0, 0 );
Imagefill ($ nimage, 0, 0, $ white );
Switch ($ iinfo [2])
{
Case 1:
$ Simage = imagecreatefromgif ($ destination );
Break;
Case 2:
$ Simage = imagecreatefromjpeg ($ destination );
Break;
Case 3:
$ Simage = imagecreatefrompng ($ destination );
Break;
Case 6:
$ Simage = imagecreatefromwbmp ($ destination );
Break;
Default:
Die ("unsupported file types ");
Exit;
}

Imagecopy ($ nimage, $ simage, 0, 0, 0, $ image_size [0], $ image_size [1]);
Imagefilledrectangle ($ nimage, 1, $ image_size [1]-15, 80, $ image_size [1], $ white );

Switch ($ watertype)
{
Case 1: // Add a watermark string
Imagestring ($ nimage, 2, 3, $ image_size [1]-15, $ waterstring, $ black );
Break;
Case 2: // Add a watermark image
$ Simage1 = imagecreatefromgif ("xplore.gif ");
Imagecopy ($ nimage, $ simage1 );
Imagedestroy ($ simage1 );
Break;
}

Switch ($ iinfo [2])
{
Case 1:
// Imagegif ($ nimage, $ destination );
Imagejpeg ($ nimage, $ destination );
Break;
Case 2:
Imagejpeg ($ nimage, $ destination );
Break;
Case 3:
Imagepng ($ nimage, $ destination );
Break;
Case 6:
Imagewbmp ($ nimage, $ destination );
// Imagejpeg ($ nimage, $ destination );
Break;
}

// Overwrite the original uploaded file
Imagedestroy ($ nimage );
Imagedestroy ($ simage );
}

If ($ imgpreview = 1)
{
Echo "<br> image preview: <br> ";
Echo "Echo "alt =" image preview: r file name: ". $ destination." r upload time: "> ";
}
}
?>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.