【发布时间】:2013-12-02 10:58:13
【问题描述】:
我有这个上传表单:
<form action="upload.php" enctype="multipart/form-data" method="post">
<b>Select your image:</b><br><input type="file" name="userfile" id="file" size="90%" /><br>
(Max size: 10mb)<br>
<b>Browseable:</b><br><input type="radio" name="browse" id="browse" value="1" /> Yes <input type="radio" name="browse" id="browse" value="0" /> No<br>
<input type="submit" value="Upload Image" size="100" />
</form>
还有这个 PHP 代码:
<?PHP
$maxsize = 10485760; // Max File Size IN BYtes
$accepted = array('png', 'jpg', 'jpeg','JPEG', 'gif', 'ico', 'tif', 'bmp', 'PNG');
$length = 10;
$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
if($_SERVER['REQUEST_METHOD'] == 'POST') {
preg_match('/\.([a-zA-Z]+?)$/', $_FILES['userfile']['name'], $matches);
if(in_array(strtolower($matches[1]), $accepted)) {
if($_FILES['userfile']['size'] <= $maxsize) {
$newname = md5_file($_FILES['userfile']['tmp_name']).'.'.$matches[1];
$browse = $_POST["browse"];
if ($browse = "1") {
$filedir = 'img';
} else {
$filedir = 'noimg';
}
move_uploaded_file($_FILES['userfile']['tmp_name'], $filedir.'/'.$newname);
header("Location: index.php?p=uploaded&img=$newname");
} else
header("Location: index.php?p=error&num=2");
} else
header("Location: index.php?p=error&num=1");
}
?>
$_POST["browse"] 之后的部分不起作用。此脚本的目的是将图像放在上传后的公共或私有文件夹中。我也很好奇这个上传脚本是否安全。
【问题讨论】:
-
Megabyte = "MB",而不是 "mb"。
-
试试打印 $browse 看看你得到了什么
标签: php html forms post upload