2016-12-14 131 views
-2

如何以及在哪裏存儲用戶照片?我在這裏閱讀了一些非常好的答案,但他們沒有解釋我必須寫什麼代碼以及在哪裏發送照片。如何保存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等嘰嘰喳喳不要將文件保存在數據庫中,因爲它們佔用了大量的服務器空間,因此將它們保存在文件夾中。有人可以幫助我如何一步一步做到這一點?

+0

以及所有在PHP文檔記錄。 http://php.net/manual/en/features.file-upload.php – ceejayoz

+0

@ceejayoz它不顯示在哪裏存儲文件 – 6623ms

+0

它演示了'move_uploaded_file'函數的用法。 *存儲這些文件的位置取決於您。把它們放在你需要它們的地方。 – ceejayoz

回答

0

假設你的代碼沒有錯誤,讓我解釋一下這裏發生了什麼。 HTML部分很清晰,並且那個東西用來對瀏覽器說,我們會上傳文件。

所以,躍居PHP代碼:

<?php 

$target_dir = "uploads/"; 
//This is the directry where the images will be uploaded, "server_path/uploads" 

//Now since we have the target directory, let's define the actual path. 
//Actual Path = Target Dir + File Name + File Extension 

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 

//By now, we have just madefined locations, (in the form of Variables) 

//Now, lets do few checks, and make a success flag to keep a track 
$uploadOk = 1; 



// Check if file already exists 
if (file_exists($target_file)) { 
    //If the file already exists 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
    //Echo and change the flag to False 
} 


// Check if image file is a actual image or fake image. 
//If file is image, then only image size will be returned 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
if(!($check)) { 
    //That is, the Check is false, the file is NOT AN IMAGE 
    echo "File is not an image."; 
    $uploadOk = 0; 
    //Echo, and change the Flag to false. 
} 


//Now check, if the flag is true, that means upload is OK, and NO ERRORS. 
//Then upload the file. 
if ($uploadOk) { 
    //If upload is OK, then go ahead 
    move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file); 
    // Move the uploaded file to the desired folder 
} 
?> 

這只是上傳的文件,額外的檢查可以實現,等確認文件大小等More Info on this W3 Page

正在做什麼? 首先,只要您在該選擇文件按鈕上選擇文件,該文件就會被引用到只有計算機知道的fakepath(數據抽象)。然後在提交文件時,圖像數據從瀏覽器(fakepath)發送到服務器,服務器存儲在temp_dir(或等同文件夾,用於臨時存儲),然後開始執行腳本。並在腳本中,因爲我們已經提到了存儲$target_dir的位置和文件名,因此它是,然後move_uploaded_file,所以服務器知道,是時候從fakepath移動到一個實際路徑......然後你得到的文件在該目錄。如果move_uploaded_file尚未出現,那麼臨時文件在腳本執行後或之後的一段時間內將被取消鏈接(取決於您如何配置服務器,文件號碼爲php.ini)。

注:上傳目錄(uploads/在這種情況下),需要將已經創建,服務器將只添加的文件,如果floder已經不存在,它會執行,(沒有任何錯誤),但該文件不會顯示,(因爲沒有文件夾來保存它)。

現在,您要將其添加到數據庫中,可以保留此鏈接並將其添加到數據庫(實際上,數據庫爲UPDATE),但這是不同的事情。 (我剛剛說過,因爲你在問題中提到過)。

注意:您可能需要更改代碼以根據需要使用它。

希望這有助於:)

+0

'C:\ wamp \ www \ xx \ uploads'這是一個有效的位置嗎? – 6623ms

+0

我收到了這個錯誤'未定義的索引:圖像在C:\ wamp \ www \ xx \ upload.php在第39行' – 6623ms

+0

'move_uploaded_file($ _ FILES ['image'] ['tmp_name'],$ target_file); ' – 6623ms

0
$image14 = $_FILES['img']['name'];      //img your form input name="img" 
$image15 = $_FILES['img']['tmp_name']; 
$ext  = pathinfo($image14, PATHINFO_EXTENSION); 
$path2 = "images/" . time() . $image14;    // "image/" specify your foldername 
move_uploaded_file($image15, $path2);     // save $path2 into your database