没有register_globals的图片上传

时间:2011-06-09 09:26:00

标签: php image-processing register-globals

我使用的图片上传脚本已停止工作,因为我的主机已关闭register_globals。但是,我不知道如何在没有它的情况下使其工作。如果你能帮助我,我会很高兴的。这是代码:

            $uploadedfile = $_FILES['photo']['tmp_name'];
            $src = imagecreatefromjpeg($uploadedfile);

            // get the date from EXIF data 
            $exif = exif_read_data($uploadedfile, 0, true);
            foreach ($exif as $key => $section) {
                foreach ($section as $name => $val) {
                    if ($name == 'DateTimeOriginal') {
                        $the_filename = explode(" ", $val);
                        $the_filenamedate = str_replace(":", "", $the_filename[0]);
                        $the_filenametime = str_replace(":", "", $the_filename[1]);
                        $newfilename = $the_filenamedate."-".$the_filenametime.".jpg";

                        $the_datetime = explode(" ", $val);
                        $the_date = str_replace(":", "-", $the_datetime[0]);
                        $the_time = $the_datetime[1];
                        $datetime = $the_date." ".$the_time;

                        $exif_db = 'y';
                    }
                }
            }
            // use current date and time if no exif data
            if (empty($newfilename)) {
                $newfilename = date("Ymd-His").".jpg";
                $datetime = date("Y-m-d H:i:s");
                $exif_db = 'n';
            }

            // resize if necessary
            list($width,$height) = getimagesize($uploadedfile);
            if ($resize_it == 'y') {
                if ($width > $maxwidth) {
                    $newwidth = $maxwidth;
                    $newheight = ($height/$width)*$newwidth;
                } else {
                    $newwidth = $width;
                    $newheight = ($height/$width)*$newwidth;
                }
            } else {
                $newwidth = $width;
                $newheight = $height;
            }
            $tmp = imagecreatetruecolor($newwidth,$newheight);
            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

            $filename = $dirpath.$gal_id."/".$newfilename;
            imagejpeg($tmp,$filename,100);

            // create thumbnail
            $uploadedthumb = $_FILES['photo']['tmp_name'];
            $srcthumb = imagecreatefromjpeg($uploadedthumb);
            list($widththumb,$heightthumb) = getimagesize($uploadedthumb);
            if ($widththumb > $heightthumb) {
                $newheightthumb = 100;
                $newwidththumb = ($widththumb/$heightthumb)*$newheightthumb;
            } elseif ($widththumb == $heightthumb) {
                $newheightthumb = 100;
                $newwidththumb = 100;
            } elseif ($widththumb < $heightthumb) {
                $newwidththumb = 100;
                $newheightthumb = ($heightthumb/$widththumb)*$newwidththumb;
            } else {
                $newheightthumb = 100;
                $newwidththumb = ($widththumb/$heightthumb)*$newheightthumb;
            }

            $tmpthumb = imagecreatetruecolor($newwidththumb,$newheightthumb);
            imagecopyresampled($tmpthumb,$srcthumb,0,0,$src_top,$src_left,$newwidththumb,$newheightthumb,$widththumb,$heightthumb);
            $thumbname = $dirpath.$gal_id."/zth_".$newfilename.".jpg";
            imagejpeg($tmpthumb,$thumbname,100);

            // free memory, destroying the source's and the pic's canvas
            imagedestroy($srcthumb);
            imagedestroy($tmpthumb);
            imagedestroy($src);
            imagedestroy($tmp);

提前致谢!

1 个答案:

答案 0 :(得分:3)

使用$ _POST数组读取已发布的表单字段。名为emil的字段将具有PHP变量$_POST['emil']中的值,而不是$emil中的值。您必须更改您阅读表单字段的所有实例。

同样适用于查询字符串中的变量,现在可以在$_GET中找到,$_COOKIE中的Cookie和$_SESSION中的会话变量。