Skip to content

Instantly share code, notes, and snippets.

@PaulGwamanda
Created February 17, 2017 07:44
Show Gist options
  • Save PaulGwamanda/01e43c1fba92d63b74d9b006b932b602 to your computer and use it in GitHub Desktop.
Save PaulGwamanda/01e43c1fba92d63b74d9b006b932b602 to your computer and use it in GitHub Desktop.
File upload field in a contact form - creates a folder and uploads to a directory and can be retrieved by calling the variable $img_link
$target_dir = "uploads/";
$temp = explode(".", $_FILES["fileToUpload"]["name"]);
$newfilename = rand(1, 99999) . '.' . end($temp);
$target_file = $target_dir . '' . $newfilename;
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check !== false) {
$ERROR = "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
$ERROR = "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
$ERROR = "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 90000000000000) {
$ERROR = "Sorry, your file is too large.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
// $ERROR ="Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
//$ERROR = "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$img_link = 'http://deloitte.onlinereport.co.za/' . $target_file;
$ERROR = "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
} else {
$ERROR = "Sorry, there was an error uploading your file.";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment