0

I am trying to learn Symfony4 and I want to create an application with the Twitter API. The problem I have is that when I try to load an image in a form, I can not keep the value of an entry (the icon) in a variable $ _FILE and that's why it shows me this error.

What I intend is to publish a multimedia tweet, if instead of trying to upload the photo by form I put the route, if you publish the tweet with the photo, the stick with that way of proceeding is that I could not choose which photo to upload and it will always publish the one defined by the route.

This is my code below :

$session= new Session(); #en symfony la session se crea instanciando un objeto de la clase Session.
$session->start(); #para iniciar la sesion, llamamos al metodo start del objeto creado a partir de Session, en lugar de usar session_start() como veniamos haciendo en php plano.

d($session->set('media',$_FILES['file'])); # d() = vardump() pertenece a la libreria kint

$mensaje = $_POST['tweet']; #almacena el texto introducido en textarea
$respuesta = sendTweet($mensaje); #llama a la funcion para enviar tweet (en /lib/funcionesTwitter.php) y pasa como parametro el texto almacenado en textarea
d($json = json_decode($respuesta)); #descompone el resultado de la funcion sendTweet() en un json con los datos del tweet que luego llamaremos en la plantilla twig

/*claves de acceso de la api creada en twiter.developers*/
$accessToken="xxx";
$accessTokenSecret="xxx";
$consumerKey="xxx";
$consumerKeySecret="xxx";


$connection = new TwitterOAuth($consumerKey, $consumerKeySecret, $accessToken, $accessTokenSecret);

$foto= $session->get('media');

$media1 = $connection->upload('media/upload', ['media' => $foto]); #almacenamos la imagen en la variable y usamos el obj connection para llamar al metodo de la clase TwitterOAuth de la libreria con el mismo nombre.
$media2 = $connection->upload('media/upload', ['media' => '../doc/img/supernova2.jpg']);
$parameters = [
    'status' => $json->text,
    'media_ids' => implode(',', [$media1->media_id_string, $media2->media_id_string])
];
$result = $connection->post('statuses/update', $parameters);

#redirige al template twig con la ruta de esta funcion e inserta en el template las variables aqui definidas y que llamemos en el tpl.
return $this->render('TwitterTpl/twitter.html.twig', array(
  'tweets'=>'',
  'titulo'=>'',
  'json'=>$json,
  'imagen'=>$result
));

and this is the form that I have in the twig template:

<form class="fomulario" enctype="multipart/form-data" action="{{path('enviaTweetMulti')}}" method="post" >
    <label for="exampleFormControlTextarea1">Enviar tweet</label>
    <textarea class="form-control z-depth-1 mb-3" id="exampleFormControlTextarea6" rows="3" placeholder="Write something here..." rows="7" cols="20" maxlength="140" name="tweet"></textarea>

      <div class="form-group">
        <input type="file" class="form-control-file" id="exampleFormControlFile1">
      </div>

    <button type="submit" class="btn btn-outline-primary waves-effect" ><i class="fa fa-share ml-1"></i> Tweet</button>
  </form>

my idea is that the result was the power to choose a photo that was on my computer with a form and that when sending it, a tweet with the selected image was published on twitter

but as I said, it gives me the error Notice: Undefined index: file in this line $ session-> set ('media', $ _ FILES ['file'])

cisco
  • 165
  • 1
  • 8
  • 3
    `enctype="multipart/form-data"` are u using this in your
    or not?
    – devpro Jul 05 '19 at 14:50
  • @devpro yes, this is my form => `
    ` is in the twig template
    – cisco Jul 05 '19 at 14:54
  • `print_r($_FILES['file'])` use this before this line `d($session->set('media',$_FILES['file']));` and share the result – devpro Jul 05 '19 at 14:55
  • @devpro I can not execute the `print_r ()`, I write before `$ session` but I always get an error because when sending the form it redirects me to the path I indicated in the controller and already shows the bug in the browser – cisco Jul 05 '19 at 15:04
  • 1
    So the redirect will clear the `$_FILES` global array. You need to resolve that issue first. – waterloomatt Jul 05 '19 at 15:16

0 Answers0