2012-11-16 59 views
4

我使用POST方法上傳upload_photo.php文件中使用uploader.php處理圖像的照片。 Uploader.php將調整圖像大小並覆蓋舊圖像。它在本地工作正常,但不在服務器上。

move_uploaded_file返回false,但是$_FILES['uploadedfile']['error'] == 0由於對我而言不會產生。

我已經發布了整個uploader.php和upload_photo.php的片段,它顯示了表單標籤。

<?php 
//This is uploader.php 
session_start(); 
include ('dbconfig.php'); 
include('SimpleImage.php'); 

mysql_connect(HOST, USERNAME, PASSWORD); 
$conn = mysql_select_db(DB_NAME); 

$target_path = "uploads/"; 

$target_path = $target_path . renameImage(); 

$_SESSION['client_photo'] = $target_path; 

$query = "UPDATE client "; 
$query .= "SET personal_photo = ('$target_path') "; 
$query .= "WHERE client_id = ".$_SESSION['clientID']; 
$results = mysql_query($query) or die('Error msg: '.mysql_error().'<br/> 
     sql: '.query.'<br/>'); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    $msg = "The file ". $_SESSION['client_photo']. 
    " has been uploaded"; 
    chmod($target_path, "0666"); 
    $work = new ImgResizer($target_path); 
    $work -> resize(600, $target_path); 
} else{ 
    $msg = "There was an error uploading the file, please try again!"; 
    $msg .= $_FILES['uploadedfile']['error']; 
} 
header("Location: upload_photo.php?msg=$msg"); 

function renameImage(){ 
    mysql_connect(HOST, USERNAME, PASSWORD); 
    $conn = mysql_select_db(DB_NAME); 

    $sql = "SELECT first_name, last_name, client_id 
      FROM client 
      WHERE client_id = ".$_SESSION['clientID']; 
    $res = mysql_query($sql) or die('Error msg: '.mysql_error().'<br/> 
       sql: '.$sql.'<br/>'); 
    if($row = mysql_fetch_array($res, MYSQLI_ASSOC)){ 
     $_SESSION['successphoto'] = 1; 
     return $row{first_name}.'_'.$row{last_name}.'_'.$row{client_id}; 
    } 
    else{ 
     echo "There was an error while fetching the row.<br/>"; 
    } 
} 

class ImgResizer { 
    private $originalFile = ''; 
    public function __construct($originalFile = '') { 
     $this -> originalFile = $originalFile; 
    } 
    public function resize($newWidth, $targetFile) { 
     if (empty($newWidth) || empty($targetFile)) { 
      return false; 
     } 
     $src = imagecreatefromjpeg($this -> originalFile); 
     list($width, $height) = getimagesize($this -> originalFile); 
     $newHeight = ($height/$width) * $newWidth; 
     $tmp = imagecreatetruecolor($newWidth, $newHeight); 
     imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
     if (file_exists($targetFile)) { 
      unlink($targetFile); 
     } 
     imagejpeg($tmp, $targetFile, 85); // 85 is my choice, make it between 0 – 100 for output image quality with 100 being the most luxurious 
    } 
} 

?> 

這裏是upload_photo.php

echo '<form enctype="multipart/form-data" action="uploader.php" method="POST">'; 
echo '<tr>'; 
//echo "<td colspan='2'>".$_GET['text']."</td>"; 
echo '</tr>'; 
echo '<tr align="center">'; 
echo '<td colspan="2">'; 
echo '<input type="hidden" name="MAX_FILE_SIZE" value="5000000" /> 
     Choose a file to upload: <input name="uploadedfile" type="file" accept="image/*"/><br /> 
     <input type="submit" value="Upload File" /> 
     </form>'; 
+0

你可以檢查你是否有寫權限,你試圖移動上傳的文件嗎? –

+0

根據你的php文件的位置,$ target_path可能是錯誤的。我不知道你的網站結構,但試試這個:$ target_path ='/ uploads /'; – wakooka

+0

@mazzucci以下是上傳的權限:drwxrwxrwx 2 root root 4096 Nov 16 15:40上傳 – rharrison33

回答

3

片斷這可能意味着,通過上傳好去 - 將文件上傳到temp目錄 - 但該文件無法被移動到你設定的目的地。

您應該仔細檢查您將文件移動到的路徑,並確保網頁用戶(apachewww或其他)有權寫入該目錄。

+0

這裏是上傳的權限:drwxrwxrwx 2 root root 4096 Nov 16 15:40上傳 – rharrison33

+0

我認爲這是一個權限問題,但我不確定什麼是正確的權限。 – rharrison33

+0

@ rharrison33沒有理由讓它成爲世界可寫的,只需將所有權改爲網絡用戶,並確保所有者可以書寫。你確定路徑是正確的嗎?你正在使用相對路徑,所以也許它不是你想象的那樣。 – jeroen

相關問題