上传的文件验证无效 [英] uploaded file validation is not working

查看:93
本文介绍了上传的文件验证无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试验证要上传的文件类型和文件大小,如果这样正确,则插入db


这是我到目前为止所做的代码。到目前为止,我已经注释掉了那些不起作用的部件。



有人可以帮帮我。



[PHP]<?php

;


//此函数将扩展名与文件名的其余部分分开并返回

函数findexts($ filename)

{

$ filename = strtolower($ filename);

$ exts = split(" [/ \\。]",$ filename);

$ n = count($ exts)-1;

$ exts = $ exts [$ n];

返回$ exts;

}


//这将函数应用于我们的文件

$ ext = findexts($ _FILES [''resume' '][''名称'']) ;


//这一行为变量分配一个随机数。如果您愿意,也可以在此处使用时间戳。

$ ran =''resume _''。rand();


//这会获取您生成的随机数(或时间戳)并添加一个。最后,所以它已准备好附加文件扩展名。

$ ran2 = $ ran。"。" ;;


//这会将您要保存的子目录分配到...确保它存在!

$ target =" / var / www / virtual / domain / uploads /" ;;

//这结合了目录,随机文件名和扩展名


$ target = $ target。 。$ RAN2 $ EXT;


$ ok = 1;


/ * //检查文件类型

if($ ext == " doc"){

$ ok = 1;

}

elsif($ ext ==" pdf"){

$ ok = 1;

}

elsif($ ext ==" txt"){

$ ok = 1;

}

else {

$ ok = 0;

Echo"你只能上传MS Word,PDF或文本文件。请点击后退按钮,然后重试。< br>" ;;


} * /


//这里我们检查$ ok是否因错误设置为0

if($ ok == 0)

{

Echo"您只能上传MS Word,PDF或文本文件。请点击后退按钮,然后重试。< br>" ;;

}


//这是我们的尺寸条件

// if($ uploaded_size> 2000000)

// {

// echo"您的文件超出大小限制(MAX SIZE ALLOWED = 2 MB)。请点击后退按钮并更正此内容。< br>" ;;


//}



//如果一切正常我们会尝试上传它


//将简历写入服务器

if(move_uploaded_file($ _ FILES [''resume''] [''tmp_name''],$ target))

{


#################在数据库中插入########## ###########

$ con = mysql_connect(" localhost"," dbusr"," dbpass");

if(b) !$ con)

{

die(''无法连接:''。mysql_error());

} mysql_select_db(" db",$ con);

$ sql =" INSERT INTO apps(applicationID,salutation,first_name,last_name,dob,nationality,address,work,home,mobile,email,marital_status,salary, position,resume,createddate)

VALUES(NULL,''$ _POST [salutation]'',''$ _POST [first_name]'',''$ _POST [last_name]'','' $ _POST [dob]'',''$ _POST [nationality]'',''$ _POST [address]'',''$ _POST [work]'',''$ _POST [home]'',''$ _POST [mobile]'',''$ _POST [email]'',''$ _POST [marital_status]'',''$ _POST [salary]'',''$ _POST [position]'',''$ target'',NOW())" ;;


if(!mysql_query($ sql) ,$ con))


{

die(''错误:''。 mysql_error());

}


echo&谢谢$ _POST [first_name] $ _POST [last_name]用于提交你的申请 ;;

}

else {


//如果不是

echo会给出错误抱歉,上传文件时出现问题。请单击后退按钮并更正文件,然后重试。;

}


mysql_close($ con)




?> [/ PHP]

Hi I am tryin to validate certian types of files to be uploaded and file size and if this follows this correctly then insert in db

this is the code i did so far. So far I commented out the parts that are not working.


Could somebody help me out please.


[PHP]<?php
;

//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

//This applies the function to our file
$ext = findexts ($_FILES[''resume''][''name'']) ;

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
$ran = ''resume_''.rand () ;

//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";

//This assigns the subdirectory you want to save into... make sure it exists!
$target = "/var/www/virtual/domain/uploads/";
//This combines the directory, the random file name, and the extension


$target = $target . $ran2.$ext;

$ok=1;

/* //Check file types
if ($ext == "doc") {
$ok=1;
}
elsif ($ext == "pdf") {
$ok=1;
}
elsif ($ext == "txt") {
$ok=1;
}
else {
$ok=0;
Echo "You may only upload MS Word, PDF or Text files. Please click the back button and try again.<br>";

}*/

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "You may only upload MS Word, PDF or Text files. Please click the back button and try again.<br>";
}


//This is our size condition
//if ($uploaded_size > 2000000)
//{
//echo "Your file is over the size limit (MAX SIZE ALLOWED = 2 MB). Please click the back button and correct this.<br>";

//}



//If everything is ok we try to upload it

//Writes the resume to the server
if(move_uploaded_file($_FILES[''resume''][''tmp_name''], $target))
{

################# Insert in Database #####################
$con = mysql_connect("localhost","dbusr","dbpass");
if (!$con)
{
die(''Could not connect: '' . mysql_error());
}mysql_select_db("db", $con);
$sql="INSERT INTO apps (applicationID, salutation, first_name, last_name, dob, nationality, address, work, home, mobile, email, marital_status, salary, position, resume, createddate)
VALUES (NULL, ''$_POST[salutation]'', ''$_POST[first_name]'', ''$_POST[last_name]'', ''$_POST[dob]'', ''$_POST[nationality]'', ''$_POST[address]'', ''$_POST[work]'', ''$_POST[home]'', ''$_POST[mobile]'', ''$_POST[email]'', ''$_POST[marital_status]'', ''$_POST[salary]'', ''$_POST[position]'', ''$target'', NOW())";

if (!mysql_query($sql,$con))

{
die(''Error: '' . mysql_error());
}


echo "Thank you $_POST[first_name] $_POST[last_name] for submitting your application ";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file. Please click the back button and correct the file then try again.";
}


mysql_close($con)




?>[/PHP]

推荐答案

filename)

{
filename)
{


filename = strtolower(
filename = strtolower(


filename);
filename) ;


这篇关于上传的文件验证无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆