Search code examples
phpshellubuntuscriptings3cmd

Extra text comes from shell_exec command when executed from php


I am using s3cmd to get files from AWS to my ubuntu server.

s3cmd get s3://bucket/filename newfilename

It works fine when run from terminal of ubuntu.

But when I run from php like below

$query1 = 's3cmd get s3://bucket/filename newfilename';
$q1 = serialize(shell_exec($query1));
$data2 = array( 'data' => $q1);
header('Content-type: application/json');
echo json_encode( $data2 );

It works but response from json_encode it concatenated with extra text.

{"data":"s:122:\"File s3:............

What is s:122 and how to remove this. I am not looking for sub_string though.


Solution

  • When you serialize some value - in a result string there's type of value and value itself. s:122, you asked about, says that values is a string with length 122. So if you don't want this string appear - don't serialize value.