如何添加成像处理有效地获得此脚本

时间:2012-04-16 13:37:53

标签: php mysql forms upload

希望有人可以帮助我。一直在浏览,我尝试似乎没有任何工作,但我新的PHP所以我慢。我需要上传6张图片,效果很好。但后来我意识到你不仅可以上传图像,还可以上传所有其他文件类型。我试图将其限制为每个100kb以下的图像。 heeeeelllllllpppppp !!!!请!

function findexts ($filename) { $filename = strtolower('$filename') ; 
$exts = preg_split("[/\\.]", $filename) ;
 $n = count($exts)-1; 
 $exts = $exts[$n];
  return $exts;
   }

$ext = findexts ($_FILES['images']['name']) ; 
$ran = rand ();
$ran2 = $ran.".";

while(list($key,$value) = each($_FILES['images']['name']))
        {
            if(!empty($value))
            {
                $filename = $ran.$value;
                    $filename=str_replace(" "," _ ",$filename);// Add _ inplace of blank space in file name, you can remove this line

                    $add = "media/".$ran."$filename";
                $insert_query = "INSERT INTO ....VALUES ...";
                       //echo $_FILES['images']['type'][$key];
                 // echo "<br>";
                    copy($_FILES['images']['tmp_name'][$key], $add);
                    chmod("$add",0777);

        mysql_query($insert_query);

            }

        }

3 个答案:

答案 0 :(得分:3)

在这里查看你的两个问题的答案:

https://stackoverflow.com/a/9153419/723855

将此功能添加到您的脚本中(从链接修改):

function acceptFileUpload($thefile){
    if(isset($_FILES[$thefile])) {
        $errors     = array();
        $maxsize    = 2097152;
        $acceptable = array(
            'application/pdf',
            'image/jpeg',
            'image/jpg',
            'image/gif',
            'image/png'
        );

        if(($_FILES[$thefile]['size'] >= $maxsize) || ($_FILES[$thefile]["size"] == 0)) {
            $errors[] = 'File too large. File must be less than 2 megabytes.';
        }

        if(!in_array($_FILES[$thefile]['type'], $acceptable)) && (!empty($_FILES[$thefile]["type"]))) {
            $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
        }

        if(count($errors) !== 0) {
            return true;
        } else {
            foreach($errors as $error) {
                echo '<script>alert("'.$error.'");</script>';
                return false;
            }

            die(); //Ensure no more processing is done
        }
    }
}

然后在您的脚本中更改您的while循环以使用此函数来检查有效文件:

while(list($key,$value) = each($_FILES['images']['name']))
    {
        if(!empty($value))
        {
            if(acceptFileUpload('images'))
            {
            $filename = $ran.$value;
                $filename=str_replace(" "," _ ",$filename);// Add _ inplace of blank space in file name, you can remove this line

                $add = "media/".$ran."$filename";
            $insert_query = "INSERT INTO ....VALUES ...";
                   //echo $_FILES['images']['type'][$key];
             // echo "<br>";
                copy($_FILES['images']['tmp_name'][$key], $add);
                chmod("$add",0777);

    mysql_query($insert_query);
            }
        }

    }

我可能没有将该参数传递给acceptFileUpload()。

答案 1 :(得分:0)

您可以使用

检查文件类型
$_FILES['image']['type']

或者如果你想检查扩展名

$extension = explode('.',(string)$_FILES['image']['name']);
//then check if its "jpg", "gif" or "png"

可以使用

检查文件大小
$_FILES['image']['size']

因此,对于每个图片更新,您的脚本应该是这样的:

$extension = explode('.',$_FILES['image']['name']);
$imgextensions = array();
$size      = $_FILES['image']['size'];
if(($extension == 'jpg' || $extension == 'gif' || $extension == 'png') &&
    $size < 100000 ){
    // upload your file to your filesystem
}else{
    //inform the user
}

答案 2 :(得分:0)

在每个文件的处理脚本上运行四个函数,如果所有测试都通过,那么文件符合您的条件并且可以安全存储(png / jpg / gif +非零+ 10Kb限制+是上传文件)

//Example Call: checkFileExtension($_FILES['fieldname']['name']);
function checkFileExtension($filename) {
    $filename = strtolower($filename) ;
    $filenamePartsArray = preg_split("[/\\.]", $filename) ;
    $extension = $filenamePartsArray[count($filenamePartsArray) - 1];
    if (($extension == 'gif') || ($extension == 'jpeg') || ($extension == 'jpg') || ($extension == 'png')) {
        return true;
    } else {
        return false;
    }
}

//Example Call: checkFileMIME($_FILES['fieldname']['type']);
function checkFileMIME($filetype) {
    if (($filetype == 'image/png') || ($filetype == 'image/jpeg') || ($filetype == 'image/gif')) {
        return true;
    } else {
        return false;
    }
}

//Example Call: checkFileSize($_FILES['fieldname']['size'], 10);
function checkFileSize($filesize, $limitKb = 0) {
    if ($filesize == 0) {
        return false;
    }
    if ($limitKb != 0) {
        if ($filesize > ($limitKb * 1024)) {
            return false;
        }
    }
    return true;
}

//Native Call: is_uploaded_file($_FILES['fieldname']['tmp_name']);

编辑:伪示例使用

foreach ($_FILES as $fieldname => $file) {
    if ((checkFileExtension($file['name'])) && (checkFileMIME($file['type'])) && (checkFileSize($file['size'], 10)) && (is_uploaded_file($file['tmp_name']))) {
        //Move the image with move_uploaded_file
        //Save the file location with DB insert
    }
}