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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?