(Code snippet test successful, upload successfully!) )
Because of the impulse to learn PHP code yesterday, it came from something like imitating the image Library of a similar company. Therefore, today when the most basic function of the implementation of PHP has a conceptual understanding, it is natural to think of an important function to be implemented urgently, that is, the image upload.
So, began to search online, find the individual blog in the three page composed of code snippets, so copied down research, the results are always debugging. This thought is the company computer into the domain problem in mischief, later after a try, intend to temporarily put. Go home to try (because the home computer is the purest environment to prepare).
Home, the code copied out to try it, still no, the same error content. Very decisively, going to find another way to get a piece of code again. Ha, very smooth, found a code fragment of only one page, one attempt succeeded. cheers!
------------The following code reproduced, and posted to share with you--------
Copy the Code code as follows:
$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
$destinati c Repeat-x Bottom}
if ($_server[' request_method '] = = ' POST ')
{
if (!is_uploaded_file ($_files["Upfile"][tmp_name]))
Whether the file exists
{
echo "file does not exist! ";
Exit
}
$file = $_files["Upfile"];
if ($max _file_size < $file ["size"])
Check File size
{
echo "file is too big! ";
Exit
}
if (!in_array ($file ["type"], $uptypes))
Check file types
{
echo "can only upload image files or flash! ";
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];
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 ("Cannot upload this type of file! ");
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: \ "border= ' 0 ' >";
}
}
?>
The above describes the picture and Fax viewer php image upload code, including the picture and Fax Viewer content, I hope the PHP tutorial interested in a friend helpful.