Skip to content

Instantly share code, notes, and snippets.

@ScottSmith95
Created February 16, 2014 05:52
Show Gist options
  • Save ScottSmith95/9029854 to your computer and use it in GitHub Desktop.
Save ScottSmith95/9029854 to your computer and use it in GitHub Desktop.
A script for custom image uploads via Tweetbot or any other app that uses a similar format. Creates short file names for uploaded items that are essentially unique by using the time and date uploaded (see line 22, 23). Adapted from http://www.macstories.net/news/tweetbot-for-mac-review/#customuploads.
<?php
$domain = "img.scottshar.es/";
//server-side directory
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self;
// Image filetype check source:
// http://designshack.net/articles/php-articles/smart-file-type-detection-using-php/
$tempFile = $_FILES['media']['tmp_name'];
$imginfo_array = getimagesize($tempFile);
$path = $_FILES['media']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
if ($imginfo_array !== false) {
$mime_type = $imginfo_array['mime'];
$mime_array = array("video/quicktime", "image/png", "image/jpeg", "image/gif", "image/bmp");
if (in_array($mime_type , $mime_array)) {
//generate filename (hours, minutes, seconds, day of the year)
while(file_exists($uploadFilename = $uploadsDirectory . date("Hisz") . '.' . $ext)){$now++;}
//"Hisz" = 24 hour time (with leading zeros), minutes (with leading zeros), seconds (with leading zeros), and day of the year
//upload the file to the webserver
@move_uploaded_file($tempFile, $uploadFilename);
//generate the filename that will be given to Tweetbot
$outputFilename = $domain . basename($uploadFilename);
//respond with JSON encoded media URL
$response = array(url=>$outputFilename);
echo json_encode($response);
}
}
else {
echo "This is not a valid image file";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment