2012-11-16 2 views
4

uploader.php를 사용하여 이미지를 처리하는 upload_photo.php 파일에 POST 메서드를 사용하여 사진을 업로드하고 있습니다. 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 루트 루트 4096 11 월 16 일 15:40 업로드 – rharrison33

답변

3

에서 미리보기입니다 당신이 설정 한 목적지.

파일을 이동할 경로를 다시 확인하고 해당 웹 사용자 (apache, www 또는 그 밖)가 해당 디렉토리에 쓸 수있는 권한이 있는지 확인해야합니다.

+0

업로드 권한은 다음과 같습니다. drwxrwxrwx 2 루트 루트 4096 Nov 16 15:40 uploads – rharrison33

+0

권한 문제라고 생각 되나 올바른 권한은 무엇인지 잘 모르겠습니다. – rharrison33

+0

@ rharrison33 세계 기록 가능하게 만들 이유는 없지만 소유권을 웹 사용자에게 변경하고 소유자가 쓸 수 있는지 확인하십시오. 경로가 정확한지 절대적으로 확신합니까? 당신은 상대 경로를 사용하고 있습니다. 그래서 아마 그것이 당신이 생각하는 것이 아닙니다. – jeroen

관련 문제