Jump to content
  • 0

Загрузка нескольких файлов на сервер.


Нарек
 Share

Question

Вот так выглядит html

<input type='text' name="phone"><input type='file' id="upload1"  name="attachment[]"><input type='file' id="upload2"  name="attachment[]"><input type='file' id="upload3"  name="attachment[]"><input type='file' id="upload4"  name="attachment[]"><input type='file' id="upload5"  name="attachment[]"><input type='file' id="upload6"  name="attachment[]">

Пытаюсь отправить на сервер все заполненные инпуты с файлами и с телефоном вот этим скриптом.

	public function send() {		$phone = self::getvar('phone');		$from_email = self::mime_encode(self::$from_name,"UTF-8")." <".self::$from_email.">";		$tmpName = $_FILES['attachment']['tmp_name']; 		$fileType = $_FILES['attachment']['type']; 		$fileName = $_FILES['attachment']['name'];		$subject = 'Bla bla bla';		$text = "Bla bla: $phone";		if($fileName) {			$un        = strtoupper(uniqid(time()));			$head      = "From: $from_email\n";			$head     .= "To: ".self::$to_email."\n";			$head     .= "Subject: ".self::mime_encode($subject,'UTF-8')."\n";			$head     .= "X-Mailer: PHPMail Tool\n";			$head     .= "Mime-Version: 1.0\n";			$head     .= "Content-Type:multipart/mixed;";			$head     .= "boundary=\"----------".$un."\"\n\n";			$zag       = "------------".$un."\nContent-Type:text/plain; charset=UTF-8\n";			$zag      .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n";			$zag      .= "------------".$un."\n";			$zag      .= "Content-Type: application/octet-stream;";			$zag      .= "name=\"".$fileName."\"\n";			$zag      .= "Content-Transfer-Encoding:base64\n";			$zag      .= "Content-Disposition:attachment;";			$zag      .= "filename=\"".$fileName."\"\n\n";			$zag      .= chunk_split(base64_encode(file_get_contents($tmpName)))."\n";			mail(self::$to_email, self::mime_encode($subject,'UTF-8'), $zag, $head);		} else {			$from_email = self::mime_encode(self::$from_name,"UTF-8")." <".self::$from_email.">";			$head="From: ".self::$from_email."\r\n";			$head.="X-Mailer: Sertse Mailer\r\n";			$head.="Content-Type: text/plain; charset=UTF-8\r\n";			$head.="Content-Transfer-Encoding: 8bit\r\n";			$head.="X-Priority: 3\r\n";			mail(self::$to_email, self::mime_encode($subject,'UTF-8'), $text, $head);		}	}

Естественно не отправляет, наверняка тут нужен некий foreach который разобьет все по отдельности, да не знаю как) JQuery не отпускает меня учить php пока..

Буду благодарен за помощь)

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Получилось, что отправляет последний загруженный файл а не все..

public function send() {		$phone = self::getvar('phone');		$from_email = self::mime_encode(self::$from_name,"UTF-8")." <".self::$from_email.">";		foreach ($_FILES["attachment"]["error"] as $key => $error) {    if ($error == UPLOAD_ERR_OK) {        $tmpName = $_FILES["attachment"]["tmp_name"][$key];        $fileName = $_FILES["attachment"]["name"][$key];        move_uploaded_file($tmp_name, "data/$fileName");    }}		$subject = 'Bla bla bla';		$text = "Bla bla: $phone";		if($fileName) {			$un        = strtoupper(uniqid(time()));			$head      = "From: $from_email\n";			$head     .= "To: ".self::$to_email."\n";			$head     .= "Subject: ".self::mime_encode($subject,'UTF-8')."\n";			$head     .= "X-Mailer: PHPMail Tool\n";			$head     .= "Mime-Version: 1.0\n";			$head     .= "Content-Type:multipart/mixed;";			$head     .= "boundary=\"———-".$un."\"\n\n";			$zag       = "————".$un."\nContent-Type:text/plain; charset=UTF-8\n";			$zag      .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n";			$zag      .= "————".$un."\n";			$zag      .= "Content-Type: application/octet-stream;";			$zag      .= "name=\"".$fileName."\"\n";			$zag      .= "Content-Transfer-Encoding:base64\n";			$zag      .= "Content-Disposition:attachment;";			$zag      .= "filename=\"".$fileName."\"\n\n";			$zag      .= chunk_split(base64_encode(file_get_contents($tmpName)))."\n";			mail(self::$to_email, self::mime_encode($subject,'UTF-8'), $zag, $head);		} else {			$from_email = self::mime_encode(self::$from_name,"UTF-8")." <".self::$from_email.">";			$head="From: ".self::$from_email."\r\n";			$head.="X-Mailer: Sertse Mailer\r\n";			$head.="Content-Type: text/plain; charset=UTF-8\r\n";			$head.="Content-Transfer-Encoding: 8bit\r\n";			$head.="X-Priority: 3\r\n";			mail(self::$to_email, self::mime_encode($subject,'UTF-8'), $text, $head);		}	}
Link to comment
Share on other sites

  • 0

Так если переместить его в foreach то для каждого файла не будет отправляться отдельное письмо? Или я что-то не так понимаю?

Сейчас попробую.

 

Да, каждый файл отправляет отдельным письмом.

Edited by Нарек
Link to comment
Share on other sites

  • 0

Как я понял тебе нужно через цикл для каждого файла повторить вот эту часть 

                        $zag      .= "————".$un."\n";			$zag      .= "Content-Type: application/octet-stream;";			$zag      .= "name=\"".$fileName."\"\n";			$zag      .= "Content-Transfer-Encoding:base64\n";			$zag      .= "Content-Disposition:attachment;";			$zag      .= "filename=\"".$fileName."\"\n\n";			$zag      .= chunk_split(base64_encode(file_get_contents($tmpName)))."\n";

и добавить в конец к заголовкам
проще всё это делается через phpMailer

Link to comment
Share on other sites

  • 0

Как я понял тебе нужно через цикл для каждого файла повторить вот эту часть 

                        $zag      .= "————".$un."\n";			$zag      .= "Content-Type: application/octet-stream;";			$zag      .= "name=\"".$fileName."\"\n";			$zag      .= "Content-Transfer-Encoding:base64\n";			$zag      .= "Content-Disposition:attachment;";			$zag      .= "filename=\"".$fileName."\"\n\n";			$zag      .= chunk_split(base64_encode(file_get_contents($tmpName)))."\n";

и добавить в конец к заголовкам

проще всё это делается через phpMailer

Мне  надо чтобы все файлы отправлялись одним письмом )

Edited by Нарек
Link to comment
Share on other sites

  • 0

ну, а я о чем
у тебя и будет что то типа 

 

                        $zag      .= "————".$un."\n";			$zag      .= "Content-Type: application/octet-stream;";			$zag      .= "name=\"".$fileName1."\"\n";			$zag      .= "Content-Transfer-Encoding:base64\n";			$zag      .= "Content-Disposition:attachment;";			$zag      .= "filename=\"".$fileName1."\"\n\n";			$zag      .= chunk_split(base64_encode(file_get_contents($tmpName1)))."\n";                        $zag      .= "————".$un."\n";			$zag      .= "Content-Type: application/octet-stream;";			$zag      .= "name=\"".$fileName2."\"\n";			$zag      .= "Content-Transfer-Encoding:base64\n";			$zag      .= "Content-Disposition:attachment;";			$zag      .= "filename=\"".$fileName2."\"\n\n";			$zag      .= chunk_split(base64_encode(file_get_contents($tmpName2)))."\n";

и т д 

Link to comment
Share on other sites

  • 0
public function send() {		$phone = self::getvar('phone');		$from_email = self::mime_encode(self::$from_name,"UTF-8")." <".self::$from_email.">";			$subject = 'Bla bla bla';		$text = "Bla bla: $phone";		if($fileName) {			$un        = strtoupper(uniqid(time()));			$head      = "From: $from_email\n";			$head     .= "To: ".self::$to_email."\n";			$head     .= "Subject: ".self::mime_encode($subject,'UTF-8')."\n";			$head     .= "X-Mailer: PHPMail Tool\n";			$head     .= "Mime-Version: 1.0\n";			$head     .= "Content-Type:multipart/mixed;";			$head     .= "boundary=\"———-".$un."\"\n\n";			$zag       = "————".$un."\nContent-Type:text/plain; charset=UTF-8\n";			$zag      .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n";	                foreach ($_FILES["attachment"]["error"] as $key => $error) {                           if ($error == UPLOAD_ERR_OK) {                               $tmpName = $_FILES["attachment"]["tmp_name"][$key];                               $fileName = $_FILES["attachment"]["name"][$key];                               move_uploaded_file($tmp_name, "data/$fileName");  // что за переменная $tmp_name???                               $zag      .= "————".$un."\n";		               $zag      .= "Content-Type: application/octet-stream;";			       $zag      .= "name=\"".$fileName."\"\n";			       $zag      .= "Content-Transfer-Encoding:base64\n";			       $zag      .= "Content-Disposition:attachment;";			       $zag      .= "filename=\"".$fileName."\"\n\n";			       $zag      .= chunk_split(base64_encode(file_get_contents($tmpName)))."\n";                            }                        }						mail(self::$to_email, self::mime_encode($subject,'UTF-8'), $zag, $head);		} else {			$from_email = self::mime_encode(self::$from_name,"UTF-8")." <".self::$from_email.">";			$head="From: ".self::$from_email."\r\n";			$head.="X-Mailer: Sertse Mailer\r\n";			$head.="Content-Type: text/plain; charset=UTF-8\r\n";			$head.="Content-Transfer-Encoding: 8bit\r\n";			$head.="X-Priority: 3\r\n";			mail(self::$to_email, self::mime_encode($subject,'UTF-8'), $text, $head);		}	}

что то типа этого.... 

Link to comment
Share on other sites

  • 0

Теперь отправляет только текстовое поле..

Вот весь скрипт для наглядности

<?phpclass Sendform {	private static $from_name = 'asd';	private static $from_email = 'mailer@asd.com';	private static $to_email = 'asd@bk.ru';public function send() {		$phone = self::getvar('phone');		$from_email = self::mime_encode(self::$from_name,"UTF-8")." <".self::$from_email.">";			$subject = 'Bla bla bla';		$text = "Bla bla: $phone";		if($fileName) {			$un        = strtoupper(uniqid(time()));			$head      = "From: $from_email\n";			$head     .= "To: ".self::$to_email."\n";			$head     .= "Subject: ".self::mime_encode($subject,'UTF-8')."\n";			$head     .= "X-Mailer: PHPMail Tool\n";			$head     .= "Mime-Version: 1.0\n";			$head     .= "Content-Type:multipart/mixed;";			$head     .= "boundary=\"———-".$un."\"\n\n";			$zag       = "————".$un."\nContent-Type:text/plain; charset=UTF-8\n";			$zag      .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n";	                foreach ($_FILES["attachment"]["error"] as $key => $error) {                           if ($error == UPLOAD_ERR_OK) {                               $tmpName = $_FILES["attachment"]["tmp_name"][$key];                               $fileName = $_FILES["attachment"]["name"][$key];                               move_uploaded_file($tmp_name, "data/$fileName");                               $zag      .= "————".$un."\n";		               $zag      .= "Content-Type: application/octet-stream;";			       $zag      .= "name=\"".$fileName."\"\n";			       $zag      .= "Content-Transfer-Encoding:base64\n";			       $zag      .= "Content-Disposition:attachment;";			       $zag      .= "filename=\"".$fileName."\"\n\n";			       $zag      .= chunk_split(base64_encode(file_get_contents($tmpName)))."\n";                            }                        }						mail(self::$to_email, self::mime_encode($subject,'UTF-8'), $zag, $head);		} else {			$from_email = self::mime_encode(self::$from_name,"UTF-8")." <".self::$from_email.">";			$head="From: ".self::$from_email."\r\n";			$head.="X-Mailer: Sertse Mailer\r\n";			$head.="Content-Type: text/plain; charset=UTF-8\r\n";			$head.="Content-Transfer-Encoding: 8bit\r\n";			$head.="X-Priority: 3\r\n";			mail(self::$to_email, self::mime_encode($subject,'UTF-8'), $text, $head);		}	}	private static function mime_encode($text,$charset) {         return "=?".$charset."?B?".base64_encode($text)."?=";	}		private static function getvar($phone) {		return addslashes(htmlspecialchars(strip_tags($_POST[$phone])));	}	}$reg = new Sendform;if(isset($_POST['phone'])) $reg->send();?>
Edited by Нарек
Link to comment
Share on other sites

  • 0

Вообщем - забивай и используй phpMailer. там все понятнее и с заголовками возится не нужно http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail


Ааа... это потому что там зачем то if($fileName) стоит

Link to comment
Share on other sites

  • 0
Ааа... это потому что там зачем то if($fileName) стоит

A что должно стоять?) Я тоже обратил внимания, что такой переменной как бы нет сверху, но до конца не был уверен что вместо него поставить)) 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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. See more about our Guidelines and Privacy Policy