PHP Upload Pictures Learning notes and _php tutorials

Source: Internet
Author: User
Tags imagecopy imagejpeg
We must use the #_file variable to upload the file in PHP, and this automatic global variable $_files is supported starting with PHP version 4.1.0. Prior to this, PHP supported $HTTP _post_files arrays starting with version 4.0.0. These arrays will contain all the information about your uploaded files, where we recommend you to use the $_files.

If PHP's set option Register_globals is on, the associated variable name will also be present. Starting with the PHP 4.2.0 version, the default value of Register_globals is set to OFF.

We assume that the name of the File upload field is userfile. Names can be arbitrarily named.

$_files[' UserFile ' [' Name ']
The original name of the client machine file.

$_files[' userfile ' [' type ']
The MIME type of the file requires the browser to provide support for that information, such as "Image/gif".

$_files[' userfile ' [' Size ']
The size of the uploaded file, in bytes.

$_files[' UserFile ' [' Tmp_name ']
Temporary file names stored on the server after the files have been uploaded.

$_files[' userfile ' [' Error ']
The error code associated with the file upload. [' ERROR '] was added in version PHP 4.2.0.

handler function:

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

Move_uploaded_file--Move uploaded files 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 upload file (that is, uploaded via PHP's HTTP POST upload mechanism). If the file is valid, move it to the file specified by destination.

If filename is not a valid upload file, nothing happens and Move_uploaded_file () returns FALSE.

If filename is a legitimate upload file, but for some reason it cannot be moved, nothing happens and 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 also available (PHP 3, PHP 4, PHP 5)
Copy--Copy files
BOOL Copy (string source, String dest)

Copy the file from source to Dest. Returns TRUE if successful, and FALSE if it fails.

Submit page:

The code is as follows Copy Code

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 upload success";
}
}

Other forms that are not file types can still be received with $_post[' name '.

PHP Upload Image Simple implementation

The code is as follows Copy Code


PHP Upload Image Simple implementation


if ($_get[' action '] = = ' Upfile ')
{
$target _path = ' temp_ '. $_files[' photo ' [' name '];
Echo ' uploaded temp file: '. $_files[' photo ' [' tmp_name ']. '
';
Echo ' uploaded target file: '. $target _path. '
';
echo $_server["Script_filename"]. '
';
echo $_server["OS"]. '
';
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 ' succeed!
';
} else {
Echo ' failed! ';
}
Exit
}
?>

Registration




The above code only applies to learning, if you want to use on the current server we must write the following

The code is as follows Copy Code

/******************************************************************************

Parameter description:
$max _file_size: Upload file size limit, unit byte
$destination _folder: Upload file path
$watermark: Whether additional watermark (1 is watermark, the other is not watermark);

Instructions for use:
1. Remove the "Extension=php_gd2.dll" line in the php.ini file, because we need to use the GD library;
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; Upload file size limit, unit byte
$destination _folder= "uploadimg/"; Upload file path
$watermark = 1; Whether additional watermark (1 is watermark, the other is not watermark);
$watertype = 1; Watermark Type (1 for text, 2 for picture)
$waterposition = 1; The watermark position (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner, and 5 is centered);
$waterstring = "http://www.bKjia.c0m/"; Watermark String
$waterimg = "Xplore.gif"; Watermark Picture
$imgpreview = 1; Whether to generate a preview map (1 is generated, others are not generated);
$imgpreviewsize =1/2; Thumbnail scale
?>


Zwell Picture Uploading Program


if ($_server[' request_method '] = = ' POST ')
{
if (!is_uploaded_file ($_files["Upfile"][tmp_name]))
Whether the file exists
{
echo "Picture does not exist!";
Exit
}

$file = $_files["Upfile"];
if ($max _file_size < $file ["size"])
Check File size
{
echo "File too big!";
Exit
}

if (!in_array ($file ["type"], $uptypes))
Check file types
{
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 "file with the same name already exists";
Exit
}

if (!move_uploaded_file ($filename, $destination))
{
echo "Error moving File";
Exit
}

$pinfo =pathinfo ($destination);
$fname = $pinfo [basename];
echo "has been successfully uploaded
File name: ". $destination _folder. $fname."
";
echo "width:". $image _size[0];
echo "Length:". $image _size[1];
echo "
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,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 type");
Exit
}

Imagecopy ($nimage, $simage, 0,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 watermark String
Imagestring ($nimage, 2,3, $image _size[1]-15, $waterstring, $black);
Break
Case 2://Add watermark Picture
$simage 1 =imagecreatefromgif ("Xplore.gif");
Imagecopy ($nimage, $simage 1,0,0,0,0,85,15);
Imagedestroy ($simage 1);
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 upload file
Imagedestroy ($nimage);
Imagedestroy ($simage);
}

if ($imgpreview ==1)
{
echo "
Picture preview:
";
echo "echo" alt= "Picture preview: R filename:". $destination. " R upload time: ">";
}
}
?>

http://www.bkjia.com/PHPjc/628871.html www.bkjia.com true http://www.bkjia.com/PHPjc/628871.html techarticle We must use the #_file variable to upload the file in PHP, and this automatic global variable $_files is supported starting with PHP version 4.1.0. Before that, starting with version 4.0.0, PHP supports ...

  • Related Article

    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.