데이터베이스에 이미지를 업로드하는 이미지 업로드 양식이 있습니다.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");
}
?>
확장 이름을 연결하지 않는 이유 – Ghost
그럴 수 있었습니까? 이 이미지를 사용하여 문제를 해결 했으므로 이미지 이름을 임의로 지정해야합니다. $ 경로 = $ _FILES [ '그림'] [ '이름']; $ ext = pathinfo ($ path, PATHINFO_EXTENSION); – salep
그냥 평소처럼 문자열 끝에 연결하십시오. – Ghost