Skip to content

Instantly share code, notes, and snippets.

@ewillhite
Created February 28, 2013 20:06
Show Gist options
  • Save ewillhite/5059672 to your computer and use it in GitHub Desktop.
Save ewillhite/5059672 to your computer and use it in GitHub Desktop.
File Upload
$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