【问题标题】:php $_files keeps being uploadedphp $_files 不断上传
【发布时间】:2011-06-27 15:37:41
【问题描述】:

我有一个脚本可以执行一些简单的编辑,效果很好,问题是即使图像上传成功并且我刷新页面,即使它不是从表单提交的文件输入中选择的,也会创建相同的图像:

<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>

这是 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);

    }


}

【问题讨论】:

  • 如果刷新页面,则重新提交表单。大多数浏览器都要求这样做。一个常见的解决方案是提交完成后重定向到不同的页面,甚至是同一个页面,这样会阻止重新提交。

标签: php forms post file-upload gd


【解决方案1】:

您正在刷新哪个页面? “你上传完成”的那个?这是有道理的——这是上传的结果页面。刷新那个会给你通常的“这个页面是 POST 的结果?重做 POST?”输入警告,这将重新提交整个表单,包括文件。

为防止刷新重新上传,您必须在表单处理代码完成后将用户重定向到其他地方。

<?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】:

    在此处阅读有关 POST-REDIRECT-GET 的信息:

    http://en.wikipedia.org/wiki/Post/Redirect/Get

    【讨论】:

      【解决方案3】:

      当您刷新页面时,表单会重新发送,一切都会再次发生。处理表单时的一般规则是在脚本处理完提交的表单后将用户重定向到另一个页面。然后,即使他们重新加载页面,他们也不会重新提交任何东西,这样的问题就会避免

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-27
        • 2011-11-19
        • 2015-08-22
        • 2017-12-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多