Skip to content

Instantly share code, notes, and snippets.

@greghunt
Created April 16, 2014 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greghunt/10881232 to your computer and use it in GitHub Desktop.
Save greghunt/10881232 to your computer and use it in GitHub Desktop.
PHP Save Upload Script
function save_upload( $_FILES = false ) {
$photos = array();
if( $_FILES ) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
foreach( $_FILES as $name=>$photo ) {
$temp = explode(".", $photo["name"]);
$filename = $temp[0];
$extension = end($temp);
$newfile = $filename . '-' . time() . '.' . $extension;
$type = $photo["type"];
$size = $photo["size"];
$name = explode('-',$name);
$name = $name[1];
if( $size > 5000000 ) {
$_SESSION['error'] = "The file ($filename) you tried uploading is too large ($size).";
} else {
if ((( $type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/jpg")
|| ($type == "image/pjpeg")
|| ($type == "image/x-png")
|| ($type == "image/png"))
&& in_array($extension, $allowedExts)) {
if ($photo["error"] > 0) {
$_SESSION["error"] = $photo["error"];
} else {
//Success
if (file_exists(APP_PATH . '/uploads/' . $newfile)) {
$_SESSION["notice"] = $newfile . " already exists.";
} else {
move_uploaded_file($photo["tmp_name"], APP_PATH . '/uploads/' . $newfile);
$imagedetails = getimagesize(APP_PATH . '/uploads/' . $newfile);
$photos[$name]['filename'] = $newfile;
$photos[$name]['type'] = $type;
$photos[$name]['size'] = $size;
$photos[$name]['width'] = $imagedetails[0];
$photos[$name]['height'] = $imagedetails[1];
$photos[$name]['name'] = $name;
}
}
}
}
}
}
return $photos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment