Answer the question
In order to leave comments, you need to log in
Why doesn't move_uploaded_file work?
$('.request__button').on('click', function(e) {
var phone = $('#c_phone-number').val();
var email = $('#c_email').val();
if( typeof filesBlock == 'undefined' ) return;
var data = new FormData();
let action = 'request';
data.append( 'action', action );
data.append( 'phone', phone );
data.append( 'email', email );
for(let i = 0; i < filesCount; i++) {
data.append(i, filesBlock[i]);
}
data.append('upload_files', 1);
$.ajax({
url: myajax.url,
type: 'POST',
cache: false,
dataType: 'json',
processData: false,
contentType: false,
data: data,
}).done(function(data){
});
return false;
});
function request_action() {
$chat_id = 'свой номер ид';
$token = 'свой номер токена';
$phone = $_POST['phone'];
$email = $_POST['email'];
$text .= "<b>Сообщение с сайта</b>\n\n";
$text .= "Телефон: ".$phone."\n";
$text .= "Email: ".$email."\n";
if( isset( $_POST['upload_files'] ) ) {
$site = $_SERVER['DOCUMENT_ROOT'];
$path = '/wp-content/uploads/files';
$uploaddir = $site . $path;
// cоздадим папку если её нет
if( ! is_dir( $uploaddir ) ) mkdir( $uploaddir, 0755 );
$files = $_FILES; // полученные файлы
$done_files = array();
// переместим файлы из временной директории в указанную
foreach( $files as $file ){
$file_name = $file['name'];
if( move_uploaded_file( $file['tmp_name'], "$uploaddir/$file_name" ) ){
$done_files[] = realpath( "$uploaddir/$file_name" );
$text .= "Ссылка на файл: ".$done_files[]."\n";
}
}
$data = $done_files ? array('files' => $done_files ) : array('error' => 'Ошибка загрузки файлов.');
die( json_encode( $data ) );
}
$text = urlencode($text);
$str = "https://api.telegram.org/bot".$token."/sendMessage?chat_id=".$chat_id."&parse_mode=html&text=".$text;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
wp_die();
}
Answer the question
In order to leave comments, you need to log in
So offhand I see an error when sending files:
for(let i = 0; i < filesCount; i++) {
data.append(i, filesBlock[i]);
}
provided that the error is in move_uploaded_file
, check if the destination folder exists, check the write permissions to this folder, check the case of characters in the entire path to the destination folder, and of course there should not be Cyrillic in the path to the folder
, and it would be nice to dump the $ variable _FILES, maybe it's empty
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question