2014-03-18 127 views
-1

我已經嘗試過所有的想法。當我想在我的服務器上上傳照片時,名稱是寫入數據庫,但沒有上傳到服務器。PHP文件上傳不起作用

HTML

<form role="form" action="db_work.php?index=list_slider&action=new" method="post" ENCTYPE="multipart/form-data"> 
<input type="hidden" name="MAX_FILE_SIZE" value="512000"/> 

PHP

$targetFolder = 'uploads/foto/slider'; // Relative to the root 
      $tempFile = $_FILES['Filedata']['tmp_name']; 
      $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; 
      $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name']; 

      // Validate the file type 
      $fileTypes = array('jpg','jpeg','gif','png'); // File extensions 
      $fileParts = pathinfo($_FILES['Filedata']['name']); 
      $fileName = $_FILES['Filedata']['name']; 
      $fileName = strtolower ($fileName); 

      $mysqli->query("UPDATE list_slider SET item_file = '$fileName' WHERE id='$id'"); 

      if (in_array($fileParts['extension'],$fileTypes)) { 
       move_uploaded_file($tempFile,$targetFile); 
       echo '1'; 
      } else { 
       echo 'Invalid file type.'; 
      } 

這個腳本是在文件夾管理。並在根文件夾上傳。

+0

什麼是'如果(in_array($ fileParts [ '擴展'],$文件類型))' –

+1

附註的結果您處理上傳的方式極其不安全。 –

+0

確保您打開了錯誤顯示和報告。例如。 'ini_set('display_errors',1); ini_set('log_errors',1); ini_set('error_log',dirname(__ FILE__)。'/error_log.txt'); error_reporting(E_ALL);' – Jasper

回答

1
<!doctype html> 
<html lan="en"> 
<head> 
<title>Upload Script</title> 
</head> 
<body> 
<form enctype="multipart/form-data" action="uploader.php" method="POST"> 
<table align="center" width="60%"> 
<tr> 
<td colspan="3" align="center"> 
<h2>Upload Image</h2> 
</td> 
</tr> 
<input type="hidden" name="insert" value="updetails" /> 
<tr> 
<td><label for="photo">Upload Image</label></td> 
<td width="5%">:</td> 
<td><input name="uploadimage" type="file" id="photo" /></td> 
</tr> 
<tr height="20"> </tr> 
<tr> 
<td colspan="3" align="center"> 
<input type="reset" value="Reset" /> 
<input type="submit" value="Upload" name="submit"/> 
</td> 
</tr> 
</table> 
</form> 
</body> 
</html> 

uploader.php

if($_POST['submit']=='Upload') 
{ 
    $allowedExts = array("gif", "jpeg", "jpg", "png"); 

    $info = pathinfo($_FILES['uploadimage']['name']); 
    $ext = $info['extension']; // get the extension of the file 

    $target_path = "uploads/"; 
    $target_path = $target_path.str_replace(' ', '_', $_FILES["uploadimage"]["name"]); 

    if ((($_FILES["uploadimage"]["type"] == "image/gif") || ($_FILES["uploadimage"]["type"] == "image/jpeg") 
     || ($_FILES["uploadimage"]["type"] == "image/jpg") || ($_FILES["uploadimage"]["type"] == "image/pjpeg") 
     || ($_FILES["uploadimage"]["type"] == "image/x-png")|| ($_FILES["uploadimage"]["type"] == "image/png")) 
     && ($_FILES["uploadimage"]["size"] < 5242880) && in_array($ext, $allowedExts)) //5242880 Maximum File size in bytes (5mb) 
    { 
     if ($_FILES["uploadimage"]["error"] > 0) { 
      echo "Return Code: " . $_FILES["uploadimage"]["error"] . "<br>"; 
     } 
     else { 
      move_uploaded_file($_FILES['uploadimage']['tmp_name'], $target_path); 

      echo "<br />The image ". basename($_FILES['uploadimage']['name'])." has been uploaded"; 

     } 
    } 
    else 
    { 
     echo "Invalid file"; 
    } 
} 
else 
{ 
    echo"<br /><b>You Dont have permission to open this Page</b>"; 
} 
+0

你可以通過在if(move_uploaded_file($ _ FILES ['uploadimage'] ['tmp_name'],$ target_path)){db insert聲明...} –

+0

您正在使用$ _SERVER ['DOCUMENT_ROOT']。可能有一些服務器的根目錄權限問題。所以最好使用相對路徑來上傳文件。 –