(Code snippets are successfully tested and uploaded successfully !)
Because yesterday I remembered the impulse to learn PHP code. It came from something similar to the company's IMAGE LIBRARY. So today, when we have a conceptual understanding of PHP after the most basic functions are implemented, we naturally think of an important function to be implemented urgently, that is, the upload of images.
As a result, I began to search online and find code snippets composed of three pages in my blog. So I copied them and did not debug the results. I thought it was a strange problem with the company's computer entry into the domain. After some attempts, I plan to temporarily release it. Go home and try it (because the computer at home is the purest environment ).
When I got home, I copied the code and tried it. It still didn't work. The same error was reported. It is decisive. I plan to find another code snippet. Ha, very smooth. Find a code snippet on only one page and try it at a time. CHEERS!
------------ The following code is reproduced and pasted to share with you --------
Copy codeThe Code is as follows:
<? Php
$ Uptypes = array ('image/jpg ', // list of uploaded file types
'Image/jpeg ',
'Image/png ',
'Image/pjpeg ',
'Image/gif ',
'Image/bmp ',
'Image/x-png ');
$ Max_file_size = 5000000; // size limit of uploaded files, in bytes
$ Destination_folder = "upload/"; // 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 = "newphp. site. cz"; // 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> M4U BLOG-fywyj.cn </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Style type = "text/css"> body, td {font-family: tahoma, verdana, arial; font-size: 11px; line-height: 15px; background-color: white; color: #666666; margin-left: 20px ;}
Strong {font-size: 12px ;}
Aink {color: # 0066CC ;}
A: hover {color: # FF6600 ;}
Aisited {color: #003366 ;}
A: active {color: #9DCC00 ;}
Table. itable {}
Td. irows {height: 20px; background: url ("index. php? I = dots "repeat-x bottom} </style>
</Head>
<Body>
<Center> <form enctype = "multipart/form-data" method = "post" name = "upform">
Upload files: <br>
<Input name = "upfile" type = "file" style = "width: 200; border: 1 solid #9a9999; font-size: 9pt; background-color: # ffffff "size =" 17 ">
<Input type = "submit" value = "Upload" style = "width: 30; border: 1 solid #9a9999; font-size: 9pt; background-color: # ffffff "size =" 17 "> <br>
The file type that can be uploaded is: jpg | jpeg | png | pjpeg | gif | bmp | x-png | swf <br>
<A href = "index. php"> return </a>
</Form>
<? Php
If ($ _ SERVER ['request _ method'] = 'post ')
{
If (! Is_uploaded_file ($ _ FILES ["upfile"] [tmp_name])
// Whether a file exists
{
Echo "<font color = 'red'> the file does not exist! </Font> ";
Exit;
}
$ File = $ _ FILES ["upfile"];
If ($ max_file_size <$ file ["size"])
// Check the file size
{
Echo "<font color = 'red'> the file is too large! </Font> ";
Exit;
}
If (! In_array ($ file ["type"], $ uptypes ))
// Check the file type
{
Echo "<font color = 'red'> only image files or Flash files can be uploaded! </Font> ";
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 "<font color = 'red'> the file with the same name already exists! </A> ";
Exit;
}
If (! Move_uploaded_file ($ filename, $ destination ))
{
Echo "<font color = 'red'> An error occurred while moving the file! </A> ";
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];
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 ("<font color = 'red'> files of this type cannot be uploaded! </A> ");
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 "<a href = \"". $ destination. "\" target = '_ blank'> Echo "alt = \" image preview: \ r file name: ". $ destination." \ r upload time: \ "border = '0'> </a> ";
}
}
?>
</Center>
</Body>
</Html>