2017-03-09 69 views
1

我正在製作一個網站,人們可以上傳個人資料圖像。該頁面重定向,就好像沒有錯誤,並且SQL更新正常,但該圖像不會出現在其目錄中。 php.ini表示它有權上傳。此外,當我試圖在本地主機上同樣的事情,它的工作完美PHP上傳圖像不做任何事,沒有給出錯誤

的HTML:

<!DOCTYPE html> 
<html> 
<body> 

<form action="upload.php" method="post" enctype="multipart/form-data"> 
    Select image to upload: 
    <input type="file" name="file" id="file"> 
    <input type="submit" value="Upload Image" name="submit"> 
</form> 

</body> 
</html> 

的PHP:

<?php 
session_start(); 
include_once 'dbh.php'; 
$id = $_SESSION['userID']; 

if (isset($_POST['submit'])) { 
$file = $_FILES['file']; 

//about the file 
$fileName = $file['name']; 
$fileTmpName = $file['tmp_name']; 
$fileSize = $file['size']; 
$fileError = $file['error']; 
$fileType = $file['type']; 

$fileExt = explode('.', $fileName); 
$fileActualExt = strtolower(end($fileExt)); 

$allowed = array('jpg', 'jpeg', 'png'); 

    //checks if it's in the allowed type 
     if (in_array($fileActualExt, $allowed)) { 
      //checks for errors 
      if ($fileError === 0) { 
       //checks for appropriate file size 
       if ($fileSize < 1000000) { 
       $fileNameNew = "profile".$id.".".$fileActualExt; 
       $fileDestination = 'uploads/'.$fileNameNew; 
       move_uploaded_file($fileTmpName, $fileDestination); 
       $sql = "UPDATE users SET profilePicture='$fileNameNew' WHERE userID='$id';"; 
       $result = mysqli_query($conn, $sql); 
       header("Location: index.php?uploadsuccess"); 
       } else { 
        echo "Your file is too big!"; 
       } 
      } else { 
       echo "There was an error uploading your file!"; 
      } 
     } else { 
      echo "You must choose a valid file type!"; 
     } 

} 
+0

'的error_reporting(E_ALL); ini_set('display_errors','On');'在腳本的頂部可能會顯示'move_uploaded_file'失敗以及原因。 – drew010

+1

@ drew010,這導致我的解決方案,謝謝! – Sebastian

+0

很高興幫助你!該技巧將爲您節省將來無數小時的時間,並在開發過程中遇到問題時始終記住它。 – drew010

回答

0

檢查路徑上傳目錄。

$path = getcwd(); 
echo $path; 

$fileDestination = 'uploads/'.$fileNameNew; 

最終可能看起來像

$fileDestination = '/home/user/public_html/uploads/'.$fileNameNew;

相關問題