1

I'm trying to upload an image to WordPress Media Library via the REST API. This code works just fine:

<?php

  if (!empty($_GET['act'])) {
          echo "Did not send any media";
    $file = file_get_contents('/var/www/Digital_Signage/form/exampleFolder/26/example-video.mp4'); 
    $url = 'http://10.124.133.1:8182/wp-json/wp/v2/media/';
    $ch = curl_init();
    $username = 'gpda';
    $password = 'xxxx 1234 xxxx 1234 xxxx 1234';
 
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, 1 );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, [
            'Content-Disposition: form-data; filename="/var/www/Digital_Signage/form/exampleFolder/26/example-video.mp4"', 
            'Authorization: Basic ' . base64_encode( $username . ':' . $password ),
    ] );
    $result = curl_exec( $ch );
    curl_close( $ch ) ;
    print_r( json_decode( $result ) ) ;
      } else
?>
 
(.. your html ..)
<form action="script.php" method="get">
  <input type="hidden" name="act" value="run">
  <input type="submit" value="Aceptar">
</form>
 
<?php
?>

The problem comes when I Try to put a variable in “file_get_contents” and “Content-Disposition: form-data; filename…"



First I declare my variable ruta which comes from a URL

enter image description here

$folder_files=$_GET['ruta'];

If I

echo "/var/www/Digital_Signage/form/".$folder_files;

It outputs this

/var/www/Digital_Signage/form/exampleFolder/26/example-video.mp4



Then I try to use my variable ruta ($folder_files) on these two

$file = file_get_contents("/var/www/Digital_Signage/form/".$folder_files);
'Content-Disposition: form-data; filename=""/var/www/Digital_Signage/form/".$folder_files"',

But if I use this then It does not upload anything, what can I do????



Full code would be like this

<?php

  if (!empty($_GET['act'])) {
          echo "Did not send any media";
    $file = file_get_contents("/var/www/Digital_Signage/form/".$folder_files);    
    $url = 'http://10.124.133.1:8182/wp-json/wp/v2/media/';
    $ch = curl_init();
    $username = 'gpda';
    $password = 'xxxx 1234 xxxx 1234 xxxx 1234';

    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, 1 );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, [
            'Content-Disposition: form-data; filename=""/var/www/Digital_Signage/form/".$folder_files"',
            'Authorization: Basic ' . base64_encode( $username . ':' . $password ),
    ] );
    $result = curl_exec( $ch );
    curl_close( $ch ) ;
    print_r( json_decode( $result ) ) ;
      } else
?>

(.. your html ..)
<form action="script.php" method="get">
  <input type="hidden" name="act" value="run">
  <input type="submit" value="Aceptar">
</form>

<?php
?>
GBT55
  • 83
  • 5

0 Answers0