2012-02-09 48 views
1

確定這樣的問題似乎是在功能上沒有創建圖像,或在腳本它自其沒有圖像不傳遞給函數功能不會產生圖像仍然像它通過

這裏上傳腳本(注:這是注入AJAX)

$path = "cache/"; 

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

     if(strlen($name)) 
       { 
       ist($txt, $ext) = explode(".", $name); 
       if(in_array($ext,$valid_formats)) 
       { 
       if($size<(2024*2024)) 
        { 
        $new_file = "header.".$ext; 
        $tmp = $_FILES['photoimg']['tmp_name']; 
        if(move_uploaded_file($tmp, $path.$new_file)) 
        { 

      echo "<img src='cache/".$new_file."?".time()."' class='preview'>"; 

       createHeader($new_file);         
        } 

        else 
        echo "failed"; 
        } 

        else 
        echo "Image file size max 1 MB";      
        } 
        else 
       echo "Invalid file format.."; 
       } 

      else 
       echo "Please select image..!"; 

      exit; 
     } 

現在這裏是功能

function createHeader($new_file) { 

$path = "uploads/"; 
$image = "cache/"; 
$width = 800; 

    if(preg_match('/[.](jpg)|(jpeg)$/', $new_file)) { 

     $im = imagecreatefromjpeg($image . $new_file); 

    } else if (preg_match('/[.](gif)$/', $new_file)) { 

     $im = imagecreatefromgif($image . $new_file); 
     imagecolortransparent($im, black);  

    } else if (preg_match('/[.](png)$/', $new_file)) { 

     $im = imagecreatefrompng($image . $new_file); 
    } 

    imagealphablending($im, false); 
    imagesavealpha($im, true); 

    $ox = imagesx($im); 
    $oy = imagesy($im); 

    $nx = $width; 
    $ny = floor($oy * ($width/$ox)); 

    imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);  
    imagealphablending($im, true); 
    if(!file_exists($path)) { 
     if(!mkdir($path)) { 
      die("There was a problem. Please try again!"); 
     } 
     } 

    imagejpeg($nm, $path . $new_file, 100); 

    imagedestroy($im); 
    imagedestroy($nm); 

} 

在邏輯上什麼代碼必須是如果用戶使用我的CMS做的是loged,在他(她)可以更改標題在頁面上點擊一個圖標,這將使jQuery調用edit.php傳遞所需的查詢字符串。從那裏自己的用戶可以上傳一個新的圖像,它將被重命名爲header.jpg並覆蓋原始圖像

header.jpg必須上傳到uploads /目錄並且原始圖像緩存/應該是未鏈接的副本那些函數可以創建一個副本並將其放入uploads /目錄中。

發生了什麼事是第一個腳本正在上傳文件並將其放入緩存/目錄中,但該功能似乎不起作用,並且對上傳/目錄沒有任何作用。

任何想法我可能做錯了或不這樣做是造成這個惱人的問題?

+2

您的網絡服務器是否有權寫入您要存儲圖像的目錄? – thenetimp 2012-02-09 20:37:00

+0

@thenetimp是的,這是一個非常非常古老的功能,並且已經在許多服務器上進行過測試,這是我在自定義論壇軟件中使用的輕微重寫。它只是不工作的這個項目不知道這是否可以做到AJAX – 2012-02-09 20:38:11

+0

發現你的問題,看看我的答案,你呼籲圖像創建後,你回聲圖像標記,因此圖像不存在在那個時候。 – thenetimp 2012-02-09 20:55:15

回答

0

$ path =「cache /」;

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

    if(strlen($name)) 
      { 
      list($txt, $ext) = explode(".", $name); 
      if(in_array($ext,$valid_formats)) 
      { 
      if($size<(2024*2024)) 
       { 
       $new_file = "header.".$ext; 
       $tmp = $_FILES['photoimg']['tmp_name']; 
       if(move_uploaded_file($tmp, $path.$new_file)) 
       { 

      // This needs to happen before the echo of the image tag 
      createHeader($new_file);         

     echo "<img src='cache/".$new_file."?".time()."' class='preview'>"; 
       } 

       else 
       echo "failed"; 
       } 

       else 
       echo "Image file size max 1 MB";      
       } 
       else 
      echo "Invalid file format.."; 
      } 

     else 
      echo "Please select image..!"; 

     exit; 
    } 
+0

甚至不能看到該函數輸出消息 – 2012-02-09 20:43:13

+0

做一個測試它的工作,因爲我沒有得到一個錯誤,但它看起來像不是通過createHeader($ new_file)傳遞的圖像, – 2012-02-09 20:49:56

相關問題