2017-08-08 172 views
0

我正在用PHP,HTML和CSS創建一個社交網絡項目。用戶具有的功能之一是上傳個人資料圖片。我只是看了幾個教程,我一直在關注他們,但他們很混亂,我完全迷失了。這是我最後的希望。我在我的upload.php文件中有一些代碼,當我去那個頁面它說File saved這意味着圖像已保存,但我沒有看到圖像的任何地方。有人可以幫我將圖像保存到我的文件夾中,當用戶單擊更改配置文件圖片時,圖片會被更改並保存嗎?請幫忙 。謝謝 。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> 

<!-- Trigger the Modal --> 
<img id="myImg" src="default.png" width="200" height="150"> 

<!-- The Modal --> 
<div id="myModal" class="modal"> 

<!-- The Close Button --> 
<span class="close" 
onclick="document.getElementById('myModal').style.display='none'">&times;</span> 

<!-- Modal Content (The Image) --> 
<img class="modal-content" id="img01"> 

<!-- Modal Caption (Image Text) --> 
<div id="caption"></div> 
</div> 
<script> 
// Get the modal 
var modal = document.getElementById('myModal'); 

// Get the image and insert it inside the modal - use its "alt" text as a 
caption 
var img = document.getElementById('myImg'); 
var modalImg = document.getElementById("img01"); 
var captionText = document.getElementById("caption"); 
img.onclick = function(){ 
modal.style.display = "block"; 
modalImg.src = this.src; 
captionText.innerHTML = this.alt; 
} 

// Get the <span> element that closes the modal 
var span = document.getElementsByClassName("close")[0]; 

// When the user clicks on <span> (x), close the modal 
span.onclick = function() { 
modal.style.display = "none"; 
} 
</script> 

upload.php的:

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 

include("connect.php"); 
include("auth_login.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 = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
if($check !== false) { 
    echo "File saved - " . $check["mime"] . "."; 
    $uploadOk = 1; 
} else { 
    echo "File is not an image."; 
    $uploadOk = 0; 
} 
} 
+1

這裏的tutorial__你應該遵循http://php.net/file-upload –

+0

的__basic我建議你檢查我回答這個問題:https://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921它會教你很多關於圖像上傳和安全。您也可以在答案的底部下載一個非常易於使用的庫。 – icecub

+0

umm,默認情況下,如果您的上傳代碼有效,它會將文件保存在臨時文件夾中,您需要從中選擇該文件並將其放在任何公用文件夾中。按照任何一步一步的教程。我建議亞歷克斯視頻上的PHP,#新波士頓 –

回答

0

你的目標文件永遠不會被移動到實際的目錄!它停留在臨時目錄:

if

添加文件的舉動:

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file); 
+0

哪如果?我現在有兩個 –

+0

,現在可以節省,但我該如何更改圖片? –

相關問題