PHP image upload and preview the effect of this article is a picture upload code, he can upload to the server picture in the preview changes, if you think you can save, not can be deleted and re-upload oh.
PHP Tutorial Pictures upload and preview effects
This article is a picture upload code, he can upload to the server picture in the preview changes, if you think you can save, not can delete and re-upload oh.
*/
if (!isset ($_session)) session_start ();
/**2010-6-22
* $data array type contains the following variables
-------------------------------
* $sub _type Submit type (upload: Upload button Delete: Delete button), default upload
* $file the $_files[' filename '] array obtained through the form
* $img The ID value of the _tag_id preview picture
* $img _dir Directory of uploaded images
* $ Max_file_size Unit m (trillion), default: 1m
* $type _array allowed upload picture type (default: Image/pjpeg, Image/jpeg, image/gif)
**/
function Upload ($data) {
if (! $data [' file ']) {
echo '; exit;
}
if (! $data [' img_tag_id ']) {
echo '; exit;
}
if (! $data [' Img_dir ']) {
echo '; exit;
}
if (!isset ($data [' max_file_size '])) {
$data [' max_file_size '] = 1024x768 *;
} else{
$data [' max_file_size '] = $data [' max_file_size '] * 1024x768;
}
if (!isset ($data [' Type_array '])) {
$data [' type_array '] = Array (' Image/pjpeg ', ' image/jpeg ', ' image/gif ');
}
if (!isset ($data [' Sub_type '])) {
$data [' sub_type '] = ' upload ';
}
$imgpath = ";
if (Isset ($data [' Sub_type ']) && $data [' sub_type '] = = ' Delete ') {
if (isset ($_session[' name ')) && $_session[' name ']) {
if (Is_file ($_session[' Imgpath ')) {
$b = unlink ($_session[' Imgpath ');
}
unset ($_session[' name '], $_session[' Imgpath ');
if (!isset ($_session[' name ')) {
Echo ';
Echo ';
}else{
Echo ';
}
}else{
Echo ';
}exit;
}
if (Isset ($_session[' Imgpath ')) && $_session[' Imgpath ']) {
echo "; exit;
}
if (!in_array ($data [' file '] [' type '], $data [' Type_array '])) {
echo "; exit;
}
if ($data [' file '] [' size '] > $data [' max_file_size ']) {
echo "; exit;
}
if (!is_dir ($data [' Img_dir '])) {
@mkdir ($data [' Img_dir '], 0777, true);
}
$imgpath = $data [' Img_dir ']. ' /'. Date (' His ', Time ()). Rand (999). $data [' file '] [' name '];
$isupload = Move_uploaded_file ($data [' file '] [' tmp_name '], $imgpath);
if (! $isupload) {
echo "; exit;
}else{
Echo ';
}
$_session[' name '] = $data [' file '] [' name '];
$_session[' imgpath '] = $imgpath;
return $imgpath;
}
/*test_start*/
$sub _type = ";
if (Isset ($_post[' submit_upload '))) $sub _type = ' upload ';
if (Isset ($_post[' Submit_delete '))) $sub _type = ' delete ';
if ($sub _type) {//echo "; exit;
$data = Array (' sub_type ' = $sub _type,
' File ' = $_files[' file ',
' img_tag_id ' = ' Picview ',
' Img_dir ' = ' upload_img ',
);
$imgpath = Upload ($data);
}else{
$imgpath = isset ($_session[' Imgpath ')? $_session[' Imgpath ': ';
}
?>
http://www.bkjia.com/PHPjc/444841.html www.bkjia.com true http://www.bkjia.com/PHPjc/444841.html techarticle php image upload and preview the effect of this article is an image upload code, he can upload to the server's picture in the preview changes, if you think you can save, not can ...