Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function uploadImageAsync(uri) {
- let apiUrl = 'upload.php';
- let uriParts = uri.split('.');
- let fileType = uriParts[uriParts.length - 1];
- let formData = new FormData();
- formData.append('photo', {
- uri,
- name: `photo.${fileType}`,
- type: `image/${fileType}`,
- });
- let options = {
- method: 'POST',
- body: formData,
- headers: {
- Accept: 'application/json',
- 'Content-Type': 'multipart/form-data',
- },
- };
- return fetch(apiUrl, options);
- }
- <input type="file" name="img">
- if(file_exists($_FILES['img']['tmp_name']) && is_uploaded_file($_FILES['img']['tmp_name']))
- {
- $target = md5(time());
- $target_dir = "photos/";
- $target_file = $target.basename($_FILES["img"]["name"]);
- $uploadOk = 1;
- $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
- // Check if image file is a actual image or fake image
- $check = getimagesize($_FILES["img"]["tmp_name"]);
- if($check != false) {
- $uploadOk = 1;
- }
- else{
- echo "File is not an image.";
- $uploadOk = 0;
- }
- if ($_FILES["img"]["size"] > 50000000) {
- $uploadOk = 0;
- echo "Sorry, your file is too large.";
- }
- // Allow certain file formats
- if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
- && $imageFileType != "gif" ) {
- echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- $uploadOk = 0;
- }
- // Check if $uploadOk is set to 0 by an error
- if ($uploadOk == 0) {
- echo "Sorry, your image was not uploaded.";
- // if everything is ok, try to upload file
- } else {
- $target_file = strtr($target_file1,
- 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
- 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
- $target_file = preg_replace('/([^.a-z0-9]+)/i', '_', $target_file);
- if (!move_uploaded_file($_FILES["img"]["tmp_name"], $target_dir. $target_file)) {
- echo "Sorry, there was an error uploading your file. Please retry.";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement