0
0
0906ddd2021-09-10 15:49:56
PHP
0906ddd, 2021-09-10 15:49:56

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

2 answer(s)
N
Nadim Zakirov, 2021-09-10
@zkrvndm

So offhand I see an error when sending files:

for(let i = 0; i < filesCount; i++) {
          data.append(i, filesBlock[i]);
        }

S
Sergey, 2021-09-10
@sslion

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 question

Ask a Question

731 491 924 answers to any question