2016-01-26 50 views
0

需要你的幫助。 情況如下。我有一些簡單的輸入。 用戶填寫表單並可以上傳或不上傳他的標識。但在這段代碼中,當我按提交按鈕沒有標誌有一個錯誤。所以,問題是我該如何解決這個問題,並允許用戶提交此表單,而不需要上傳文件?非常感謝!!窗體有和沒有上傳文件(php)

<? 
    if(isset($_POST['add'])){  
    $title = sf($_POST['title']); 
    $company = sf($_POST['company']); 
    $city = sf($_POST['city']); 

    $path = '../img/logo'; 
    // Get extension 
    $extension = strtolower(substr(strrchr($_FILES['logo']['name'], '.'), 1)); 

    $blacklist = array(".php", ".phtml", ".php3", ".php4", ".html", ".htm"); 
    foreach ($blacklist as $item) 
    if(preg_match("/$item\$/i", $_FILES['logo']['name'])) exit; 
    $type = $_FILES['logo']['type']; 
    $size = $_FILES['logo']['size']; 
    $filename = DFileHelper::getRandomFileName($path, $extension); 
    $target = $path . '/' . $filename . '.' . $extension; 
    if (($type != "image/jpg") && ($type != "image/jpeg")) exit; 
    if ($size > 10485760) exit; 

    move_uploaded_file($_FILES['logo']['tmp_name'], $target); 

    $jb = $mysql->prepare("INSERT INTO tb_job (j_title, j_company, j_city, j_logo) VALUES (?, ?, ?, ?)"); 
    $jb->execute(array($title, $compnay, $city, $filename,)); 

    echo 'OK'; 
    Header("Refresh: 2, /jobs/cv"); 
    } 

    ?> 

<form enctype="multipart/form-data" method="post" action="">    
    <input type="text" name="title" /> 
    <input type="text" name="compnay" /> 
    <input type="text" name="city" /> 
    <input type="file" name="logo" /> 
    <input type="submit" value="OK" /> 
</form> 
+1

你應該把一個條件,如果窗體上有一個標誌。例如'if(!empty($ _ FILES))' – roullie

+0

我應該在這種條件下放什麼行?所有的行? –

+0

你必須檢查'$ _FILES ['logo'] ['error']'是否等於'UPLOAD_ERR_NO_FILE'。如果是,則沒有文件被選擇上傳。 –

回答

0

你應該把一個條件,如果窗體上有一個標誌

<? 
    if(isset($_POST['add'])){  
    $title = sf($_POST['title']); 
    $company = sf($_POST['company']); 
    $city = sf($_POST['city']); 
    $filename = ""; 

    if(!empty($_FILES) && !$_FILES['logo']['error']){ // check if there is a "logo" 
     $path = '../img/logo'; 
     // Get extension 
     $extension = strtolower(substr(strrchr($_FILES['logo']['name'], '.'), 1)); 

     $blacklist = array(".php", ".phtml", ".php3", ".php4", ".html", ".htm"); 
     foreach ($blacklist as $item) 
     if(preg_match("/$item\$/i", $_FILES['logo']['name'])) exit; 
     $type = $_FILES['logo']['type']; 
     $size = $_FILES['logo']['size']; 
     $filename = DFileHelper::getRandomFileName($path, $extension); 
     $target = $path . '/' . $filename . '.' . $extension; 
     if (($type != "image/jpg") && ($type != "image/jpeg")) exit; 
     if ($size > 10485760) exit; 

     move_uploaded_file($_FILES['logo']['tmp_name'], $target); 
    } 

    $jb = $mysql->prepare("INSERT INTO tb_job (j_title, j_company, j_city, j_logo) VALUES (?, ?, ?, ?)"); 
    $jb->execute(array($title, $compnay, $city, $filename,)); 

    echo 'OK'; 
    Header("Refresh: 2, /jobs/cv"); 
    } 

?> 
+0

很多謝謝!當然,只是累了26小時)) –

+0

那麼,在我的情況下,即使我沒有選擇一個文件上傳,我總是在'$ _FILES'中有文件。所以我必須檢查是否有錯誤來確定文件是否被選中。 –

+0

@YaStudio沒問題。 :)很高興我的幫助,26小時不需要更高。 :) – roullie

0
if (isset($_FILES["example"])){ 
    echo true; 
}else{ 
    echo false; 
}