I’m doing a multiple file (JPEG, ZIP, JAVA JAR) upload via an HTML form and in some cases the JAR file refuses to upload (The JPEG and ZIP have no problem). I do extensive file testing in the PHP code (below), but no error is returned. Sometimes, everything works, and other times, the JAR doesn’t upload. I thought I narrowed the issue down to Safari users, but I’ve had one report of the issue occurring on Firefox. Any help would be appreciated.
//*****************************************************
//The actions I’m calling to construct the BuildArtwork Class and test the file uploads
//*****************************************************
$artMaker = new BuildArtwork($username, $POST['title'], $FILES['thumbnail'], $FILES['filename'], $FILES['sourcecode']); $artMaker->testUpload($FILES['thumbnail'], "img"); $artMaker->testUpload($FILES['filename'], "art"); $artMaker->testUpload($_FILES['sourcecode'], "zip");
<?php
class BuildArtwork{
public $coder_id;
public $max_file_size;
public $upload_required;
public $error;
public $title;
public $thumbnail;
public $filename;
public $sourcecode;
function __construct($coder_id, $title, $thumbnail, $filename, $sourcecode){
$this->coder_id = $coder_id;
$this->max_file_size = 4000000;
$upload_required = true;
$error = "";
$this->title = $title;
$this->thumbnail = $thumbnail;
$this->filename = $filename;
$this->sourcecode = $sourcecode;
}
function testUpload($fileToTest, $type){
do {
/ Check for the mime type to be correct /
switch($type){
case "img":
if (!in_array($fileToTest['type'], array ('image/jpeg', 'image/pjpeg'))) {
$error = 1;
}
break;
case "art":
if (!in_array($fileToTest['type'], array ('application/octet-stream', 'application/x-shockwave-flash'))) {
$error = $fileToTest['type'];
}
break;
case "zip":
if ($fileToTest['type']!= 'application/zip'){
$error = 4;
};
break;
default:
break;
}
/ We check for all possible error codes we might get /
switch ($fileToTest['error']) {
case UPLOAD_ERR_INI_SIZE:
$error = 5;
break 2;
case UPLOAD_ERR_PARTIAL:
$error = 6;
break 2;
case UPLOAD_ERR_NO_FILE:
if ($upload_required) {
$error = 7;
break 2;
}
break 2;
case UPLOAD_ERR_OK:
case UPLOAD_ERR_FORM_SIZE:
if ($fileToTest['size'] > $this->max_file_size) {
$error = 8;
}
break 2;
default:
$error = 9;
}
} while (0);
if($error){
$this->setError($error);
}else{
$this->createArt($fileToTest);
}
}
function createArt($fileToMake){
// Make title lowercase to create folder name
$folderTitle = $this->strip_ext($this->filename['name']);
$folderTitle = strtolower($folderTitle);
// Create artwork folder within coder folder if folder doesn't already exist
$coderpath = "/home/mysite/mysite.com/folder/" . strtolower($this->coder_id) ."/". $folderTitle ."/";
// folder name must be unique to each artwork
if(!file_exists($coderpath)){
mkdir($coderpath, 0777);
}
//copy file to server
if($fileToMake == $this->thumbnail){
//resize image
$imgsize = getimagesize($this->thumbnail['tmp_name']);
if ($imgsize[0] != 150 || $imgsize[1] != 100){
exec("/usr/bin/convert " . $this->thumbnail['tmp_name'] . " -geometry 150x100! " . $this->thumbnail['tmp_name']);
}
//compress image to 85% quality
exec("/usr/bin/convert " . $this->thumbnail['tmp_name'] . " -quality 85 " . $this->thumbnail['tmp_name']);
copy($this->thumbnail['tmp_name'], $coderpath . $folderTitle . ".jpg");
//move file to permanent directory and rename to match filename
if (!move_uploaded_file($this->thumbnail['tmp_name'], $coderpath . $folderTitle . ".jpg")) {
$this->setError(11);
}
}else if($fileToMake == $this->filename){
copy($this->filename['tmp_name'], $coderpath . strtolower($this->filename['name']));
chmod($coderpath . $this->filename['name'], 0755);
//move file to permanent directory
if (!move_uploaded_file($this->filename['tmp_name'], $coderpath . strtolower($this->filename['name']))) {
$this->setError(12);
}
}else if($fileToMake == $this->sourcecode){
copy($this->sourcecode['tmp_name'], $coderpath . $folderTitle . ".zip");
//move zipped file to permanent directory and rename to match filename
if (!move_uploaded_file($this->sourcecode['tmp_name'], $coderpath . $folderTitle . ".zip")) {
$this->setError(13);
}
}
}
// strips extension to just the name
function strip_ext($name){
$ext = strrchr($name, '.');
if($ext !== false){
$name = substr($name, 0, -strlen($ext));
}
return $name;
}
// returns extension of a filename
function get_ext($name){
$ext = strrchr($name, '.');
if($ext !== false){
$ext = substr($ext, 1, strlen($ext));
}
return $ext;
}
function setError($number){
var_dump($_FILES);
header("Location:error.php?error=" . $number);
}
}
?>