duanmi1900 2011-02-15 14:24
浏览 32
已采纳

php $ _files一直在上传

I have a script that performs some simple editing which works fine, the problem is even once the image is uploaded successfully and I refresh the page the same image is being created even though it's not selected from the file input on form submission:

<form  method="post"  enctype="multipart/form-data" id="<?php echo $_SERVER['PHP_SELF']; ?>">
                     <fieldset id="product-images">
                    <input type="file" id="image" name="image" />
                    <input type="submit" name="picture" value="upload Picture"/>
</form>

Here's the php

 <?php if(isset($_POST['picture']) && !empty($_FILES)){(uploadImage($_FILES['image'])} ;?>   

function uploadImage($image)
{
    $id = intval($_GET['id']);
    $size = 75; // the thumbnail height

    $filedir = '../img/products/'; // the directory for the original image
    $thumbdir = '../img/products/thumbs/'; // the directory for the thumbnail image
    $prefix = 'small_'; // the prefix to be added to the original name

    $maxfile = '2000000';
    $mode = '0666';

    $userfile_name = $image['name'];
    $userfile_tmp = $image['tmp_name'];
    $userfile_size = $image['size'];
    $userfile_type = $image['type'];

    if (isset($image['name'])) 
    {
        $prod_img = $filedir.$userfile_name;
        $url = strstr($userfile_name, '.', true);
        $type = strstr($userfile_name, '.');


        $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));

        $sizes = getimagesize($prod_img);
        $width = $size[0];
        $height = $size[1];
        if($width > $height) $biggestSide = $width;
        else $biggestSide = $height;

        //The crop size will be half that of the largest side
        $cropPercent = 1;
        $cropWidth   = $biggestSide*$cropPercent;
        $cropHeight  = $biggestSide*$cropPercent;

        //getting the top left coordinate
        $c1 = array("x"=>($width-$cropWidth)/2, "y"=>($height-$cropHeight)/2);

        $aspect_ratio = $sizes[1]/$sizes[0]; 

        if ($sizes[1] <= $size)
        {
            $new_width = $sizes[0];
            $new_height = $sizes[1];
        }else{
            $new_height = $size;
            $new_width = abs($new_height/$aspect_ratio);
        }

        $destimg=ImageCreateTrueColor($new_width,$new_height)
            or die('Problem In Creating image');
        $srcimg=ImageCreateFromJPEG($prod_img)
            or die('Problem In opening Source Image');
        if(function_exists('imagecopyresampled'))
        {
            imagecopyresampled($destimg,$srcimg,0,0,$c1['x'],$c1['y'],$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
            or die('Problem In resizing');
        }else{
            Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
            or die('Problem In resizing');
        }
        ImageJPEG($destimg,$prod_img_thumb,90)
            or die('Problem In saving');
        imagedestroy($destimg);
        imagedestroy($srcimg);

        insertURL($id, $url, $type);

    }


}
  • 写回答

3条回答 默认 最新

  • douzheng1853 2011-02-15 14:30
    关注

    Which page are you refreshing? the "your uploaded completed" one? That stands to reason - it's the result page of the upload. Refreshing that one would give you the usual "this page is the result of a POST? redo the POST?" type warning, which would re-submit the entire form, including the file.

    To prevent a refresh from re-doing the upload, you have to redirect the user elsewhere after the form handling code's finished.

    <?php
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
        // handle the upload
    
        if (/*form processed OK*/) {
           header("Location: successpage.php");
           exit();
        } else {
           // report any error conditions
        }
    }
    
    // show the form
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分