Jump to content

File upload script problem


PRodgers4284

Recommended Posts

I am trying to add an upload script to a add form but im not sure where to put it in the code, can anyone help?

 

My code is

 

<?php 
$error_stat = 0; 
$jobtitle_message = '';
$jobcatergory_message = '';
$joblocation_message = '';
$employmenttype_message = '';
$salary_message = '';
$date_message = '';
$educationallevel_message = '';
$description_message = '';

if (isset($_POST['submit'])) { 


$jobtitle = $_POST['jobtitle']; 
$jobcatergory = $_POST['jobcatergory']; 
$joblocation = $_POST['joblocation']; 
$employmenttype= ($_POST['employmenttype']); 
$salary = $_POST['salary']; 
$date = $_POST['date']; 
$educationallevel = $_POST['educationallevel']; 
$description = $_POST['description']; 



//Error checking 



// Job Title check) 
if (!$jobtitle) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a username
$jobtitle_message = '*Please enter a job title*';
}

else if (ctype_digit($jobtitle)) { 
   $error_stat = 1; 
   $jobtitle_message .= '*Invalid Job Title*'; 
} 

else if ( preg_match( '/\W/', $jobtitle)){
     $error_stat = 1; 
    $jobtitle_message = '*Invalid jobtitle, letters only, no spaces*'; 

} 

$jobtitle = $_POST['jobtitle']; 
$jobtitle = trim($jobtitle); 

   if (strlen($jobtitle) > 30){ 
   $error_stat = 1; 
   $jobtitle_message = '*Job Title must be 20 characters or less*'; 
} 






//	Job Catergory Check)  
if ($jobcatergory == 'Please Select'){
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;
$jobcatergory_message = '*Please select a Job Catergory*';
}





//	Job Location Check)  
if ($joblocation == 'Please Select'){
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;
$joblocation_message = '*Please select a Job location*';
}


//	Employment Type Check)  
if ($employmenttype == 'Please Select'){
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;
$employmenttype_message = '*Please select Employment type*';
}





// Salary check) 
if (!$salary) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a username
$salary_message = '*Please enter job salary*';
}

else if (!ctype_digit($salary)) { 
   $error_stat = 1; 
   $salary_message .= '*Invalid salary*'; 
}




//Date check) 
if (empty($date)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a dob
$date_message = '*Please enter job closing date*';
}

//Check the format and explode into $parts
  elseif (!ereg("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", 
          $date, $parts)){
	$error_stat = 1; 	

//Set the message to tell the user the date is invalid
$date_message = '*Invalid date, must be DD/MM/YYYY format*';
}
    
  elseif (!checkdate($parts[2],$parts[1],$parts[3]))
  {
  $error_stat = 1; 
  
  //Set the message to tell the date is invalid for the month entered
$dob_message = '*Invalid dob, month must be between 1-12*';
}

elseif (intval($parts[3]) < 2008 || 
          intval($parts[3]) > intval(date("Y")))
  {
    
    $error_stat = 1; 

   //Set the message to tell the user the date is invalid for the year entered
$dob_message = '*Invalid dob, year must 2008 onwards*';
  }
  

// Job Title check) 
if (!$description) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a username
$description_message = '*Please enter a job description*';
}

$description = $_POST['description']; 
$description = trim($description); 

   if (strlen($description) > 150){ 
   $error_stat = 1; 
   $description_message = '*Job Title must be 150 characters or less*'; 
} 

  
  
  
//	Educational Level Check)  
if ($educationallevel == 'Please Select'){
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;
$educationallevel_message = '*Please select Educational level required*';
}

if($_FILES['userfile']['size'] > 2000000 ){
    //Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;
$filesize_message = '*Filesize too large *';

}

$fileTypes = array("application/pdf", "application/msword");

if( !in_array("{$_FILES['userfile']['type']}", $fileTypes) ){
    $error_stat = 1;
$filetype_message = '*Filetype not allowed *';

}

    
$account = mysql_fetch_array(mysql_query("SELECT * FROM employers WHERE username='" . $_SESSION["username"] . "'")); 

$username = $account["username"];

//Then, only run the query if there were no errors (if $error_stat still equals 0) 
if ($error_stat == 0) { 

   mysql_query("INSERT INTO jobs (username, jobtitle, jobcatergory, joblocation, employmenttype, salary, date, educationallevel, description, name, type, size, path) VALUES ('$username', '$jobtitle', '$jobcatergory', '$joblocation', '$employmenttype', '$salary', '$date', '$educationallevel', '$description', '$name', '$type', '$size', '$path')"); 

echo "<h3>Registration Successful!</h3>"; 
   echo "<p>Thankyou, <b>$username</b>,registration was successful</p>"; 
   echo "<p>login.</p>";
  	echo "<a href=\"index2.php\">Login</a>";
  	
}
}



//Then, for the form, only show it if 1) the form hasn't been submitted yet OR 2) there is an error 
if (!isset($_POST['submit']) || $error_stat == 1) { 



?> 

 

 

 

Heres is the upload code (I have this working just cant same to get it fitted in to the code)

 

$uploadDir = 'forms/'; 


$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

    // the files will be saved in filePath 
    $filePath = $uploadDir . $fileName;

    // move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
    $result    = move_uploaded_file($tmpName, $filePath);


include("database.php");

    if(!get_magic_quotes_gpc())
    {
        $fileName  = addslashes($fileName);
        $filePath  = addslashes($filePath);
    }  

Link to comment
https://forums.phpfreaks.com/topic/92325-file-upload-script-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.