2010-02-27 5 views
1

아파치 localhost에서 코드를 실행했고 내 호스트에서도 시도했습니다. 두 가지 방법 모두 파일을 옮겼지만 파일은 0kb로 보였다.PHP에서 move_uploaded_file() 함수에 관한 문제

경우 (는 isset ($ _ POST [ '업로드'])) {

if($_FILES['profile_foto']['size']>0&&$_FILES['profile_foto']['size']<102400){ 

     $image_extension=explode("/",$_FILES['profile_foto']['type']); 

     if($image_extension[1]!="jpeg") 
      echo "<script type='text/javascript'>alert('The extension of the profile image must be jpeg!!!')</script>"; 

     else{ 

      if($_POST['image_id']==""){ 

       $image_id= md5(uniqid()); 

       $_FILES['profile_foto']['name']="tempo/".$image_id.".jpg"; 

       move_uploaded_file($_FILES['profile_foto']['temp_name'],$_FILES['profile_foto']['name']); 

       list($width,$height)=getimagesize("tempo/".$image_id.".jpg"); 

       if($width<=0||$width>170||$height<=0||$height>200){ 

        $myFile="tempo/".$image_id.".jpg"; 
        $fh=fopen($myFile,'w') or die("The File could not be opened!!!"); 
        fclose($fh); 
        unlink($myFile); 

        echo "<script type='text/javascript'>alert('The width of your profile image must be less than 170 px, the height must be less than 200 px!!!')</script>"; 

       } 
       else 
        $_POST['image_id']=$fotograf_id; 

      } 
      else{ 

       $image_id= md5(uniqid()); 

       $_FILES['profile_foto']['name']="tempo/".$image_id.".jpg"; 

       move_uploaded_file($_FILES['profile_foto']['temp_name'],$_FILES['profile_foto']['name']); 

       list($width,$height)=getimagesize("tempo/".$image_id.".jpg"); 

       if($width<=0||$width>170||$height<=0||$height>200){ 

        $myFile="tempo/".$image_id.".jpg"; 
        $fh=fopen($myFile,'w') or die("The File could not be opened!!!"); 
        fclose($fh); 
        unlink($myFile); 

        echo "<script type='text/javascript'>alert('The width of your profile image must be less than 170 px, the height must be less than 200 px!!!')</script>"; 

       } 
       else{ 
        $image_will_be_deleted=$_POST['image_id']; 

        $myFile="tempo/".$image_will_be_deleted.".jpg"; 
        $fh=fopen($myFile,'w') or die("The File cannot be opened!!!"); 
        fclose($fh); 
        unlink($myFile); 
        $_POST['image_id']=$image_id; 

       } 
      } 
     } 
    } 
    else 
     echo "<script type='text/javascript'>alert('The size of the profile image must be less than 100 kb!!!')</script>"; 

} 
+4

$_FILES['profile_foto']['tmp_name'] 

일한다고 생각합니다. – Sarfraz

+0

코드를 실행중인 환경, O.S, 호스팅 또는 VPS 등을 지정하십시오. 질문을 다시 태그 지정하는 대신 – Alexar

+0

@ Türker 대신 코드를 표시하십시오! 그리고 SO에 오신 것을 환영합니다. –

답변

1

편리 아래 참조 어떤 당신에게 사진의 크기를 조정하기 위해 약간의 기능을 원하는 경우 : 여기

는 코드입니다. $ size1 만 제공되면 이미지는 최대 치수로 사용하여 크기가 조정됩니다. 그렇지 않으면 $ size2가 제공되면 이미지는 크기가 가장 큰 $ size1을 사용하여 비례하여 크기가 조정 된 다음 나머지는 잘립니다. 당신보다 쉽게 ​​할 수 있음 ($ 폭 < = 0 ||의 $ 폭> 170 $ 높이 < = 0 ||의 $ 높이> 200 ||) : 당신은 코드시의 약 50 라인을 게시 한

function resizeImg($name, $extension, $size1, $size2) { 
    if (preg_match('/jpg|jpeg/',$extension)){ 
     $image = imagecreatefromjpeg($name); 
    } 
    if (preg_match('/gif/',$extension)){ 
     $image = imagecreatefromgif($name); 
    } 

    $old_width = imageSX($image); 
    $old_height = imageSY($image); 
    $old_aspect_ratio = $old_width/$old_height; 

    if($size2 == 0){ 
     $new_aspect_ratio = $old_aspect_ratio; 
     if($old_width > $old_height){ 
      $new_width = $size1; 
      $new_height = $new_width/$old_aspect_ratio; 
     } else { 
      $new_height = $size1; 
      $new_width = $new_height * $old_aspect_ratio; 
     } 
    } elseif($size2 > 0){ 
     $new_aspect_ratio = $size1/$size2; 
     //for landscape potographs 
     if($old_aspect_ratio >= $new_aspect_ratio) { 
      $new_width = $size1; 
      $new_height = $size2; 
      $x1 = round(($old_width - ($old_width * ($new_aspect_ratio/$old_aspect_ratio)))/2); 
      $old_width = round($old_width * ($new_aspect_ratio/$old_aspect_ratio)); 
      $y1 = 0; 
      //for portrait photographs 
     } else{ 
      $new_width = $size1; 
      $new_height = $size2; 
      $x1 = 0; 
      $y1 = round(($old_height/2) - ($new_height/2)); 
      $old_height = round($old_width/$new_aspect_ratio); 
     } 
    } 

    $new_image = imagecreatetruecolor($new_width, $new_height); 
    imagecopyresized($new_image, $image, 0, 0, $x1, $y1, $new_width, $new_height, $old_width, $old_height); 

    return $new_image; 
} 
+0

야. 이 접근법에 대해 http://programanddesign.com/php/resize-images-using-this-php-script/ – mpen

5

당신은 단 하나가 작동하지 않는다고 주장합니다. 50 줄의 코드를 제거하고 다음 코드로 바꿉니다.

move_uploaded_file($_FILES['profile_foto']['temp_name'],'tempo/test.jpg'); 

.... 파일을 업로드하려고 할 때 어떤 결과가 발생하는지 확인하십시오.

C.는

+2

+1. 항상 간단한 단계부터 시작하여 작동하게 한 다음 한 번에 하나씩 새 기능을 추가하고 작동하는지 테스트 한 다음 반복하십시오. 복잡한 코드로 시작하면 버그를 찾을 때 잃어 버릴 수 있습니다. 크기 검사, 파일 유형 확인, 사후 처리 등의 복잡한 기능을 별도의 기능으로 분리 할 수도 있습니다. –

2

나는

$_FILES['profile_foto']['temp_name'] 

이 문 당신이 당신의 코드를 표시해야합니다

move_uploaded_file($_FILES['profile_foto']['temp_name'], $_FILES['profile_foto']['name']); 
+0

문제를 해결해 주셔서 감사합니다. 그 이유는 temp_name 대신 tmp_name을 사용해야했기 때문입니다. 복잡한 알고리즘의 전부를 찾아 주셔서 대단히 감사합니다. –

관련 문제