function omanchem_blog_api_file_upload_callback() { drupal_add_http_header('Content-Type', 'application/json; charset=utf-8'); if ($_SERVER['REQUEST_METHOD'] !== 'POST') { drupal_add_http_header('Status', '405 Method Not Allowed'); print drupal_json_encode(array('success' => FALSE, 'message' => 'Only POST requests are allowed.')); drupal_exit(); } $expected_api_key = 'R7!xQ2@kL9#vP4$mN8^tW1&bH6*zC3!yF5@uJ0'; $api_key = isset($_POST['api_key']) ? trim($_POST['api_key']) : ''; if ($api_key !== $expected_api_key) { drupal_add_http_header('Status', '403 Forbidden'); print drupal_json_encode(array('success' => FALSE, 'message' => 'Invalid API key.')); drupal_exit(); } if (empty($_FILES['file'])) { drupal_add_http_header('Status', '400 Bad Request'); print drupal_json_encode(array('success' => FALSE, 'message' => 'No file uploaded.')); drupal_exit(); } $directory = 'public://blog-images'; file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); // Check if directory is actually writable $real_dir = drupal_realpath($directory); if (!$real_dir || !is_writable($real_dir)) { drupal_add_http_header('Status', '500 Internal Server Error'); print drupal_json_encode(array( 'success' => FALSE, 'message' => 'Upload directory is not writable.', 'directory' => $real_dir, )); drupal_exit(); } // Try WITHOUT file_validate_is_image first — this is the most common culprit $validators = array( 'file_validate_extensions' => array('png jpg jpeg gif webp'), 'file_validate_size' => array(10 * 1024 * 1024), ); $file = file_save_upload('file', $validators, $directory, FILE_EXISTS_RENAME); if (!$file) { // Capture Drupal's internal error messages $all_messages = drupal_get_messages(); // Also try manual save as fallback $tmp_path = $_FILES['file']['tmp_name']; $original_name = $_FILES['file']['name']; $safe_name = preg_replace('/[^a-zA-Z0-9_\-\.]/', '_', $original_name); $destination = $real_dir . '/' . $safe_name; // Avoid overwrite $counter = 0; while (file_exists($destination)) { $counter++; $info = pathinfo($safe_name); $destination = $real_dir . '/' . $info['filename'] . '_' . $counter . '.' . $info['extension']; } if (!move_uploaded_file($tmp_path, $destination)) { drupal_add_http_header('Status', '400 Bad Request'); print drupal_json_encode(array( 'success' => FALSE, 'message' => 'File upload failed. file_save_upload and manual fallback both failed.', 'drupal_messages' => $all_messages, 'debug' => array( 'directory_real' => $real_dir, 'directory_writable' => is_writable($real_dir), 'tmp_exists' => file_exists($tmp_path), 'php_upload_error' => $_FILES['file']['error'], ), )); drupal_exit(); } // Manual fallback succeeded — register with Drupal's file system $uri = $directory . '/' . basename($destination); $file = new stdClass(); $file->uid = 1; $file->status = FILE_STATUS_PERMANENT; $file->filename = basename($destination); $file->uri = $uri; $file->filemime = file_get_mimetype($destination); $file->filesize = filesize($destination); file_save($file); } $file->status = FILE_STATUS_PERMANENT; file_save($file); $image_info = image_get_info($file->uri); print drupal_json_encode(array( 'success' => TRUE, 'message' => 'File uploaded successfully.', 'fid' => $file->fid, 'filename' => $file->filename, 'uri' => $file->uri, 'url' => file_create_url($file->uri), 'filesize' => $file->filesize, 'width' => !empty($image_info['width']) ? $image_info['width'] : NULL, 'height' => !empty($image_info['height']) ? $image_info['height'] : NULL, )); drupal_exit(); } Blog | Page 22 | Oman CHEMICAL

Your One Stop for All Chemicals
Industrial • Marine • Cleaning
+968 9948 9269 / +968 9396 0629
WhatsApp / Call Us Anytime
omanchem.com@gmail.com
Drop Us a Line
Muscat, Sultanate of Oman
Locate Us

Blog

Product Short Description: Purified Methanol treated with SILVER NITRATE to make chlorie FREE. Usefull for cleaning operation where chloride free surface lavel required. Product Description:
read more
on 15 Dec 2022 1:27 PM
  • METHANOL TECH GRADE 25 LTR Sohar Muscat Barka Ruwi Madayn
Product Short Description: Pasivation Grade Improve the corrosion resistance of parts made from austenitic, ferritic and martensitic corrosion-resistant steels of the 200, 300 and 400 series and precipitation hardened corrosion-resistant steels. Product Description:
read more
on 15 Dec 2022 1:21 PM
  • NITRIC ACID 68% Sohar Muscat Barka Ruwi Madayn
Product Short Description: Pure specified product for specialised TANK WASH Product Description: p-Xylene is a colorless, flammable liquid that is insoluble in water with the chemical formula C8H10 or C6H4(CH3)2 , Suitable for TANK WASH
read more
on 15 Dec 2022 1:02 PM
  • P XYLENE TECH GRADE 210 LTR Sohar Muscat Barka Ruwi Madayn
Product Short Description: RXSOL PASSIVATION is highly conc and acidic product , It does not give Black or Yellow stain marks after washing with water. It simultaneously removes annealing colours, ferritic contamination & weld burns. Product Description: RXSOL PASSIVATION is highly conc and acidic product , It does not give Black or Yellow stain marks after washing with water. It simultaneously removes annealing colours, ferritic contamination & weld burns.
read more
on 15 Dec 2022 1:29 PM
  • PASSIVATION LIQUID RX Sohar Muscat Barka Ruwi Madayn
Product Short Description: Used as Cleaning and Degreasing agent , where mechanical action is limited Product Description: RXSOL BG is a liquid blend of concentrated emulsifier with detergent for cleaning and degreasing , where mechanical action is limited. It can also be used for local cleaning and degreasing of engine rooms and on deck.
read more
on 15 Dec 2022 1:07 PM
  • RESIN CLEAN Sohar Muscat Barka Ruwi Madayn
Product Short Description: Shoes Covers (1 box=100pcs) Product Description: Disposable Shoe Cover Box. RXSOL brand shoes cover coated with STEARIC BASE wax, which gives quite more chemical resistance property then ordinary shoe cover. In local market shoe cover means simple plastic shoes protection materials, this can be useful for simple official and chemical free surface area. Each packet contains 100 PCS.
read more
on 15 Dec 2022 1:25 PM
  • SHOES COVERS (1 BOX=100PCS) Sohar Muscat Barka Ruwi Madayn