2012-09-30 4 views
0

uploadify를 사용하고 있습니다.하지만 업로드 된 파일의 이름을 바꾸는 PHP를 편집하는 방법이 너무 명확하지 않습니다.Uploadify - 파일 이름 바꾸기

기본적으로 사용자는 최대 4 개의 파일을 업로드 할 수 있으며 이름은 1-img-1, 1-img-2, 1-img-3, 1-img-4와 같이 지정해야합니다. id (POST를 통해 액세스 할 수 있음).

여기 uploadify PHP 스크립트는 다음과 같습니다

<?php 
/* 
UploadiFive 
Copyright (c) 2012 Reactive Apps, Ronnie Garcia 
*/ 

// Set the uplaod directory 
$uploadDir = '/img/listing_images/'; 

// Set the allowed file extensions 
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions 

$verifyToken = md5('unique_salt' . $_POST['timestamp']); 

if (!empty($_FILES) && $_POST['token'] == $verifyToken) { $i++; 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir; 
    $targetFile = $uploadDir . $_FILES['Filedata']['name']; 

    // Validate the filetype 
    $fileParts = pathinfo($_FILES['Filedata']['name']); 
    if (in_array(strtolower($fileParts['extension']), $fileTypes)) { 
    // Save the file 
    move_uploaded_file($tempFile, $targetFile); 
    echo 1; 

} else { 

    // The file type wasn't allowed 
    echo 'Invalid file type.'; 

} 
} 
?> 

누군가가 내가 업로드 된 파일의 이름을 바꿉니다 얼마나 나를 보여주기 위해 도움이 될 수 있다면 그냥 궁금?

+0

귀하의 스크립트는 특히'$ uploadDir'이 문서 루트 내에 있다면 서버의 완전한 손상을 허용 할 것입니다. 당신의 "보안"은 아무것도 아닙니다. 사용자 제공 파일 이름에 의한 필터링은 ** 충분하지 않습니다 **. –

답변

1

가 그 이름을 가진 고유 키와 안전하게 파일을 생성, 검증되지 않은 예 :

$fileParts = pathinfo($_FILES['Filedata']['name']); 
$unique_hash = hash_hmac("md5", file_get_contents($_FILES['Filedata']['name']), SALT); 
$targetFile = $uploadDir . $unique_hash . $fileParts['extension']; 
0

그냥 같이 fileParts을 $는 targetfile 변수 값을 변경하고 $의 선언을 & 할당을 이동 :

$fileParts = pathinfo($_FILES['Filedata']['name']); 
$targetFile = $uploadDir . '1-img-' . $i . $fileParts['extension']; 
+1

정말 대단합니다. 첫 번째 이미지를 업로드 한 후 중지되는 것으로 보이지만 파일의 이름을 변경하고 있습니까? –

+0

흠. 파일 이름이 모두 동일한 값 ($ i는 항상 1)으로 설정되도록 각 파일이 별도의 요청으로 게시되었다고 추측합니다. 각 파일은 이전 파일을 덮어 쓰고 하나의 파일 만 업로드되는 것처럼 보입니다. 클라이언트는 카운터를 보내야하고 코드는'$ counter = $ _FILES [ 'Filedata'] [ 'counter']처럼 보일 것이다. $ targetFile = $ uploadDir. '1-img-'. $ 카운터. $ fileParts [ 'extension']; ' –

+0

Marc B는 좋은 지적이지만 보안은 별개의 주제입니다. http://bit.ly/SeHa6Z에는 업로드 보안을 유지하는 데 필요한 몇 가지 필수 기능이 있습니다. –

0

여기에 원래 uploadify.php

<?php 
/* 
Uploadify 
Copyright (c) 2012 Reactive Apps, Ronnie Garcia 
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/ 

// Define a destination 
$targetFolder = '/uploads'; // Relative to the root 

$verifyToken = md5('unique_salt' . $_POST['timestamp']); 

if (!empty($_FILES) && $_POST['token'] == $verifyToken) { 
$tempFile = $_FILES['Filedata']['tmp_name']; 
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; 
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name']; 

// Validate the file type 
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions 
$fileParts = pathinfo($_FILES['Filedata']['name']); 

if (in_array($fileParts['extension'],$fileTypes)) { 
    move_uploaded_file($tempFile,$targetFile); 
    echo '1'; 
} else { 
    echo 'Invalid file type.'; 
} 
} 
?> 

에게있어 나는 이것을 단지 그렇게한다.

$fileParts = pathinfo($_FILES['Filedata']['name']); 
$targetFile = rtrim($targetPath,'/') . '/' .rand_string(20).'.'.$fileParts['extension']; 

여기서 rand_string()은 임의의 불법 발음을 생성하는 함수입니다.

이 파일은 파일을 업로드 할 때마다 다른 이름 (무작위)을 생성합니다. 희망이 도움이!