PHP File Upload code (limited jpg file) _php tips

Source: Internet
Author: User
Tags file upload php file upload
Copy Code code as follows:

<?php
/* Picture upload class only jpg format picture * *
Class UploadFile
{
var $inputName; Input name
var $fileName; File naming
var $fileProperty; File properties
var $fileSize = 2097152; File size limit, 2M
var $filePath = "upload/"; File storage Path
function UploadFile ($inputName) {
$this->inputname= $inputName;
$this->getname (); Get a new name
$this->filesave ();
}
Random Name
Private Function GetName () {
$this->filename=date ("Ymdhms"). Rand (0,9). $this->getproperty ();
}
File properties, returning the suffix name
Private Function GetProperty () {
if ($_files[$this->inputname]["type"]== "Image/pjpeg" | | $_files[$this->inputname]["type"]== "Image/jpeg") {
return ". jpg";
}else{
Exit ("Incorrect file format");
}
}
File storage
Private Function FileSave () {
if ($_files[$this->inputname]["size"]> $this->filesize) {
Exit ("The file is too large, maximum is limited to". $this->filesize. " byte ");
}
if (!file_exists ($this->filepath)) {
mkdir ($this->filepath); Established if the file storage directory does not exist;
}
Move_uploaded_file ($_files[$this->inputname]["Tmp_name"),
$this->filepath. $this->filename);
}
}
if ($_get[' action ']== "FileSave") {
$f =new uploadfile ("file");
Echo ' <input name= "TextField" type= "text" size= "value=" '. $f->filepath. $f->filename. "/> Upload success! <a href= "'. $f->filepath. $f->filename. '" > Browse </a> ';
}else{
Echo ' <form action= ' "Action=filesave" method= "post" enctype= "Multipart/form-data" Name= "Form1" Id= "Form1" >
<input type= "File" name= "file" size= "/>"
<input type= "Submit" name= "Submission" value= "submitted"/>
</form> ';
}
?>

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.