2012-07-23 99 views
0

我有一個圖片上傳腳本,允許簡單的PHP上傳個人資料圖片。現在意外地失敗了上傳。我不完全確定爲什麼。我已經檢查了post_max_size和upload_max_size,它們超過1MB。我懷疑這是與/ /刪除以前的圖片,但不知道爲什麼。如果你看看它說的查詢;圖片上傳失敗意外

AND image1 !='../files/noprofile.jpg' 

這樣,如果配置文件圖片是默認配置文件,默認圖片不會被刪除。我有一種感覺,就是用這個部分來做,當圖像是默認的個人資料圖片時,上傳失敗。

我知道我應該使用mysqli,但請不要提及我正在處理它。

下面是用取出來簡單起見幾件事整個腳本:

session_start(); 
$username=$_SESSION['username']; 
$pass = $_SESSION['password']; 
$path = "../imageuploads/"; 

//Delete previous picture 
$pic = mysql_query(" 
SELECT * FROM members WHERE artist='Y' AND username='$username' AND password='$pass' AND image1 !='../files/noprofile.jpg' 
")or die(mysql_error()); 

while($fetchpic = mysql_fetch_array($pic)){ 

//if image1 does not equal no profile 
unlink($fetchpic['image1']); 

} 


$valid_formats = array("jpg", "png", "gif", "bmp", "jpeg"); 
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") 
    { 
     $name = $_FILES['photoimg1']['name']; 
     $size = $_FILES['photoimg1']['size']; 

     if(strlen($name)) 
      { 
       list($txt, $ext) = explode(".", $name); 
       if(in_array($ext,$valid_formats)) 
       { 
       if($size<(2024*2024)) 
        { 
         $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext; 
         $tmp = $_FILES['photoimg1']['tmp_name']; 
         if(move_uploaded_file($tmp, $path.$actual_image_name)) 
          { 
          mysql_query("UPDATE members SET image1='../imageuploads/$actual_image_name' WHERE username='$username' AND password ='$pass' AND artist='Y'"); 

           echo "<p1><img src='../imageuploads/".$actual_image_name."' class='imageright1'></p1>"; 
          } 
         else 
          echo "<p1>failed</p1>"; 
        } 
        else 
        echo "<p1>Your image is a bit too big. It has to be below 1MB.</p1>";     
        } 
        else 
        echo "<p1>Only JPG, PNG GIF and BMP file formats are accepted.</p1>"; 
      } 

     else 
      echo "<p1>You've gotta select an image first!</p1>"; 

     exit; 
    } 
+0

列表($ TXT,$ EXT)=爆炸(「。」,'firstname.lastname.jpg');會給出意想不到的結果 – 2012-07-23 18:17:09

+0

你推薦什麼? – 2012-07-23 18:30:18

+0

$ explodedName = explode('。',$ name);如果(in_array($ explodedName [count($ explodedName) - 1],$ valid_formats){.... – 2012-07-23 18:42:56

回答

0

在你的表單標籤,添加屬性ENCTYPE:

<form action="page.php" method="post" enctype="multipart/form-data"> 
+0

這已經完成,而不是問題 – 2012-07-23 17:23:37