PHP не создает zip-файлPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 PHP не создает zip-файл

Сообщение 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();


Подробнее здесь: https://stackoverflow.com/questions/646 ... g-zip-file
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»