(Code fragment test successful, successful upload!) )
The impulse to learn about PHP code yesterday came from something like imitating the company Image Library. Therefore, today, when the most basic function of the realization of PHP has a conceptual understanding, it is very natural to think of an important function urgent to realize, that is the image upload.
So, began to search on the Internet, to find the individual blog from three pages composed of snippets of code, and then copied down the study, the results are always debugging. This thought is the company computer into the domain problem in mischief, later after some attempt, intend to temporarily put. Go home and try (because the home computer is the purest environment).
After home, the code out to try, still not, the same error content. Decisively, I'm going to try to find another piece of code. Ah, very smooth, found a single page of code snippets, one attempt to succeed. cheers!
------------The following code reproduced and posted up to share with you--------
Copy Code code as follows:
<?php
$uptypes =array (' image/jpg ',//upload file type list
' Image/jpeg ',
' Image/png ',
' Image/pjpeg ',
' Image/gif ',
' Image/bmp ',
' Image/x-png ');
$max _file_size=5000000; Upload file size limit, unit byte
$destination _folder= "upload/"; Upload file path
$watermark = 1; Whether to add watermark (1 for Watermark, other for no watermark);
$watertype = 1; Watermark Type (1 for text, 2 for picture)
$waterposition = 1; Watermark position (1 is lower left corner, 2 is lower right corner, 3 is upper left corner, 4 is upper right corner, 5 is centered);
$waterstring = "newphp.site.cz"; Watermark String
$waterimg = "Xplore.gif"; Watermark Picture
$imgpreview = 1; Whether to generate a preview diagram (1 is a build, others are not generated);
$imgpreviewsize =1/2; Thumbnail proportions
?>
<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>
<body>
<center><form enctype= "Multipart/form-data" method= "post" name= "Upform" >
Upload file: <br><br><br>
<input name= "upfile" type= "file" style= "width:200;border:1 solid #9a9999; font-size:9pt; Background-color: #ffffff "size=" >
<input type= "Submit" value= "upload" style= width:30;border:1 solid #9a9999; font-size:9pt; Background-color: #ffffff "size=" ><br><br><br>
The types of files allowed to upload are: jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf <br><br>
<a href= "index.php" > Return </a>
</form>
<?php
if ($_server[' request_method '] = = ' POST ')
{
if (!is_uploaded_file ($_files["Upfile"][tmp_name))
Is there a file
{
echo "<font color= ' Red ' > file does not exist! </font> ";
Exit
}
$file = $_files["Upfile"];
if ($max _file_size < $file ["size"])
Check File size
{
echo "<font color= ' Red ' > file is too big! </font> ";
Exit
}
if (!in_array ($file ["type"], $uptypes))
Check file type
{
echo "<font color= ' red ' > can only upload image files or flash! </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 ' > file with the same name already exists! </a> ";
Exit
}
if (!move_uploaded_file ($filename, $destination))
{
echo "<font color= ' red ' > move file Error! </a> ";
Exit
}
$pinfo =pathinfo ($destination);
$fname = $pinfo [basename];
echo "<font color=red> has been successfully uploaded </font><br> FileName: <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,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" > cannot upload this type of file!) </a> ");
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 original upload file
Imagedestroy ($nimage);
Imagedestroy ($simage);
}
if ($imgpreview ==1)
{
echo "<br> picture preview:<br>";
echo "<a href=\" ". $destination." \ "target= ' _blank ' >echo "alt=\" Picture preview: \ r filename: ". $destination." \ r upload time: \ "border= ' 0 ' ></a>";
}
}
?>
</center>
</body>