Php 使用FOR循环一次上载多个文件时,上载将无法正确验证
对PHP来说相当陌生,我目前正在尝试验证多次上传,但似乎没有任何效果。我几乎用尽了我能找到的所有资源 很抱歉,在这个FOR循环中发生了很多事情。我的代码如下:Php 使用FOR循环一次上载多个文件时,上载将无法正确验证,php,validation,file-upload,Php,Validation,File Upload,对PHP来说相当陌生,我目前正在尝试验证多次上传,但似乎没有任何效果。我几乎用尽了我能找到的所有资源 很抱歉,在这个FOR循环中发生了很多事情。我的代码如下: function processForm() { for($i=0; $i < VAR_UPLOAD_FIELDS; $i++) { echo "Upload field $i "; if(!empty($_FILES[file][size][$i]))
function processForm()
{
for($i=0; $i < VAR_UPLOAD_FIELDS; $i++)
{
echo "Upload field $i ";
if(!empty($_FILES[file][size][$i]))
{
//*********************
//START OF TEST CODE FOR VALIDATION.
//***********************
$fileName = $_FILES[file][name][$i];
// get file name (not including path)
$file_basename = substr($fileName, -4, strripos($fileName, '.'));
$file_ext = substr($file_basename, strripos($file_basename, '.'));
$file_ext = @substr($file_ext, 1); // remove dot
// check file type if needed
if (count($exts)) {
if (!in_array($file_ext[i], $exts))
{
echo "Files of this type are not allowed for upload.";
break;
}
}
$newLocation = VAR_UPLOAD_DIRECTORY.$fileName;
$filesize = intval($_FILES["filename"]["size"]);
//*****************************
// Code to add information of uploads to database
//*****************************
if (DO_LOG) {
// Establish DB connection
$link = @mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
if (!$link) {
$errors[] = "Could not connect to mysql.";
break;
}
$res = @mysql_select_db(DB_DATABASE, $link);
if (!$res) {
$errors[] = "Could not select database.";
break;
}
$m_ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$m_size = $filesize;
$m_fname = mysql_real_escape_string($fileName);
$sql = "insert into test_uploads_log (log_filename,log_size,log_ip) values ('$m_fname','$m_size','$m_ip')";
$res = @mysql_query($sql);
if (!$res) {
$errors[] = "Could not run query.";
break;
}
@mysql_free_result($res);
@mysql_close($link);
}
//*****************************
// End of database code
//*****************************
//Files copied to location.
if(!copy($_FILES[file][tmp_name][$i],$newLocation))
{
echo "<b>Failed - ".$_FILES[file][name][$i]." would not copy to ".$newLocation."</b> (Check your upload directory and permissions)";
}
else
{
###/ SUCCESS /###
#/ Stripping of VAR_BASE_DIRECTORY for better viewing and linking
// $urlShow = str_replace(VAR_BASE_DIRECTORY,'',$newLocation);
$urlShow = $newLocation;
echo "<b> Uploaded successfully - <a href=\"$urlShow\" target=\"_blank\">$urlShow</a></b>";
}
}
else
{
echo "<b>No file uploaded</b>";
}
echo "<br />";
}
return;
}
循环似乎被完全忽略了:
// check file type if needed
if (count($exts)) {
if (!@in_array($file_ext[i], $exts))
{
echo "Files of this type are not allowed for upload.";
break;
}
}
我上传了JPG,JPEG,GIF,GIF,基本上是我试图限制的任何东西,所有东西都上传得很好。没有任何东西经过正确验证。这些值甚至被添加到数据库中。有人能解释我做错了什么吗?为什么不调用回音和中断
谢谢
Daz.不应该有任何
i
$file\u ext
不是数组,它只是一个扩展值
if (!@in_array($file_ext, $exts))
这是因为PHP找不到名为
$exts
的非局部变量(我看不到它是在函数中定义的)
你有两条路:
- 在函数内部将该变量定义为
global$vars
- 将其定义为局部变量
if (!@in_array($file_ext, $exts))