Projet

Général

Profil

Révision 4444412d

Ajouté par Julien Enselme il y a environ 10 ans

Update drupal 7.27 -> 7.28

Voir les différences:

drupal7/includes/file.inc
621 621
    module_invoke_all('entity_update', $file, 'file');
622 622
  }
623 623

  
624
  // Clear internal properties.
624 625
  unset($file->original);
626
  // Clear the static loading cache.
627
  entity_get_controller('file')->resetCache(array($file->fid));
628

  
625 629
  return $file;
626 630
}
627 631

  
......
1293 1297
  if (file_unmanaged_delete($file->uri)) {
1294 1298
    db_delete('file_managed')->condition('fid', $file->fid)->execute();
1295 1299
    db_delete('file_usage')->condition('fid', $file->fid)->execute();
1300
    entity_get_controller('file')->resetCache();
1296 1301
    return TRUE;
1297 1302
  }
1298 1303
  return FALSE;
......
1402 1407
 * Temporary files are periodically cleaned. To make the file a permanent file,
1403 1408
 * assign the status and use file_save() to save the changes.
1404 1409
 *
1405
 * @param $source
1406
 *   A string specifying the filepath or URI of the uploaded file to save.
1410
 * @param $form_field_name
1411
 *   A string that is the associative array key of the upload form element in
1412
 *   the form array.
1407 1413
 * @param $validators
1408 1414
 *   An optional, associative array of callback functions used to validate the
1409 1415
 *   file. See file_validate() for a full discussion of the array format.
......
1414 1420
 *   (Beware: this is not safe and should only be allowed for trusted users, if
1415 1421
 *   at all).
1416 1422
 * @param $destination
1417
 *   A string containing the URI $source should be copied to.
1418
 *   This must be a stream wrapper URI. If this value is omitted, Drupal's
1419
 *   temporary files scheme will be used ("temporary://").
1423
 *   A string containing the URI that the file should be copied to. This must
1424
 *   be a stream wrapper URI. If this value is omitted, Drupal's temporary
1425
 *   files scheme will be used ("temporary://").
1420 1426
 * @param $replace
1421 1427
 *   Replace behavior when the destination file already exists:
1422 1428
 *   - FILE_EXISTS_REPLACE: Replace the existing file.
......
1434 1440
 *   - source: Path to the file before it is moved.
1435 1441
 *   - destination: Path to the file after it is moved (same as 'uri').
1436 1442
 */
1437
function file_save_upload($source, $validators = array(), $destination = FALSE, $replace = FILE_EXISTS_RENAME) {
1443
function file_save_upload($form_field_name, $validators = array(), $destination = FALSE, $replace = FILE_EXISTS_RENAME) {
1438 1444
  global $user;
1439 1445
  static $upload_cache;
1440 1446

  
1441 1447
  // Return cached objects without processing since the file will have
1442 1448
  // already been processed and the paths in _FILES will be invalid.
1443
  if (isset($upload_cache[$source])) {
1444
    return $upload_cache[$source];
1449
  if (isset($upload_cache[$form_field_name])) {
1450
    return $upload_cache[$form_field_name];
1445 1451
  }
1446 1452

  
1447 1453
  // Make sure there's an upload to process.
1448
  if (empty($_FILES['files']['name'][$source])) {
1454
  if (empty($_FILES['files']['name'][$form_field_name])) {
1449 1455
    return NULL;
1450 1456
  }
1451 1457

  
1452 1458
  // Check for file upload errors and return FALSE if a lower level system
1453 1459
  // error occurred. For a complete list of errors:
1454 1460
  // See http://php.net/manual/features.file-upload.errors.php.
1455
  switch ($_FILES['files']['error'][$source]) {
1461
  switch ($_FILES['files']['error'][$form_field_name]) {
1456 1462
    case UPLOAD_ERR_INI_SIZE:
1457 1463
    case UPLOAD_ERR_FORM_SIZE:
1458
      drupal_set_message(t('The file %file could not be saved, because it exceeds %maxsize, the maximum allowed size for uploads.', array('%file' => $_FILES['files']['name'][$source], '%maxsize' => format_size(file_upload_max_size()))), 'error');
1464
      drupal_set_message(t('The file %file could not be saved, because it exceeds %maxsize, the maximum allowed size for uploads.', array('%file' => $_FILES['files']['name'][$form_field_name], '%maxsize' => format_size(file_upload_max_size()))), 'error');
1459 1465
      return FALSE;
1460 1466

  
1461 1467
    case UPLOAD_ERR_PARTIAL:
1462 1468
    case UPLOAD_ERR_NO_FILE:
1463
      drupal_set_message(t('The file %file could not be saved, because the upload did not complete.', array('%file' => $_FILES['files']['name'][$source])), 'error');
1469
      drupal_set_message(t('The file %file could not be saved, because the upload did not complete.', array('%file' => $_FILES['files']['name'][$form_field_name])), 'error');
1464 1470
      return FALSE;
1465 1471

  
1466 1472
    case UPLOAD_ERR_OK:
1467 1473
      // Final check that this is a valid upload, if it isn't, use the
1468 1474
      // default error handler.
1469
      if (is_uploaded_file($_FILES['files']['tmp_name'][$source])) {
1475
      if (is_uploaded_file($_FILES['files']['tmp_name'][$form_field_name])) {
1470 1476
        break;
1471 1477
      }
1472 1478

  
1473 1479
    // Unknown error
1474 1480
    default:
1475
      drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $_FILES['files']['name'][$source])), 'error');
1481
      drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $_FILES['files']['name'][$form_field_name])), 'error');
1476 1482
      return FALSE;
1477 1483
  }
1478 1484

  
......
1480 1486
  $file = new stdClass();
1481 1487
  $file->uid      = $user->uid;
1482 1488
  $file->status   = 0;
1483
  $file->filename = trim(drupal_basename($_FILES['files']['name'][$source]), '.');
1484
  $file->uri      = $_FILES['files']['tmp_name'][$source];
1489
  $file->filename = trim(drupal_basename($_FILES['files']['name'][$form_field_name]), '.');
1490
  $file->uri      = $_FILES['files']['tmp_name'][$form_field_name];
1485 1491
  $file->filemime = file_get_mimetype($file->filename);
1486
  $file->filesize = $_FILES['files']['size'][$source];
1492
  $file->filesize = $_FILES['files']['size'][$form_field_name];
1487 1493

  
1488 1494
  $extensions = '';
1489 1495
  if (isset($validators['file_validate_extensions'])) {
......
1540 1546
    return FALSE;
1541 1547
  }
1542 1548

  
1543
  $file->source = $source;
1549
  $file->source = $form_field_name;
1544 1550
  // A URI may already have a trailing slash or look like "public://".
1545 1551
  if (substr($destination, -1) != '/') {
1546 1552
    $destination .= '/';
......
1549 1555
  // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and
1550 1556
  // there's an existing file so we need to bail.
1551 1557
  if ($file->destination === FALSE) {
1552
    drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $source, '%directory' => $destination)), 'error');
1558
    drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $form_field_name, '%directory' => $destination)), 'error');
1553 1559
    return FALSE;
1554 1560
  }
1555 1561

  
......
1568 1574
    else {
1569 1575
      $message .= ' ' . array_pop($errors);
1570 1576
    }
1571
    form_set_error($source, $message);
1577
    form_set_error($form_field_name, $message);
1572 1578
    return FALSE;
1573 1579
  }
1574 1580

  
......
1576 1582
  // directory. This overcomes open_basedir restrictions for future file
1577 1583
  // operations.
1578 1584
  $file->uri = $file->destination;
1579
  if (!drupal_move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->uri)) {
1580
    form_set_error($source, t('File upload error. Could not move uploaded file.'));
1585
  if (!drupal_move_uploaded_file($_FILES['files']['tmp_name'][$form_field_name], $file->uri)) {
1586
    form_set_error($form_field_name, t('File upload error. Could not move uploaded file.'));
1581 1587
    watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->uri));
1582 1588
    return FALSE;
1583 1589
  }
......
1597 1603
  // If we made it this far it's safe to record this file in the database.
1598 1604
  if ($file = file_save($file)) {
1599 1605
    // Add file to the cache.
1600
    $upload_cache[$source] = $file;
1606
    $upload_cache[$form_field_name] = $file;
1601 1607
    return $file;
1602 1608
  }
1603 1609
  return FALSE;

Formats disponibles : Unified diff