2017-05-28 65 views
0

我有一個管理頁面,管理員將在窗體中插入一個新產品。我無法上傳圖片,得到未定義的fileToUpload錯誤。我需要上傳圖像並將圖像名稱存儲到數據庫中。PHP圖片上傳 - 未定義fileToUpload

這裏是我的代碼:

session_start(); 
include_once '../inc/config.php'; 

if(!isset($_SESSION['admin'])) 
{ 
    header('Location: login.php'); 
    exit; 
} 

if(isset($_POST['insert'])) 
{ 
$cname = mysqli_real_escape_string($connect, $_POST['cname']); 
$ccost = mysqli_real_escape_string($connect, $_POST['ccost']); 
$ctype = mysqli_real_escape_string($connect, $_POST['ctype']); 
$cimage = mysqli_real_escape_string($connect, $_POST['cimage']); 
$ccapa = mysqli_real_escape_string($connect, $_POST['ccapa']); 
$cstatus = mysqli_real_escape_string($connect, $_POST['cstatus']); 

$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
if($check !== false) { 
    echo "File is an image - " . $check["mime"] . "."; 
    $uploadOk = 1; 
} else { 
    echo "File is not an image."; 
    $uploadOk = 0; 
} 

if (file_exists($target_file)) { 
echo "Sorry, file already exists."; 
$uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 500000) { 
echo "Sorry, your file is too large."; 
$uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType 
!= "jpeg" 
&& $imageFileType != "gif") { 
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
$uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
echo "Sorry, your file was not uploaded."; 
// if everything is ok, try to upload file 
} else { 
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], 
$target_file)) { 
    echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
} else { 
    echo "Sorry, there was an error uploading your file."; 
} 
} 

if(mysqli_query($connect, "INSERT INTO cars(car_name, car_type, image, rent_cost, capacity, status) VALUES 
    ('".$cname."', '".$ctype."', '". basename($_FILES["fileToUpload"]["name"]). "', '".$ccost."', '".$ccapa."', '".$cstatus."')")){ 
     $success = "<div class='alert alert-success'><strong>Successfully Inserted!</strong></div>"; 
    }else{ 
     $errormsg = "<div class='alert alert-danger'>Error inserting item.</div>"; 
    } 
} 

我的HTML代碼:

<div id="insert" class="collapse"> 
 
    </br> 
 
\t <form class="form-horizontal" method="post" action=""> 
 
<fieldset> 
 

 
<!-- Form Name --> 
 
<legend>Insert an Item</legend> 
 

 
<!-- Text input--> 
 
<div class="form-group"> 
 
    <label class="col-md-4 control-label" for="textinput">Car Name</label> 
 
    <div class="col-md-4"> 
 
    <input id="textinput" name="cname" type="text" placeholder="Enter car name.." class="form-control input-md" required=""> 
 
    
 
    </div> 
 
</div> 
 

 
<!-- Text input--> 
 
<div class="form-group"> 
 
    <label class="col-md-4 control-label" for="textinput">Rent Cost</label> 
 
    <div class="col-md-4"> 
 
    <input id="textinput" name="ccost" type="number" placeholder="Enter rent cost.." class="form-control input-md" required=""> 
 
    
 
    </div> 
 
</div> 
 

 
<!-- Text input--> 
 
<div class="form-group"> 
 
    <label class="col-md-4 control-label" for="textinput">Car Type</label> 
 
    <div class="col-md-4"> 
 
    <input id="textinput" name="ctype" type="text" placeholder="Enter car type.." class="form-control input-md" required=""> 
 
    </div> 
 
</div> 
 

 
<!-- File Button --> 
 
<div class="form-group"> 
 
    <label class="col-md-4 control-label" for="filebutton">Image File</label> 
 
    <div class="col-md-4"> 
 
    <input id="filebutton" name="fileToUpload" class="input-file" type="file"> 
 
    </div> 
 
</div> 
 

 
<!-- Text input--> 
 
<div class="form-group"> 
 
    <label class="col-md-4 control-label" for="textinput">Capacity</label> 
 
    <div class="col-md-4"> 
 
    <input id="textinput" name="ccapa" type="number" placeholder="Enter car capacity.." class="form-control input-md" required=""> 
 
    
 
    </div> 
 
</div> 
 

 
<!-- Select Basic --> 
 
<div class="form-group"> 
 
    <label class="col-md-4 control-label" for="selectbasic">Status</label> 
 
    <div class="col-md-4"> 
 
    <select id="selectbasic" name="cstatus" class="form-control"> 
 
     <option value="Available">Available</option> 
 
     <option value="Not Avaialble">Not Available</option> 
 
    </select> 
 
    </div> 
 
</div> 
 

 
<!-- Button --> 
 
<div class="form-group"> 
 
    <label class="col-md-4 control-label" for="singlebutton"></label> 
 
    <div class="col-md-4"> 
 
    <button id="singlebutton" name="insert" class="btn btn-success">Insert</button> 
 
    </div> 
 
</div> 
 

 
</fieldset> 
 
</form> 
 

 
    </div>

謝謝!

回答

0

嘗試以下操作:

  1. 添加enctype="multipart/form-data"到您的表單標籤
  2. 確保您的服務器可以接受POST和上傳數據是 大到足以容納你的文件的大小
  3. 待辦事項一個print_r($_FILES)您的表格提交後,看看是否 $_FILES["fileToUpload"]定義爲
+0

完成數1和2號3,我還沒有提交任何da但是,它的顯示錯誤 – Pidaus

+0

你的腳本甚至不應該進入文件上傳得到處理的塊,除非isset($ _ POST ['insert'])爲真。你能回覆你正在看到的確切的PHP錯誤嗎?還可以考慮在腳本的頂部放置一個print_r($ _ REQUEST)來監視腳本獲得的輸入。 –

0

您需要在action=""後添加enctype="multipart/form-data"。否則發送到服務器的數據將不會被識別。

0

感謝那些回答我的問題的人。我已經解決了我的問題。 。

  • 對於不確定的變量,我只是需要把它isset內($ _ POST([ '插入'])
  • 對於PHP上傳腳本,我只需要刪除: $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; }
  • 求是,ENCTYPE =「多部分/格式數據」是必需的標籤,以便將圖像/媒體發佈到服務器。

並固定它。