本文章是一款图片上传代码,他可以把上传到服务器的图片在进行预览修改,如果是你想的你就可以保存了,不是可以删除再重新上传,实例代码如下:
- if(!isset($_session))session_start();
-
-
-
-
-
-
-
-
-
-
-
- function upload($data){
- if(!$data['file']){
- echo '<script>alert("file不能为空!");</script>';exit;
- }
- if(!$data['img_tag_id']){
- echo '<script>alert("预览图片标签id不能为空!");</script>';exit;
- }
- if(!$data['img_dir']){
- echo '<script>alert("图片上传目录不能为空!");</script>';exit;
- }
- if(!isset($data['max_file_size'])){
- $data['max_file_size'] = 1024 * 1024;
- }else{
- $data['max_file_size'] = $data['max_file_size'] * 1024 * 1024;
- }
- 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 '<script>alert("删除成功!");</script>';
- echo '<script>parent.document.getelementbyid("'.$data['img_tag_id'].'").style.display = "none";</script>';
- }else{
- echo '<script>alert("删除失败!");</script>';
- }
- }else{
- echo '<script>alert("没有稿件!");</script>';
- }exit;
- }
-
- if(isset($_session['imgpath']) && $_session['imgpath']){
- echo '<script>alert("稿件已经存在,要想重新上传请删除原来的稿件!");</script>';exit;
- }
- if(!in_array($data['file']['type'], $data['type_array'])){
- echo '<script>alert("稿件类型不匹配,请上传.jpg、.gif和.png格式的图片!");</script>';exit;
- }
- if($data['file']['size'] > $data['max_file_size']){
- echo '<script>alert("您上传的稿件过大,请选择2m以下的图片上传!");</script>';exit;
- }
-
- if(!is_dir($data['img_dir'])){
- @mkdir($data['img_dir'], 0777, true);
- }
- $imgpath = $data['img_dir'].'/'.date('his', time()).rand(100, 999).$data['file']['name'];
- $isupload = move_uploaded_file($data['file']['tmp_name'], $imgpath);
- if(!$isupload){
- echo '<script>alert("稿件上传失败,请尝试重新上传!");</script>';exit;
- }else{
- echo '<script>alert("稿件上传成功!");</script>';
- }
- $_session['name'] = $data['file']['name'];
- $_session['imgpath'] = $imgpath;
-
- return $imgpath;
- }
-
-
- $sub_type = '';
- if(isset($_post['submit_upload']))$sub_type = 'upload';
- if(isset($_post['submit_delete']))$sub_type = 'delete';
- if($sub_type){
- $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']: '';
- }
- ?>
-
- <form action="ad.php" method="post" enctype="multipart/form-data" target="frame">
- <input type="file" name="file" class="input">
- <input type="submit" name="submit_upload" value="上 传">
- <input type="submit" name="submit_delete" value="删 除">
- <iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe>
- </form>
-
- <img id="picview" height="100" style="display:none;">
-
- <script>
- if("<?php echo $imgpath; ?>"){
- parent.document.getelementbyid("picview").src = "<?php echo $imgpath; ?>";
- parent.document.getelementbyid("picview").style.display = "block";
- }
- </script>
|