How to upload .txt file through PHP Script to Github

So I’ve been working on this script for the past 3 days for my company to get a log file automatically sent to my Github repository. I expected this to be quite easy actually (boy was I mistaken) and have no idea how to use the API to send over information. After doing quite a bit of research, I found another user’s post here:

However, this states to require a sha.txt file, which I don’t understand how to get. According to the code another user provided, you have to already have the file in your repository (or maybe I’m misunderstanding). How do I get a sha file for something that doesn’t exist? Can someone please assist me?

For clarity, I’m using my companies cpanel > phpmyadmin section to utilize the script (so shell_exec and generally shell_exec commands are off the table). Here is the transfer file that I’m speaking about:

<?php
$file_git = "wall.jpg";
$data_git = array(
'sha'=>file_get_contents("sha.txt"),
'message'=>'image',
'content'=> base64_encode($file_git),
'committer'=> array(
'name'=>'Jacob',
'email' => '45331093+greenandgreen@users.noreply.github.com'
)
);
$data_string_git = json_encode($data_git);
$ch_git = curl_init('https://api.github.com/repos/YOUR_REPO/contents/wall.jpg');
curl_setopt($ch_git, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch_git, CURLOPT_POSTFIELDS, $data_string_git);
curl_setopt($ch_git, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_git, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 YaBrowser/19.9.3.314 Yowser/2.5 Safari/537.36',
'Authorization: token PLACE_YOUR_PERSONAL_TOKEN_HERE'
));
$result_git = curl_exec($ch_git);
echo $result_git;
$p_git = json_decode($result_git);
file_put_contents("sha.txt",$p_git->content->sha);
?>

TLDR; I’m trying to get an example or assistance on how to pass a file through php script to my github repository.

Okay, so I’ve found that I can get the sha information by calling the PHP method sha1_file() but I’m still unclear on how that will code piece above listed is used to send a file to Github…

So I found a few corrections that get me closer to the end result.

  1. The code listing above works only for public repositories.
  2. The curl_init requires the owner name before the repos (joshg/repos/updateCollection/contents/{file path}
  3. The name is the only piece actually sending over with this script; the contents of the file do not get moved most likely as a result of this piece here that is displayed from the echo:

{“verified”:false,“reason”:“unsigned”,“signature”:null,“payload”:null}}}

Thanks for this post. I was also facing the same issue and with the above correction I have been able to get through.

Did your file actually send through, or just the name? I still can’t get the file across.