コード例 #1
0
function doUploadImage($passed_mime = null)
{
    /***
     * Works with the default $_FILES object and handles image
     * uploads.
     *
     * It then creates a thumbnail for easy display.
     *
     * It takes one optional function argument
     *
     * @param passed_mime The mime type of the file (to be passed to client)
     *
     * It has the following optional parameters, that should be
     * provided in the POST / GET request:
     *
     *
     * @param uploadpath the path, relative to this file, to save the
     * uploaded images. Should end in a trailing slash.
     *
     * @param thumb_width the maximum width of a thumbnail. (Aspect
     * ratio will be maintained)
     *
     * @param thumb_height the maximum height of a thumbnail (Aspect
     * ratio will be maintained)
     ***/
    if (empty($_FILES)) {
        return array('status' => false,'error' => 'No files provided','human_error' => 'Please provide a file to upload');
    }
    $temp = $_FILES['file']['tmp_name'];
    $uploadPath = $_REQUEST['uploadpath'];
    $savePath = dirname(__FILE__).'/'.$uploadPath;
    if (!file_exists($savePath)) {
        if(!mkdir($savePath)) {
        return array(
            'status' => false,
            'error' => "Bad path '$savePath'",
            'human_error' => 'There is a server misconfiguration preventing your file from being uploaded',
        );
        }
    }
    $file = $_FILES['file']['name'];
    $exploded = explode('.', $file);
    $extension = array_pop($exploded);
    $fileName = md5($file.microtime_float());
    $newFilePath = $fileName.'.'.$extension;
    $fileWritePath = $savePath.$newFilePath;
    # We want to suppress the warning on move_uploaded_file, or else
    # it'll return an invalid JSON response
    #error_reporting(0); # Disable this for debugging
    // ini_set("error_log","/usr/local/web/amphibian_disease/error-admin.log");
    // ini_set("display_errors",1);
    // ini_set("log_errors",1);
    // error_reporting(E_ALL);
    $status = move_uploaded_file($temp, $fileWritePath);
    $uploadStatus = array('status' => $status,'original_file' => $file,'wrote_file' => $newFilePath,'full_path' => getRelativePath($fileWritePath));
    if (!$status) {
        # We bugged out on completing the upload. Return this status.
        # Formal mime type:
        # https://secure.php.net/manual/en/fileinfo.constants.php
        $finfo = finfo_open(FILEINFO_MIME);
        $mime = finfo_file($finfo, $temp);
        finfo_close($finfo);
        # Return the "real" path for debugging
        $uploadStatus["error"] = "Could not move uploaded file to destination ( Destination directory: ".realpath($savePath).")";
        $uploadStatus["mime"] = "Formal mime: '".$mime."'";
        $uploadStatus["human_error"] = "There was a problem saving your file to the server";
        return $uploadStatus;
    }
    # $uploadStatus["s3"] = copyToS3($fileWritePath, $newFilePath);
    # OK, create the thumbs.
    if (intval($_REQUEST['thumb_width']) > 0) {
        $thumb_max_width = intval($_REQUEST['thumb_width']);
    } else {
        $thumb_max_width = 640;
    }
    if (intval($_REQUEST['thumb_height']) > 0) {
        $thumb_max_height = intval($_REQUEST['thumb_height']);
    } else {
        $thumb_max_height = 480;
    }
    $fileThumb = $savePath.$fileName.'-thumb.'.$extension;
    $resizeStatus = ImageFunctions::staticResizeImage($fileWritePath, $fileThumb, $thumb_max_width, $thumb_max_height);
    # $resizeStatus["s3"] = copyToS3($resizeStatus['output'], $fileName.'-thumb.'.$extension);
    $resizeStatus['output'] = getRelativePath($resizeStatus['output']);
    $uploadStatus['resize_status'] = $resizeStatus;
    $uploadStatus['thumb_path'] = $resizeStatus['output'];
    $uploadStatus["wrote_thumb"] = str_replace("uploaded/","",$resizeStatus["output"]);
    $uploadStatus['mime_provided'] = $passed_mime;
    return $uploadStatus;
}
コード例 #2
0
function saveEntry($get)
{
    /***
     * Save a new taxon entry
     ***/
    $data64 = $get["data"];
    $enc = strtr($data64, '-_', '+/');
    $enc = chunk_split(preg_replace('!\\015\\012|\\015|\\012!', '', $enc));
    $enc = str_replace(' ', '+', $enc);
    $data_string = base64_decode($enc);
    $data = json_decode($data_string, true);
    if (!isset($data["id"])) {
        # The required attribute is missing
        $details = array("original_data" => $data64, "decoded_data" => $data_string, "data_array" => $data);
        return array("status" => false, "error" => "POST data attribute \"id\" is missing", "human_error" => "The request to the server was malformed. Please try again.", "details" => $details);
    }
    # Add the perform key
    global $db;
    $ref = array();
    $ref["id"] = $data["id"];
    unset($data["id"]);
    try {
        $result = $db->updateEntry($data, $ref);
        # Now, we want to do image processing if an image was alerted
        $imgDetails = false;
        if (!empty($data["image"])) {
            $img = $data["image"];
            $imgDetails = array("has_provided_img" => true);
            # Process away!
            $file = dirname(__FILE__) . "/" . $img;
            $imgDetails["file_path"] = $file;
            $imgDetails["relative_path"] = $img;
            if (file_exists($file)) {
                $imgDetails["img_present"] = true;
                # Resize away
                try {
                    $i = new ImageFunctions($file);
                    $thumbArr = explode(".", $img);
                    $extension = array_pop($thumbArr);
                    $outputFile = dirname(__FILE__) . "/" . implode(".", $thumbArr) . "-thumb." . $extension;
                    $imgDetails["resize_status"] = $i->resizeImage($outputFile, 256, 256);
                } catch (Exception $e) {
                    $imgDetails["resize_status"] = false;
                    $imgDetails["resize_error"] = $e->getMessage();
                }
            } else {
                $imgDetails["img_present"] = false;
            }
        }
    } catch (Exception $e) {
        return array("status" => false, "error" => $e->getMessage(), "humman_error" => "Database error saving", "data" => $data, "ref" => $ref, "perform" => "save");
    }
    if ($result !== true) {
        return array("status" => false, "error" => $result, "human_error" => "Database error saving", "data" => $data, "ref" => $ref, "perform" => "save");
    }
    return array("status" => true, "perform" => "save", "data" => $data, "img_details" => $imgDetails);
}