2014-12-22 3 views
-1

데이터베이스에 이미지를 업로드하는 이미지 업로드 양식이 있습니다.PHP 이미지 업로드 : 이미지 이름 변경

아래 코드는 각 이미지에 대해 임의의 값을 생성합니다.

$name = "cover".rand(1,1000000).".jpg"; // generates random image name 

그러나 업로드 된 파일이 .jpg가 아닌 경우 어떻게해야합니까? PNG, GIF 또는 다른 이미지 파일 일 수 있습니다. 이미지 끝에 파일 확장자를 어떻게 동적으로 추가 할 수 있습니까?

강력한 랜덤 화 함수를 사용하고 싶습니다. 각 이미지에 고유 한 값을 부여하려면 어떤 종류의 함수를 사용해야합니까?

양식

<form action="upload.php" method="post" enctype="multipart/form-data" name="uploadform"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="350000"> 
    <input name="picture" type="file" id="picture" size="50"> 
    <input name="upload" type="submit" id="upload" value="Upload Picture!"> 
</form> 

upload.php로

<?php 
// if something was posted, start the process... 
if (isset($_POST['upload'])) { 

// define the posted file into variables 
    $name = $_FILES['picture']['name']; 
    $tmp_name = $_FILES['picture']['tmp_name']; 
    $type = $_FILES['picture']['type']; 
    $size = $_FILES['picture']['size']; 

// if your server has magic quotes turned off, add slashes manually 
    if (!get_magic_quotes_gpc()) { 
     $name = addslashes($name); 
    } 

// open up the file and extract the data/content from it 
    $extract = fopen($tmp_name, 'r'); 
    $content = fread($extract, $size); 
    $content = addslashes($content); 
    fclose($extract); 

// connect to the database 
    include "../cn/connect.php"; 

    $name = "cover".rand(1,1000000).".jpg"; // generates random image name 

// the query that will add this to the database 
    $stmt = $sqli->prepare("INSERT INTO image1(name, type, size) VALUES (?,?,?)"); 
    $stmt->bind_param("sss", $name, $_FILES['picture']['type'], $_FILES['picture']['size']); 
    $stmt->execute(); 
    $stmt->close(); 
    echo "Mission has been completed, sir!"; 

    if (!empty($_FILES)) { 
     $target = "upload/"; 
     $target = $target . basename($_FILES['picture']['name']); 
     $ok = 1; 
     $picture_size = $_FILES['picture']['size']; 
     $picture_type = $_FILES['picture']['type']; 
     //This is our size condition 
     if ($picture_size > 5000000) { 
      echo "Your file is too large.<br>"; 
      $ok = 0; 
     } 


     //This is our limit file type condition 
     if ($picture_type == "text/php") { 
      echo "No PHP files<br>"; 
      $ok = 0; 
     } 

     //Here we check that $ok was not set to 0 by an error 
     if ($ok == 0) { 
      Echo "Sorry your file was not uploaded"; 
     } //If everything is ok we try to upload it 
     else { 
      if (move_uploaded_file($_FILES['picture']['tmp_name'], $target)) { 
       echo "The file " . basename($_FILES['picture']['name']) . " has been uploaded <br/>"; 
      } else { 
       echo "Sorry, there was a problem uploading your file."; 
      } 
     } 
    } 

    echo "Successfully uploaded your picture!"; 
} else { 
    die("No uploaded file present"); 
} 

?> 
+2

확장 이름을 연결하지 않는 이유 – Ghost

+1

그럴 수 있었습니까? 이 이미지를 사용하여 문제를 해결 했으므로 이미지 이름을 임의로 지정해야합니다. $ 경로 = $ _FILES [ '그림'] [ '이름']; $ ext = pathinfo ($ path, PATHINFO_EXTENSION); – salep

+2

그냥 평소처럼 문자열 끝에 연결하십시오. – Ghost

답변

1

늘은

$type = $_FILES['picture']['type']; 

는 파일의 유형을 말해? 이 파일을 사용하여 올바른 (올바른) 올바른 파일 형식을 추가하십시오.

+1

형식은 제공하지만 확장명은 "있는 그대로"제공하지 않습니다. 확장 기능을 직접 가져와야하는데, "이미지/PNG"가 아닙니다. substr()과 쪼갤 수도 있지만이 질문을 할 때 생각할 수는 없다고 생각합니다. – salep

+1

'if ($ type == "image/png") {}'를 unqiue로 사용하면 png 파일에만 "image/png"가 표시됩니다. 다른 형식에도 동일합니다. –

+0

@Evan Carslake! 감사합니다. – salep