2013-03-29 81 views
0
<?php 

if (isset($_GET['editpic'])) { 
    $Id   = $_GET['editpic']; 
    //Get Form Data from Data base against the Id 
    $Edit_Query = "SELECT product_picture FROM products WHERE id='$Id'"; 
    $Result  = mysql_query($Edit_Query); 
    while ($Row = mysql_fetch_array($Result)) { 
     $Old_File_Name = $Row['product_picture']; 
    } 
} 

//To Update Record 
if (isset($_POST['update'])) { 
    $Id     = $_POST['id']; 
    //To get file 
    $Allowed_Extensions = array(
     'jpg', 
     'jpeg', 
     'png', 
     'bmp', 
     'gif' 
    ); 
    $Allowed_Size  = 2097152; 
    $File_Name   = $_FILES['newfile']['name']; 
    $File_Size   = $_FILES['newfile']['size']; 
    $File_Tmp   = $_FILES['newfile']['tmp_name']; 
    $File_Explode  = (explode('.', $File_Name)); 
    $File_Extension  = strtolower(end($File_Explode)); 
    //Form Validation 
    $Errormessage  = array(); 
    if (empty($File_Name)) { 
     $Errormessage[] = "Please choose an image for your product"; 
    } 
    if (!in_array($File_Extension, $Allowed_Extensions)) { 
     $Errormessage[] = "Please choose only image file"; 
    } 
    if ($File_Size > $Allowed_Size) { 
     $Errormessage[] = "Maximum file limit is 2Mb"; 
    } 
    if (empty($Errormessage)) { 
     unlink("product_images/" . $Old_File_Name); 
     if (move_uploaded_file($File_Tmp, "product_images/" . $File_Name)) { 
      //To Rename the uploaded file 
      $Random  = rand() * 1200; 
      $File_New_Name = $Random . "." . $File_Extension; 
      rename("product_images/" . $File_Name, "product_images/" . $File_New_Name); 
      $Query = "UPDATE products SET product_picture='$File_New_Name' WHERE id='$Id'"; 
      $Result = mysql_query($Query); 
      if ($Result) { 
       header("location: manage_inventory.php"); 
      } 

     } 


    } 

} //End isset update 

?> 

除了取消鏈接功能,一切正常,我無法弄清楚我的舊文件變量有什麼問題,它不會從文件夾中刪除現有文件。取消鏈接功能不起作用

另外,我需要知道爲什麼它從URL丟失?edit=$id如果任何驗證錯誤發生在我的更新。

+0

我假設用戶在運行的Apache(通常是'WWW-data')對這些圖像正確的文件權限? – castis

+0

即時通訊在我的本地主機上運行xamp是啊它的apache –

+0

它在我對代碼進行一些更改之前沒有問題......它肯定是編碼錯誤 –

回答

0

首先檢查以確保文件存在,然後測試解除鏈接調用的結果。

本質:

$fileToDelete = "product_images/".$Old_File_Name; 
if(!file_exists($fileToDelete)) { 
    die("Eh? I can't delete something that doesn't exist!"); 
} 

if(!unlink($fileToDelete)) { 
    die("I can't delete that file, check my permissions"); 
} 

(但具有更好的錯誤處理)