Last active
October 25, 2019 15:04
-
-
Save barmgeat/0f8e70d84c81ae404098e20ba1679bc4 to your computer and use it in GitHub Desktop.
note extension for sure html but for markup i make it js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static function postRequest($url, $postData, $headers){ | |
//init curl: | |
$ch = curl_init($url); | |
// Configuring curl options | |
$options = array( | |
CURLOPT_CUSTOMREQUEST => 'POST', | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POSTFIELDS => $postData, | |
CURLOPT_HTTPHEADER => $headers | |
); | |
// Setting curl options | |
curl_setopt_array( $ch, $options ); | |
// Getting results | |
$result = curl_exec($ch); // Getting jSON result string | |
$info = curl_getinfo($ch); | |
curl_close($ch); | |
$responseCode = $info['http_code']; | |
return $result; | |
} | |
public static function deleteRequest($url, $headers){ | |
$url = $url; | |
$options = array( | |
CURLOPT_RETURNTRANSFER=> TRUE, | |
CURLOPT_CUSTOMREQUEST => 'DELETE', | |
CURLOPT_HTTPHEADER => $headers | |
); | |
$ch = curl_init($url); | |
curl_setopt_array($ch, $options); | |
$result = curl_exec($ch); | |
return $result; | |
} | |
// patch request: | |
public static function patchRequest($url, $patchData, $headers){ | |
//init curl: | |
$ch = curl_init($url); | |
// Configuring curl options | |
$options = array( | |
CURLOPT_CUSTOMREQUEST => 'PATCH', | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POSTFIELDS => $patchData, | |
CURLOPT_HTTPHEADER => $headers | |
); | |
// Setting curl options | |
curl_setopt_array( $ch, $options ); | |
// Getting results | |
$result = curl_exec($ch); // Getting jSON result string | |
$info = curl_getinfo($ch); | |
curl_close($ch); | |
$responseCode = $info['http_code']; | |
return $result; | |
} | |
public static function getRequest($url){ | |
$url = $url; | |
$options = array( | |
CURLOPT_RETURNTRANSFER=> TRUE, | |
CURLOPT_CUSTOMREQUEST => 'GET' | |
); | |
$ch = curl_init($url); | |
curl_setopt_array($ch, $options); | |
$result = curl_exec($ch); | |
return $result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//function to upload files to server: | |
/// inputName: input name in the form | |
/// allowedExt: allowed extensions as array | |
/// dir folder to upload file to should be already created look ex: | |
// ex: /uploads/ -> upload folder in the root dir | |
// will rename the file to the time in mill sec and the extension. | |
// max size in Byte | |
function uploadFile($inputName, $allowedExt, $dir, $maxSize){ | |
$currentDir = getcwd(); | |
//dir should be already created | |
$uploadDirectory = $dir; | |
$errors = []; // Store all errors here | |
$fileName = $_FILES[$inputName]['name']; | |
$fileSize = $_FILES[$inputName]['size']; | |
$fileTmpName = $_FILES[$inputName]['tmp_name']; | |
$fileType = $_FILES[$inputName]['type']; | |
$fileExtension = strtolower(end(explode('.',$fileName))); | |
$newFileName = time() . '.' .$fileExtension ; | |
$uploadPath = $currentDir . $uploadDirectory . basename($newFileName); | |
//check if there extension validate: | |
if($allowedExt != null){ | |
if (! in_array($fileExtension, $allowedExt)) { | |
$errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file"; | |
} | |
} | |
if ($fileSize > $maxSize) { | |
$errors[] = "This file is more than ". $maxSize/1000000 ." MB. Sorry, it has to be less than or equal to ". $maxSize/1000000 ." MB"; | |
} | |
if (empty($errors)) { | |
$didUpload = move_uploaded_file($fileTmpName, $uploadPath); | |
if ($didUpload) { | |
return $uploadPath; | |
} else { | |
echo "An error occurred somewhere. Try again or contact the admin"; | |
} | |
} else { | |
foreach ($errors as $error) { | |
echo $error . "These are the errors" . "\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment