1: Upload a form using PHP
1.1 Data collection for form form forms
HTML page:
Code explanation: The core module is the property of the form:
--
How to submit: method= "POST" --
Specify the Name property : For example name= "username"--
Specify file upload format for form : enctype= "Multipart/form-data" 2 Click the Submit button after the background action
2.1 Get the image data by $_files the Hyper global variable:
Two-dimensional array, you can get the content of the submission data uploaded by the form.
Array ( [PHOTO] = array ( [name] + picture.jpg file name [Type] = = Image/jpeg File Type [Tmp_name] = C:\Users\Jepson\AppData\Local\Temp\phpBF97.tmp temporarily stored path [ERROR] = 0 error code, if 0 Indicates no error [size] = 17552 file size, unit byte 17kb or so)
2.2 PHP Image dump:
//1 Getting an array of files $file=$_files[' Photo ']; if($file[' error '] = = = 0) {//2 need to dump files $ftmp=$file[' Tmp_name '];//3 Get temporary file path//4 intercept suffix name $name=$file[' Name '];//source file name 1.gif $ext=STRRCHR($name, "." );//. gif//5 randomly generated file name $newName= Time().Rand(10000, 99999).$ext; //move_uploaded_file ("Temporary File path", "Store file path")//6 dump file Move_uploaded_file($ftmp, "./upload/".$newName );
}
2.3 by $_post Super global variable gets post data, saved in global variable.
$username = $_post [' username ' ]; $nickname = $_post [' nickname ' Span style= "COLOR: #000000" >); $age = $_post [' Age ' ); $photo = ""; // picture address
2.4 Preparing the SQL statement for database write operations
Inserting data
$sql = "insert into Stu (name, nickname, age, tel, sex, photo, classid) values ('$username ', '$nickname$age, '$tel', '$sex', '$photo$class )";
2.5 Determine if the insert succeeds and jump action
if $sql ) {echo "add Success"; // Jump to Student list php file Header (' location:studentList.php '); } Else { echo "add failed"; }
Note: my_exec ($sql)--php executes the SQL statement.
Front-End Interaction summary 2: Uploading form data using PHP