2015-06-07 93 views
1

這是我的代碼。我想在將文件存儲到uploads文件夾之前更改文件名,但似乎無法弄清楚。謝謝。儲存前更改圖像名稱

// Check for errors 
if($_FILES['file_upload']['error'] > 0){ 
die('An error ocurred when uploading.'); 
} 

if(!getimagesize($_FILES['file_upload']['tmp_name'])){ 
die('Please ensure you are uploading an image.'); 
} 

// Check filetype 
if($_FILES['file_upload']['type'] != 'image/png'){ 
die('Unsupported filetype uploaded.'); 
} 

// Check filesize 
if($_FILES['file_upload']['size'] > 700000){ 
die('File uploaded exceeds maximum upload size.'); 
} 


// Check if the file exists 
if(file_exists('../uploads/profilepics/' . $_FILES['file_upload']['name'])){ 
die('File with that name already exists.'); 
} 

// Upload file 
if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], '../uploads/profilepics/' . $_FILES['file_upload']['name'])){ 
die('Error uploading file - check destination is writeable.'); 
} 

// File uploaded succesfully - upload to server and to DB 
die('File uploaded successfully.'); 
+1

手動 –

回答

2

通過生成從文件名上傳了一張新的獨特的字符串,並搶擴展創建一個新的文件名,並簡單地將它傳遞給move_uploaded_file

$extension = pathinfo($_FILES['file_upload']['name'], PATHINFO_EXTENSION); 
$nFileName = md5(time()).'.'.$extension; 


// Upload file 
if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], '../uploads/profilepics/' . $nFileName)){ 
    die('Error uploading file - check destination is writeable.'); 
} 
+0

謝謝你,它的工作查找move_uploded_file - 我需要7分鐘才能將答案打勾爲正確的:) – CharlotteOswald

+0

歡迎,沒有問題:) –