Simple PHP picture upload implementation code

Source: Internet
Author: User


<?php Tutorial
//user upload image Processing file
if (($_files["File" ["type"] = = "Image/gif") | | ($_files["File"] ["type"] = = "Image/jpeg") | | ($_files["File"] ["type"] = = "Image/pjpeg") && ($_files["file"] ["size"] < 100000) {//control the type of picture allowed to upload, the last 100000 is the allowed picture size
if ($_files["file"] ["Error"] > 0 {
echo "return Code:".) $_files["File" ["Error"]. "<br/>"; Error returns
}else{
///This is the upload image information, remove the comments before and after you can see the effect.
echo "Upload:". $_files["File" ["Name"]. "<br/>";
echo "Type:". $_files["File" ["type"]. "<br/>";
echo "Size:". ($_files["File"] ["size"]/1024). "Kb<br/>";
echo "Temp file:". $_files["File" ["Tmp_name"]. "<br/>"; */
if (file_exists ("userupload/". $_files["File" ["Name"])) {
echo $_files["file" ["Name"]. "already exists."
}else{
Move_uploaded_file ($_files["File" ["Tmp_name"], "userupload/". $_files["File" ["name"]);
}

$date =date (' ymdhis '); To get the current time, such as; 20070705163148
$fileName =$_files[' file ' [' Name ']; Get the name of the uploaded file
$name =explode ('. ', $fileName); Place file name with '. ' The partition gets the suffix name and gets an array
$newPath = $date. $name [1]; Get a new file for ' 20070705163148.jpg ', that is, the new path
$oldPath =$_files[' file ' [' Tmp_name ']; temporary folder, which is the previous path
Rename ("userupload/". $fileName, "userupload/". $newPath);

Here you can write your SQL statement, the image address is "userupload/". $newPath

?>
<script type= "Text/javascript Tutorial" >alert (' Picture upload success!! '); </script>
<?php
}
}else{
echo "Invalid file"; Wrong picture type or too big
}
?>

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.