2015-06-07 95 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

欢迎,没有问题:) –