Xataface AJAX Upload Module 0.1
jQuery Ajax Upload Widget for Xataface
/Applications/XAMPP/xamppfiles/htdocs/nanofabrication/modules/ajax_upload/actions/ajax_upload_delete_temp_file.php
Go to the documentation of this file.
00001 <?php
00002 class actions_ajax_upload_delete_temp_file {
00003         
00004         const CODE_NO_SUCH_FILE = 404;
00005         const CODE_PERMISSION_DENIED = 400;
00006         const CODE_FIELD_EMPTY = 405;
00007         
00008 
00009         function handle($params){
00010         
00011                 $app = Dataface_Application::getInstance();
00012                 $query = $app->getQuery();
00013                 
00014                 if ( !@$_POST['--field'] ) throw new Exception("No field specified");
00015                 //if ( !@$_POST['--fileId'] ) throw new Exception("No file id specified");
00016         
00017                 $fieldName = $_POST['--field'];
00018                 $tableName = $_POST['-table'];
00019                 $fileId = null;
00020                 $recordId = null;
00021                 if ( @$_POST['--fileId'] ){
00022                         $fileId = $_POST['--fileId'];
00023                 } else if ( @$_POST['--recordId'] ){
00024                         $recordId = $_POST['--recordId'];
00025                 }
00026                 
00027                 
00028                 $table = Dataface_Table::loadTable($tableName);
00029                 $field =& $table->getField($fieldName);
00030                 try {
00031                         $savepath = $field['savepath'];
00032                         if ( $fileId ){
00033                                 
00034                                 $uploadsPath = $savepath.DIRECTORY_SEPARATOR.'uploads';
00035                                 if ( !is_dir($uploadsPath) ){
00036                                         throw new Exception("Uploads directory for field $field of table $table does not exist.");
00037                                 }
00038                                 
00039                                 $filePath = $uploadsPath.DIRECTORY_SEPARATOR.basename($fileId);
00040                                 if ( !file_exists($filePath) ){
00041                                         throw new Exception("The file does not exist.", self::CODE_NO_SUCH_FILE);
00042                                 }
00043                                 
00044                                 if ( !@unlink($filePath) ){
00045                                         throw new Exception("Failed to delete file.  There is likely a permissions issue preventing the file from being deleted.");
00046                                         
00047                                 }
00048                         } else if ( $recordId ){
00049                                 $record = df_get_record_by_id($recordId);
00050                                 if ( !$record ){
00051                                         throw new Exception("Could not find record with id $recordId.  File could not be deleted.");
00052                                 }
00053                                 if ( !$record->checkPermission('edit', array('field'=>$fieldName)) ){
00054                                         throw new Exception('Failed to delete file because you don\'t have edit permission on this field.', self::CODE_PERMISSION_DENIED);
00055                                         
00056                                 }
00057                                 
00058                                 $val = $record->val($fieldName);
00059                                 if ( !$val ){
00060                                         throw new Exception('There was no file to delete.', self::CODE_FIELD_EMPTY);
00061                                 }
00062                                 
00063                                 $filePath = $savepath.DIRECTORY_SEPARATOR.basename($val);
00064                                 if ( file_exists($filePath) ){
00065                                         //throw new Exception("The file does not exist.", self::CODE_NO_SUCH_FILE);
00066                                         if ( !@unlink($filePath) ){
00067                                                 throw new Exception("Failed to delete file.  There is likely a file-system permissions issue preventing the file from being deleted.");
00068                                                 
00069                                         }
00070                                 }
00071                                 
00072                                 
00073                                 $record->setValue($fieldName, null);
00074                                 $record->save();
00075                                 
00076                         
00077                         } else {
00078                                 throw new Exception("Must supply either --recordId or --fileId parameter.");
00079                         }
00080                         
00081                         $this->out(array(
00082                                 'code'=>200,
00083                                 'message' => 'Successfully deleted file.'
00084                         ));
00085                 } catch (Exception $ex){
00086                 
00087                         if ( $ex->getCode() ){
00088                                 $this->out(array(
00089                                         'code'=>$ex->getCode(),
00090                                         'message' => $ex->getMessage()
00091                                 ));
00092                         } else {
00093                                 error_log('[ajax_upload] '.$ex->getMessage().' on line '.__LINE__.' or file '.__FILE__);
00094                                 
00095                                 $this->out(array(
00096                                         'code'=>500,
00097                                         'message' => 'Failed to delete file due to a server error.'
00098                                 ));
00099                         }
00100                 }
00101         }
00102         
00103         function out($params){
00104                 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
00105                 echo json_encode($params);
00106         }
00107 }
 All Data Structures Files Functions Variables