About PHP upload and processing of multi-image, Trouble master to write a piece of code for learning
The requirements are as follows:
There are 4 pictures under a product name. The image is uploaded to the "upimg" folder in the current path, and the picture path is stored in the MySQL corresponding field.
So when I do product information filling in, for the same product name, you must upload 4 images.
---------Database structure:
Database name: atest,
Table name: Prodcut,
Table corresponding to the field: Pname,pic1,pic2,pic3,pic4 ... (All char types)
--------HTML Form I designed this:
HTML Code
A few things to note:
1, in the database, a product name + four image path to form a piece of data, rather than save as multiple data.
2, the image file must be renamed, or there will be duplicates;
3, before uploading the file to detect whether the image format is jpg,bmp,gif or PNG, otherwise do not upload;
4, the volume of the uploaded image can not exceed 2M; that is: Name= "max_file_size" value= "2000000"
Trouble to add a section of code: upload.php, thank you!
------Solution--------------------
That's how I solved it:
After a successful, upload the class rename, a picture with a class, 4 files in four classes ..... Successful implementation of
This is the stupidest way I've been. And so the master out of the method with the array loop implementation ...
Single image upload class: uploadclass.php (corresponding to multi-image upload, rename it, and the code snippet in the corresponding changes, can be multiple references, multiple uploads)
PHP Code
Return and try Again?click me "; exit;} else{$filename =explode (".", $_files[' pic ' [' name ']);//Ibid., Pic needs to be renamed do{$filename [0]=random (10);//When using the function random, The same should correspond to renaming $name=implode (".", $filename);//$name 1= $name. " MCNCC "; $uploadfile = $uploaddir. $name;//This $uploadfile is the picture path you want to insert into the database corresponding fields, need to change}while (file_exists ($uploadfile)) ///The function name needs to correspond to the most changed if (Move_uploaded_file ($_files[' pic '] [' tmp_name '], $uploadfile))//pic need to correspond to the most changed {if (Is_uploaded_file ($_ files[' pic ' [' tmp_name ']))//pic need to correspond to make changes {echo "the Images Upload failed!";} Else{echo "
Your file has been uploaded:
"//src=" is the most changed echo.
Continue to upload
";}}}? >
------Solution--------------------
A test example
PHP code
Files 1: