UploadScreenShot Without Java

January 13th, 2010 admin Posted in Web Development No Comments »

We’ve added a basic upload fall back option so that users without Java enabled in their browser can still use UploadScreenShot.com.

Below is a screen capture of what the site now looks like, with a file upload form, when you visit the site with Java disabled:

Click here to view full size

AddThis Social Bookmark Button

Uploading To FTP Using PHP and CURL

October 15th, 2009 admin Posted in Web Development No Comments »

An interesting project that I was working on today required grabbing remote files to a server and then pushing them to a remote FTP server.

PHP has built in FTP functions, but there are many reasons that you might want to use CURL instead.

Here is the code that I used to upload to FTP, slightly modified for portability:

<?php
$ch = curl_init();
$localfile = “test.tar”;
$fp = fopen($localfile, ‘r’);

curl_setopt($ch, CURLOPT_URL, ‘ftp://username:password@ftp.domain.com/public_html/filesfromscript/’.$localfile);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));

curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);

if ($error_no == 0) {
$message = ‘File uploaded successfully.’;
} else {
$message = “File upload error: $error_no. Error codes explained here http://curl.haxx.se/libcurl/c/libcurl-errors.html”;
}
echo $message;
?>

AddThis Social Bookmark Button

Checking Remote File Size With PHP

October 15th, 2009 admin Posted in Web Development No Comments »

If you’re going to grab a file from a remote server using PHP it may be useful to know how large the file is before grabbing it. We can check the remote file headers for the file size by using CURL like so:

function remote_filesize($url, $user = “”, $pw = “”) {
ob_start();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);

if(!empty($user) && !empty($pw))
{
$headers = array(’Authorization: Basic ‘ . base64_encode(”$user:$pw”));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

$ok = curl_exec($ch);
curl_close($ch);
$head = ob_get_contents();
ob_end_clean();

$regex = ‘/Content-Length:s([0-9].+?)s/’;
$count = preg_match($regex, $head, $matches);

return isset($matches[1]) ? $matches[1] : “unknown”;
}

To use the function we could do something like this:

$file=”http://img1.putpic.com/images/main/10/28719152288-orig.jpg”;
echo remote_filesize($file);

AddThis Social Bookmark Button

Shell Script To Report Network Usage, bytes/sec

July 11th, 2009 admin Posted in System Admin, Web Development No Comments »

In distributed apps it is often important to know the current network throughput speed on a particular server to help balance file serving requests etc.

Installing MRTG or other software is not always desirable or possible. I created the shell script below to output the eth0 outbound transfer rate in bytes/second. To calculate megabits per second simply divide the output by 131072.

You could post the data to a remote URL (e.g. a master health server) or write it to an xml file for reading by remote server or app, etc.

The speed returned is the average throughput over a 55 second period, you can adjust this as needed. Tested on CentOS 5.

#!/bin/bash
txfirst=`ifconfig eth0 | grep “TX bytes:” | cut -d “:” -f3 | cut -d ” ” -f1`
datefirst=`date +%s`
loop=1
while [ loop=1 ]
do
txnew=`ifconfig eth0 | grep “TX bytes:” | cut -d “:” -f3 | cut -d ” ” -f1`
datenew=`date +%s`

if [ $txnew -gt $txfirst ]
then
transferred=$((txnew-txfirst))
timelapsed=$((datenew-datefirst))
if [ $x > 0 ]
then
bytessec=$(($transferred/$timelapsed))
echo “$bytessec bytes/sec”
fi
txfirst=$((txnew))
datefirst=$((datenew))
else
txfirst=$((txnew))
datefirst=$((datenew))

fi
sleep 55
x=$(($x+1))
done

AddThis Social Bookmark Button