dongtao4319 2012-07-12 19:54
浏览 29
已采纳

$ _FILES数组在函数中使用后会被删除吗?

I am resizing an image from a user upload form, and I have passed the contents of the $_FILES array to my resizing function. The function uses GD to resize the image and save it in the desired directory.

I need to make two copies of the image: a large copy and a small copy for a thumbnail. The problem arises when I try to call the function for a second time (but with different dimensions) to make the thumbnail image. I get an unidentified index array message for the $_FILES array.

Is the $_FILES array being deleted automatically after passing it to the function, despite the fact that I have not used the functions to clear it?

FUNCION CALL is as follows

   if ($_FILES['image']['error'] != 4){
foto($_FILES['image'], $THUMBS_DIR, 400, 400, 1);
foto($_FILES['image'], $THUMBS_DIR, 70, 70, 1);
}

FUNCTION

    /*foto function, foto upload, where to save, fotowidth, fotosize,*/
function foto($_FILES, $THUMBS_DIR, $MAX_WIDTH, $MAX_HEIGHT){

/*generate random name*/
$fecha = time();$passpart1 = $fecha;$passpart2 = mt_rand(1, 1000);$ps = $passpart1.$passpart2;$ps2= $passpart1.$passpart2.'thumb';$thumbref='0';    
if (is_uploaded_file($_FILES['tmp_name']))

{
/*resize ratio*/
$original = $_FILES['tmp_name'];
list($width, $height, $type) = getimagesize($original);
if ($width <= $MAX_WIDTH && $height <= $MAX_HEIGHT) {$ratio = 1;}
elseif ($width > $height) {$ratio = $MAX_WIDTH/$width;}
else {$ratio = $MAX_HEIGHT/$height;}

$imagetypes = array('/\.gif$/','/\.jpg$/','/\.jpeg$/','/\.png$/');
$name = preg_replace($imagetypes, '', basename($original));

$name=$ps;switch($type) 
{case 1:$source = @ imagecreatefromgif($original);if (!$source) {$result = 'Cannot process GIF files. Please use JPEG or PNG.';}break;
 case 2:$source = imagecreatefromjpeg($original);break;
 case 3:$source = imagecreatefrompng($original);break;
 default:$source = NULL;$result = 'Cannot identify file type.';}

 if (!$source) {$result = 'Problem copying original';}

 else {
 $thumb_width = round($width * $ratio);
 $thumb_height = round($height * $ratio);
 $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
 imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
 switch($type) 
 {case 1:if (function_exists('imagegif')) {$success = imagegif($thumb, $THUMBS_DIR.$ps.'.gif');$thumb_name = $ps.'.gif';}
 else {$success = imagejpeg($thumb, $THUMBS_DIR.$ps.'.jpg', 50);$thumb_name = $ps.'.jpg';}break;

 case 2:$success = imagejpeg($thumb, $THUMBS_DIR.$ps.'.jpg', 100);$thumb_name = $ps.'.jpg';break;
 case 3:$success = imagepng($thumb, $THUMBS_DIR.$ps.'.png');$thumb_name = $ps.'.png';}

 /*destroy temp or not*/
 if ($success) {$result = "$thumb_name created";}

 }
 $fotref = $thumb_name;



 } 
 else{$errorreport='error with upload';}}

Hello again i have posted the code, I didnt originally post it becuase i didnt expect people to want to see it , I really appreciate it, Thanks to everybody.

  • 写回答

1条回答 默认 最新

  • douhao2011 2012-07-12 20:54
    关注

    I'm pretty sure this is because $_FILES is a superglobal, and thus when you use it as the argument name in your function foto, it gets clobbered, such that $_FILES becomes whatever you passed as the first argument.

    Rewrite the foto function with a variable besides $_FILES, e.g.:

    function foto($image, $THUMBS_DIR, $MAX_WIDTH, $MAX_HEIGHT){
        // rest of code here with $_FILES changed to $image
    }
    

    Alternatively, as $_FILES is a super global, you could skip using it as a parameter at all, changing the call to be:

    foto($THUMBS_DIR, 400, 400, 1)
    

    And the function to be of the form:

    function foto($THUMBS_DIR, $MAX_WIDTH, $MAX_HEIGHT){
       //code placeholder 
       if (is_uploaded_file($_FILES['image']['tmp_name'])) {       
           $original = $_FILES['image']['tmp_name'];
       //rest of code
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里