如何以及在哪裏存儲用戶照片?我在這裏閱讀了一些非常好的答案,但他們沒有解釋我必須寫什麼代碼以及在哪裏發送照片。如何保存PHP圖像?
到目前爲止,這是我的代碼:
profile.php:
<form id="form2" action="upload.php" method="post" enctype="multipart/form-data">
<p id="p1">Change profile picture:</p> <br />
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<br><input id="sub1" type="submit" value="Change profile picture" name="submit"><br />
</form>
upload.php的:
<?php
$target_dir = "uploads/";
$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 if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
$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;
}
$save_path="userimg"; // Folder where you wanna move the file.
$myname = strtolower($_FILES['image']['tmp_name']); //You are renaming the file here
move_uploaded_file($_FILES['image']['tmp_name'], $save_path.$myname); // Move the uploaded file to the desired folder
}
?>
我讀了這裏的大網站,如Facebook等嘰嘰喳喳不要將文件保存在數據庫中,因爲它們佔用了大量的服務器空間,因此將它們保存在文件夾中。有人可以幫助我如何一步一步做到這一點?
以及所有在PHP文檔記錄。 http://php.net/manual/en/features.file-upload.php – ceejayoz
@ceejayoz它不顯示在哪裏存儲文件 – 6623ms
它演示了'move_uploaded_file'函數的用法。 *存儲這些文件的位置取決於您。把它們放在你需要它們的地方。 – ceejayoz