Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a form its working fine but i have two problems:

1. i am receiving error message :
Warning: mail(/var/log/phpmail.log): failed to open stream: Permission denied in /www1/xxx/Contact_Us.php on line 115.

2. i can't upload files more than 20KB even in my code i allow till 2 MB and on the php.ini upload_max_filesize is 8MB and post_max_size is 8MB.

So please can anyone help me with this two problems because i am really stuck there and don't know what to do and its the last problem i face and i can't solve it.
Thanks in advance;

What I have tried:

PHP
<?php
//set tiem zone
date_default_timezone_set('Asia/Dubai');
// Show errors
error_reporting(E_ALL);
ini_set('display_errors', 1);
	
// define Errors variables
$fnameErr = $lnameErr = $emailErr = $humanErr = $fileErr = $result = $result2 =  "" ;
	
// when we press submit do the following
if(isset($_POST['submit']))
{
// define contact form variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$design = $_POST['design'];
$country = $_POST['country'];
$comment = $_POST['comment'];
$human = $_POST['human'];

// define Checks variables
$check1 = $check2 = $check3  = $check4  = "";

	
// Let's do some checks	
// Checking the First Name
if(empty($_POST["fname"])){
	$fnameErr = "Name is Required";
}else{
	$fname = test_input($_POST["fname"]);
	// check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
      $fnameErr = "Only letters and white space allowed"; 
    }else{
		$check1 = 1;
	}
}
// Checking the Last Name	
if(empty($_POST["lname"])){
	$lnameErr = "Name is Required";
}else{
	$lname = test_input($_POST["lname"]);
	// check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
      $lnameErr = "Only letters and white space allowed"; 
    }else{
		$check2 = 1;
	}
}
//Checking the Email Adress
if(empty($_POST["email"])){
	$emailErr = "Email is Required";
}else{
	$email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }else{
		$check3 = 1;
	}
}
//Checking the Anti-Spam Question
if(empty($_POST["human"])){
	$humanErr = "Please Enter the Answer";
}else{
	if ($human != 4){
		$humanErr = "Please check your answer";
	}else{
		$check4 = 1;
	}
}
	
// checking the attachment
if(isset($_FILES) && (bool) $_FILES) {
  
	$allowedExtensions = array("pdf","doc","docx");
	
	$files = array();
	foreach($_FILES as $name=>$file) {
		$file_name = $file['name']; 
		$temp_name = $file['tmp_name'];
		$file_type = $file['type'];
		$file_size = $file['size'];
		$path_parts = pathinfo($file_name);
		$ext = $path_parts['extension'];

		if($file_size > 2000000){
        $fileErr = "Max allowed size is 2 MB";
        } else {
				if(!in_array($ext,$allowedExtensions)) {
				$fileErr = "File $file_name has the extensions $ext which is not allowed";    
				}else{
					array_push($files,$file);}
	
			   }
	}

	
// define email variables
$to = 'email@domain.com';
$from = "Job Application"; 
$subject = 'Job Application';
$message = 'From: '.$fname .$lname."\r\n".
           'E-mail: '.$email."\r\n".
           'Telephone: '.$tel."\r\n".
           'Designation: '.$design."\r\n".
           'Country Appled From: '.$country."\r\n".
           'Message: '.$comment."\r\n"."\r\n";
$headers = "From: $from";	
// boundary 
	$semi_rand = md5(time()); 
	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
	 
	// headers for attachment 
	$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
	 
	// multipart boundary 
	$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
	$message .= "--{$mime_boundary}\n";
	 
	// preparing attachments
	
	for($x=0;$x<count($files);$x++){
		$file = fopen($files[$x]['tmp_name'],"rb");
		$data = fread($file,filesize($files[$x]['tmp_name']));
		fclose($file);
		$data = chunk_split(base64_encode($data));
		$name = $files[$x]['name'];
		$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . 
		"Content-Disposition: attachment;\n" . " filename=\"$name\"\n" . 
		"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
		$message .= "--{$mime_boundary}\n";
	}	
	 
	
// Emailing the Contents if all Checks are correct	
if($check1 && $check2 && $check3 && $check4  == 1){
	mail($to, $subject, $message, $headers);
	$result =  "Message Sent Sucessfully";
}else{
	$result2 = "Message Can't be sent";
}
} }
 function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
	
?>
<!-- END OF PHP CODE -->
Posted
Updated 10-Apr-17 23:18pm

1 solution

The error is quite clear: "Permission denied".

Guessing that line 115 is this:
PHP
$file = fopen($files[$x]['tmp_name'],"rb");
you should check the file name there (e.g. by checking the return value of fopen() and showing an error message with the file name upon FALSE).

But before trying to access an uploaded file you should check for upload errors (see PHP: Handling file uploads - Manual[^]):
PHP
$file_err = $file['error'];

if($file_err != UPLOAD_ERR_OK){
    $fileErr = "File upload failed";
}
else if($file_size > 2000000){
    $fileErr = "Max allowed size is 2 MB";
}
// ...


[EDIT]
Member 13120366 commented:
its mai() function not the fopen() function
Searching the web for "php mail permission denied":
php - mail: failed to open stream: Permission denied? - Stack Overflow[^].
It is probably the mail log file. If so, setup a log file for usage with PHP (mail.log in php.ini) that has the necessary permissions.
[/EDIT]
 
Share this answer
 
v2
Comments
Member 13120366 11-Apr-17 5:20am    
its mai() function not the fopen() function
Jochen Arndt 11-Apr-17 5:32am    
See my updated answer.
For further questions it is always a good idea to indicate the code line for which the error is thrown.
Member 13120366 11-Apr-17 5:57am    
i saw your answer and i have created already a file on the server with path /var/log/phpmail.log with 777 permissions but still nothing new same error received
Jochen Arndt 11-Apr-17 6:14am    
And have you set the mail.log config too?

The log file should be stored in a directory where PHP already has write permission. /var/log is not such a directory and using 777 is not recommended.

Recommended:
- Create a directory (e.g. /var/log/php) as root
- Change the group of that directory to the group used by the web server (e.g. wwwrun, apache); owner will be still root
- Change the permission of the directory to allow the group to read and write (770)
- Specvify the file in php.ini: /var/log/php/mail.log
- Optionally create the file using touch, change owner and group to those used by the web server, and change permissions too (660)
Member 13120366 11-Apr-17 6:50am    
i have created the folder in the root directory also i changed the permission of the directory to 770 then i specified the file in php.ini now i am stuck at points
1- Change the group of that directory to the group used by the web server
2- create the file using touch, as i created it as new file and thats all so if there is a refrence link to create this file please send it to me
excuse me because all of that is new to me and thank for your help and i appreciate it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900