Я пытаюсь создать функцию, в которой пользователь выбирает изображения acf, создает zip-файл, а затем отправляет его по электронной почте, но мой код не создает zip-файл. Я попытался проверить zip->close(), и это вернуло ложное значение. Я также попробовал это без использования ajax, и это работает! Не знаю, почему это не работает при использовании в качестве функции. Пожалуйста, проверьте код ниже.
$ret = array(
'is_error' => false,
'msg' => array(),
);
if (empty($_POST['email'])) {
$ret['is_error'] = true;
$ret['msg'][] = 'Email address is a required field';
}
if ($ret['is_error'] == false) {
unset($_POST['action']);
$images = $_POST['files'];
if( $images ){
$destination = 'wp-content/uploads/downloads/' .time() . '.zip';
if ( file_exists( $destination ) ){
$ret['msg'][] = 'zip file exists';
}
else {
// If the file doesn't already exist, create the file
$files = array();
foreach( $images as $singlefile ) {
// create an array of the image files in the gallery
$files[] = get_attached_file( $singlefile );
}
if( count( $files ) ) {
//Create a zip file in the location specified
$zip = new ZipArchive();
$zip->open( $destination, ZipArchive::CREATE );
foreach( $files as $file ) {
if ( file_exists($file) ) {
// if the file actually exists, add it to the zip file
$new_filename = substr($file,strrpos($file,'/') + 1);
$zip->addFile( $file, $new_filename );
}else{
$ret['msg'][] = $file;
$ret['msg'][] = 'Not added';
}
}
$zip->close();
readfile($destination);
$ret['msg'][] = $zip->close();
if(file_exists($destination)){
ob_clean();
ob_end_flush();
$protocols = array('https://', 'http://', 'https://www.', 'http://www.', 'www.');
$siteURL = str_replace($protocols, '', get_bloginfo('wpurl'));
$headers = 'From: '.get_bloginfo('name').' ';
$to = $_POST['email'];
$subject = "test";
$body = "test";
$attachments = array( $destination );
wp_mail($to, $subject, $body, $headers, $attachments );
unlink($destination);
$ret['msg'][] = 'Files sent. Please check your email';
}else{
$ret['msg'][] = 'Files does not exist';
}
} else {
// No images are found
$ret['msg'][] = 'No files found';
}
}
}
}
echo json_encode($ret);
exit();
Подробнее здесь: https://stackoverflow.com/questions/646 ... g-zip-file
PHP не создает zip-файл ⇐ Php
Кемеровские программисты php общаются здесь
1728738375
Anonymous
Я пытаюсь создать функцию, в которой пользователь выбирает изображения acf, создает zip-файл, а затем отправляет его по электронной почте, но мой код не создает zip-файл. Я попытался проверить zip->close(), и это вернуло ложное значение. Я также попробовал это без использования ajax, и это работает! Не знаю, почему это не работает при использовании в качестве функции. Пожалуйста, проверьте код ниже.
$ret = array(
'is_error' => false,
'msg' => array(),
);
if (empty($_POST['email'])) {
$ret['is_error'] = true;
$ret['msg'][] = 'Email address is a required field';
}
if ($ret['is_error'] == false) {
unset($_POST['action']);
$images = $_POST['files'];
if( $images ){
$destination = 'wp-content/uploads/downloads/' .time() . '.zip';
if ( file_exists( $destination ) ){
$ret['msg'][] = 'zip file exists';
}
else {
// If the file doesn't already exist, create the file
$files = array();
foreach( $images as $singlefile ) {
// create an array of the image files in the gallery
$files[] = get_attached_file( $singlefile );
}
if( count( $files ) ) {
//Create a zip file in the location specified
$zip = new ZipArchive();
$zip->open( $destination, ZipArchive::CREATE );
foreach( $files as $file ) {
if ( file_exists($file) ) {
// if the file actually exists, add it to the zip file
$new_filename = substr($file,strrpos($file,'/') + 1);
$zip->addFile( $file, $new_filename );
}else{
$ret['msg'][] = $file;
$ret['msg'][] = 'Not added';
}
}
$zip->close();
readfile($destination);
$ret['msg'][] = $zip->close();
if(file_exists($destination)){
ob_clean();
ob_end_flush();
$protocols = array('https://', 'http://', 'https://www.', 'http://www.', 'www.');
$siteURL = str_replace($protocols, '', get_bloginfo('wpurl'));
$headers = 'From: '.get_bloginfo('name').' ';
$to = $_POST['email'];
$subject = "test";
$body = "test";
$attachments = array( $destination );
wp_mail($to, $subject, $body, $headers, $attachments );
unlink($destination);
$ret['msg'][] = 'Files sent. Please check your email';
}else{
$ret['msg'][] = 'Files does not exist';
}
} else {
// No images are found
$ret['msg'][] = 'No files found';
}
}
}
}
echo json_encode($ret);
exit();
Подробнее здесь: [url]https://stackoverflow.com/questions/64612299/php-not-creating-zip-file[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия