잘 작동하는이 간단한 스크립트가 있지만 현재 중복 된 이름으로 파일을 덮어 씁니다. 어떻게 이것을 피할 수 있습니까?덮어 쓰기를 방지하기 위해 업로드 된 파일의 이름을 변경하십시오.
<?php
// Configuration - Your Options
$allowed_filetypes = array('.mp3'); // These will be the types of file that will pass the validation.
$max_filesize = 1048576; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = './uploads/'; // The place the files will be uploaded to (currently a 'files' directory).
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
if(!in_array($ext,$allowed_filetypes))
header('Location: http://www.website.com/five/error');
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
header('Location: http://www.website.com/five/error');
if(!is_writable($upload_path))
header('Location: http://www.website.com/five/error');
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
// echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
header('Location: http://www.website.com/five/sent');
else
header('Location: http://www.website.com/five/error');
?>
당신은 정말'exit()를 추가해야합니다에 대한 좋은 확장 '리디렉션합니다. 그렇지 않으면 헤더를 보낸 후에도 여전히 업로드를 완료하려고 시도 할 수 있습니다. 이 경우에도'if' 문 각각에'{}'중괄호를 사용해야합니다. – Adam
이 답변은 매우 흥미 로울 수 있습니다 :
[ "PHP로 업로드 스크립트를 만들었습니다. 어떻게 파일을 덮어 쓰는 것을 피합니까?"] (http://stackoverflow.com/questions/846340/i-made-an- php-how-do-i-avoid-overwriting-file에서 스크립트 업로드/7267751 # 7267751) – SteakOverflow