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($fil ename) ;
$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_uploade d_file($_FILES['resume']['tmp_name'], $target))
{
############### ## Insert in Database ############### ######
$con = mysql_connect(" localhost","dbu sr","dbpass") ;
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_d b("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($co n)
?>[/PHP]
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($fil ename) ;
$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_uploade d_file($_FILES['resume']['tmp_name'], $target))
{
############### ## Insert in Database ############### ######
$con = mysql_connect(" localhost","dbu sr","dbpass") ;
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_d b("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($co n)
?>[/PHP]
Comment