Пример #1
0
 function save_photo($entity_id, $tmp_file, $entity = 'quiz', $filename = 'photo', $size = 100)
 {
     $result = array('result' => false, 'error' => '');
     if ($tmp_file['error'] !== 0) {
         $result['error'] = 'No file was upload';
         return $result;
     }
     $name_arr = explode('.', $tmp_file['name']);
     $file_extension = array_pop($name_arr);
     if (!in_array(strtolower($file_extension), array('jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp'))) {
         @unlink($tmp_file['tmp_name']);
         $result['error'] = 'File upload stopped by extension';
         return $result;
     }
     $old_file = he_quiz::get_photo($entity_id, $entity);
     if ($old_file) {
         $old_src = he_quiz::photo_dir() . $old_file;
         @unlink($old_src);
     }
     $new_file = "{$entity}_{$entity_id}.{$file_extension}";
     $new_src = he_quiz::photo_dir() . $new_file;
     $upload = new se_upload();
     $upload->new_upload($filename, 2 * 1024 * 1024, 'jpg');
     $upload->upload_photo($new_src, $size, $size);
     switch ($entity) {
         case 'result':
             $query = he_database::placeholder("UPDATE `se_he_quiz_result` SET `photo`='?'\r\n\t\t            WHERE `id`=?", $new_file, $entity_id);
             break;
         case 'question':
             $query = he_database::placeholder("UPDATE `se_he_quiz_question` SET `photo`='?'\r\n\t\t            WHERE `id`=?", $new_file, $entity_id);
             break;
         default:
             $query = he_database::placeholder("UPDATE `se_he_quiz` SET `photo`='?'\r\n                    WHERE `quiz_id`=?", $new_file, $entity_id);
             break;
     }
     he_database::query($query);
     $result['result'] = true;
     return $result;
 }