2015-10-07 136 views
0

美好的一天,我正在開發一個Web應用程序,用戶可以上傳照片。PHP - 上傳照片時更改文件名的目的地

我已經有一個PHP代碼,我可以將照片上傳到服務器目錄中的一個文件夾。但是,我在代碼中遇到的主要問題是,它會保留照片的上傳文件名稱,然後再上傳。我的問題是,當有兩個不同的照片具有相同的文件名,我上傳時會發生衝突。

這裏是我的代碼:

<?php 

error_reporting(-1); 
ini_set('display_errors', 'On'); 


require_once '_common.php'; 
$config = require_once '_config.php'; 
$fileName = ''; 
$mimeType = ''; 
$fileSize = 0; 

if (empty($_FILES)) { 
    _error('No file received'); 
} 

$pathArray = array(); 

foreach ($_FILES as $fileName => $fileData) { 

    if (!isset($fileData['error']) || 
     is_array($fileData['error'])) { 
     die(json_encode(array(
      'success' => false, 
      'status' => "Invalid Parameters.", 
      'files' => $_FILES 
     ))); 
    } 

    switch ($fileData['error']) { 
     case UPLOAD_ERR_OK: 
      break; 
     case UPLOAD_ERR_NO_FILE: 

      die(json_encode(array(
       'success' => false, 
       'status' => "No file sent.", 
       'files' => $_FILES))); 

     case UPLOAD_ERR_INI_SIZE: 
     case UPLOAD_ERR_FORM_SIZE: 
      die(json_encode(array(
       'success' => false, 
       'status' => "Exceeded filesize limit.", 
       'files' => $_FILES))); 

     default: 
      die(json_encode(array(
       'success' => false, 
       'status' => "Unknown errors.", 
       'files' => $_FILES))); 
    } 

    // You should also check filesize here. 
    if ($fileData['size'] > 1000000) { 
      die(json_encode(array(
       'success' => false, 
       'status' => "Exceeded File Size Limit.", 
       'files' => $_FILES))); 
    } 

    $finfo = new finfo(FILEINFO_MIME_TYPE); 
    if (false === $ext = array_search(
     $finfo->file($fileData['tmp_name']), 
     array(
      'jpg' => 'image/jpeg', 
      'png' => 'image/png', 
      'gif' => 'image/gif',), 
     true)) { 
     die(json_encode(array(
      'success' => false, 
      'status' => "Invalid file format.", 
      'files' => $_FILES))); 
    } 

    if (!move_uploaded_file(
     $fileData['tmp_name'], 
     sprintf('./gallery/%s', 
     basename($fileData['name'])))) { 

     die(json_encode(array(
      'success' => false, 
      'status' => "Failed to move uploaded file.", 
      'files' => $_FILES))); 
    } 

    $uploaddir = '/gallery/'; 
    $name = $fileData["tmp_name"]; 

    $fullPath = sprintf('gallery/%s.%s', 
     basename($fileData['tmp_name']), 
     $ext 

    array_push($pathArray, $fullPath); 

} 

print_r(json_encode(array(
    'success' => true, 
    'status' => "File is uploaded successfully.", 
    'filePath' => "$fullPath" 
))); 
?> 

我失去了我的PHP觸摸,但是,這是我已經試過:

if (!move_uploaded_file(
    $fileData['tmp_name'], 
    sprintf('gallery/%s.%s', 
    sha1_file($_FILES['fileToUpload']['tmp_name'], 
    $ext) 

    )) { 

    die(json_encode(array(
     'success' => false, 
     'status' => "Failed to move uploaded file.", 
     'files' => $_FILES))); 
} 

而應該是經歷,我改變$fullPath,以適應我想要的新目的地:

$fullPath = sprintf('gallery/%s.%s', 
    sha1_file($_FILES['fileToUpload']['tmp_name'], 
    $ext); 

但是,這些似乎都沒有工作。

我的目標是給上傳的文件一個或多或少的獨特的文件名,但是,我似乎無法使sha1_file工作。

如果任何人都可以指導我如何使sha1_file工作或以這種方式更改目標文件名的另一種方式,我將得到一個唯一的文件名(例如使用日期和時間),它將是最歡迎。

感謝您的幫助。

回答

1

你可以使用microtime中()或uniqid()函數與文件名來連接:

if (!move_uploaded_file(
    $fileData['tmp_name'], 
    sprintf('gallery/%s.%s', 
    uniqid() . '_' . $_FILES['fileToUpload']['tmp_name'], 
    $ext) 

    )) { 

    die(json_encode(array(
     'success' => false, 
     'status' => "Failed to move uploaded file.", 
     'files' => $_FILES))); 
} 

有關uniqid更多信息,請參見uniqid文檔。

+0

謝謝!我用'uniqid()。 '_'。 microtime()'代替。當然我改變了$ fullPath變量以反映相同。 – Razgriz