현재 내 코드는 사용자가 제출 한 이미지의 이름으로 이미지 파일을 업로드합니다. 이제 분명히 문제를 덮어 쓰지 않기 위해 이것을 바꾸고 싶습니다. 자, 다른 제안을 한 번 보았습니다 만, 코드로 작업 할 수없는 것 같습니다 (필자는 몇 달 동안 PHP 만 배웠습니다). 그래서, 너희들이 나를 도울 수 있니? 여기업로드 후 파일 이름을 변경하는 방법 PHP
<?php
$target_dir = "avatars/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
//echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
//echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
내 생각 엔 내가 move_uploaded_file
코드를 변경해야 할 것입니다 업로드 코드입니까? 어떤 도움을 주셔서 감사합니다!
그것은 이미 스크립트처럼 보인다 감지합니다. 이것에
같은 이름이 주어지면 –
예, 원래 이미지의 이름을 가져 왔지만 이미지를 서로 덮어 쓰지 않도록 업로드 한 후에 이미지 이름을 변경하고 싶습니다. – MyNameIsNotSoCool
'$ target_file'은 이름과 위치가 잘 맞습니다. '싼'방법은 이름에 타임 스탬프를 추가하는 것입니다 (바쁜 사이트에서는 사용하지 마십시오) –