2016-05-12 27 views
0

我在另一個我的上傳文件中使用下面的代碼(它完美地工作)去掉和替換圖像文件名稱中的標點符號和空格中的標點符號和空格上傳過程:str_replace與數組無法在我的批量上傳表單上工作

$filename = basename($_FILES['file']['name']); 
$ext = substr($filename, strrpos($filename, '.') + 1); 
$file = str_replace(
// punctuation marks, symbols, and spaces to search for in the file's name 
array("&","*", " ", "'", " ", "__", "__", "____", "_____", "*", ":", "@", "!", "$", "(", ")", "?", "[", "]", "<", ">", "`", "=", "{", "}", ";", ",", "/", "~", "#", "%", "^", "+"), 
// replacements for punctuation marks, symbols, and spaces found in the file’s name 
array("and", "_", "_", "", "_", "_", "_", "_", "_", "-", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"), 
$_FILES['file']['name']); 

我已經在我的bulk_upload.php文件中嘗試了上面的代碼,它不起作用。上傳過程中圖像文件名稱中找到的標點符號,符號和空格不會被剝離和替換。

我也曾嘗試與第一行代碼上面的代碼更改爲:

$filename = basename($_FILES['Filedata']['name']);

所以代碼就成爲繼:

$filename = basename($_FILES['Filedata']['name']); 
$ext = substr($filename, strrpos($filename, '.') + 1); 
$file = str_replace(
// punctuation marks, symbols, and spaces to search for in the file's name 
array("&","*", " ", "'", " ", "__", "__", "____", "_____", "*", ":", "@", "!", "$", "(", ")", "?", "[", "]", "<", ">", "`", "=", "{", "}", ";", ",", "/", "~", "#", "%", "^", "+"), 
// replacements for punctuation marks, symbols, and spaces found in the file’s name 
array("and", "_", "_", "", "_", "_", "_", "_", "_", "-", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"), 
$_FILES['file']['name']); 

但上面的代碼會不工作。

獲得上述代碼工作的任何幫助將不勝感激。

另外,如果可能的話,請你告訴我你提供的任何代碼應該放在我的bulk_upload.php文件中。

在此先感謝。

這裏是包含我bulk_upload.php文件中的全部代碼:

<?php 
include '../../config.php'; 

$securekey = md5($setting['license_key'].$setting['cron_last_run']); 

if ($_GET['key'] == $securekey) { 

    if ($_FILES["Filedata"]["error"] > 0) { 
     echo "Return Code: " . $_FILES["Filedata"]["error"] . "<br />"; 
    } 
    else { 
     $directory = "../../files/$_GET[folder]/"; 

     $filename = basename($_FILES['Filedata']['name']); 
     $ext = substr($filename, strrpos($filename, '.') + 1); 
     $file = str_replace(
     // punctuation marks, symbols, and spaces to search for in the file's name 
      array("&","*", " ", "'", " ", "__", "__", "____", "_____", "*", ":", "@", "!", "$", "(", ")", "?", "[", "]", "<", ">", "`", "=", "{", "}", ";", ",", "/", "~", "#", "%", "^", "+"), 
     // replacements for punctuation marks, symbols, and spaces found in the file’s name 
      array("and", "_", "_", "", "_", "_", "_", "_", "_", "-", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"), 
      $_FILES['file']['name']); 

     $valid_extensions = array('png', 'jpg', 'jpeg', 'gif', 'PNG', 'JPG', 'JPEG', 'GIF'); 
     if (!in_array($ext, $valid_extensions)) { 
      echo htmlspecialchars($filename)." is not an image file"; 
     } 
     elseif (file_exists($directory . $_FILES["Filedata"]["name"])) { 
      echo htmlspecialchars($_FILES["Filedata"]["name"]) . " already exists. "; 
     } 
     else { 
      move_uploaded_file($_FILES["Filedata"]["tmp_name"], $directory . $_FILES["Filedata"]["name"]); 
     } 
    } 

} 
else { 
    echo 'Invalid security clearance. Please Try Again.'; 
} 
?> 

回答

0

在這一行:

move_uploaded_file($_FILES["Filedata"]["tmp_name"], $directory . $_FILES["Filedata"]["name"]); 

你應該使用$file,不$_FILES["Filedata"]["name"]。即:

move_uploaded_file($_FILES["Filedata"]["tmp_name"], $directory . $file); 
+0

Eduardo,謝謝。您解決了在上傳過程中圖像文件的名稱未被剝離和替換的問題。再一次,謝謝你。 – 1Ton

0

在str_replace函數調用你還在使用$_FILES['file']['name'],它應該是$_FILES['Filedata']['name']

+0

我改變了str_replace函數調用'$ _FILES [「Filedata上」] [「名」]'如你所說,但標點,符號和空格仍然沒有被剝離,並在更換文件的名稱在上傳過程中。還有其他建議嗎? – 1Ton

相關問題