Created
February 28, 2013 20:06
-
-
Save ewillhite/5059672 to your computer and use it in GitHub Desktop.
File Upload
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
$file = file_save_upload('bg_upload'); | |
if ($form_state['values']['bgon'] == 1) { | |
if ($file) { | |
$parts = pathinfo($file->filename); | |
$destination = 'public://' . $parts['basename']; | |
$file->status = FILE_STATUS_PERMANENT; | |
if(file_copy($file, $destination, FILE_EXISTS_REPLACE)) { | |
$_POST['bg_path'] = $form_state['values']['bg_path'] = $destination; | |
// If new file has a different name than the old one, delete the old | |
if ($destination != $previous) { | |
drupal_unlink($previous); | |
} | |
} | |
} else { | |
// Avoid error when the form is submitted without specifying a new image | |
$_POST['bg_path'] = $form_state['values']['bg_path'] = $previous; | |
} | |
} | |
else { | |
$_POST['bg_path'] = $form_state['values']['bg_path'] = ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment