BubbaLovesCheese
Active member
Hi,
I'm working on a PHP script that uses a
I'm having some issues with the correct format of the directory path, and I don't know how to deal with the different file extension (jpg, jpeg, png, etc)
The csv looks like this: (And it can be adjusted easily)
And this is my php script.
Maybe it might help to put the avatars in the same directory as the PHP script. However, there are 149,000 users to go through.
Any suggestions would be much appreciated.
I'm working on a PHP script that uses a
.csv
file with userid
and avatar file path
in it to upload Avatar files from a local directory.I'm having some issues with the correct format of the directory path, and I don't know how to deal with the different file extension (jpg, jpeg, png, etc)
The csv looks like this: (And it can be adjusted easily)
Code:
userid, avatar path
1244,/uploads/avatars/1628.jpg
1245,/uploads/avatars/1629.jpeg
1246,/uploads/avatars/1631.png
1247,/uploads/avatars/1633.bmp
And this is my php script.
PHP:
<?php
// 1. file path $filePath
// 2. XF-Api-Key $apiKey It is a Super user key
// 3. XenForo base URL $url The primary URL. Do not include a trailing "/"
// 4. csv file should be : userid, avatar file path
$filePath = "avatar-doc.csv"; //csv file path
$apiKey = 'blZNOfSz-lad5Bhey4iWczSejtceKLX9'; //XF-Api-Key
$boardUrl = 'https://test-forum-2.mysite.com'; //XenForo base URL
//Check if csv file is readable
if (($handle = fopen($filePath, "r")) !== FALSE)
{
//Read file line by line till end
while (($data = fgetcsv($handle)) !== FALSE)
{
{
//Upload the avatar for each user
//set header
$headers = array(
'Content-type: application/x-www-form-urlencoded',
'XF-Api-Key: '.$apiKey
);
//set post body
$post = [
'userid' => $data[0], // userid
'avatar' => $data[1], // avatar path
'api_bypass_permissions' => 1 // bypass the context user's permissions
];
$post = http_build_query($post);
//set url
$url = $boardUrl.'/api/users/$data[0]/avatar';
//post request for creating user
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
// convert json response to array
$response = json_decode($json, 'true');
if(isset($response['success']) AND $response['success'] == 1)
{
//message for success
echo "Successfully created Avatar for " . $data[0] . "\n";
}
else
{
//message for failure due to some reason
echo "There was an ERROR adding Avatar for " . $data[0] . "\n";
}
}
}
}
Maybe it might help to put the avatars in the same directory as the PHP script. However, there are 149,000 users to go through.
Any suggestions would be much appreciated.