2012-06-26 16 views
0

我有一張表格,位於這裏,http://brandybrowauto.com/npo3.html,代碼如下。第一部分是html表單本身,它只是收集一些信息,然後調用npo3.php來收集數據並上傳圖像並將一些json添加到文件中。雖然它仍然凌亂,需要大量的清理工作,但我只是想讓這個功能本身起作用。我有一些小的驗證,但在我的測試中,進入一個具有完整777權限的文件夾,我繼續得到「此擴展不允許的錯誤,請僅上傳圖片」。這是我在php頁面中的擴展檢查器。我不知道爲什麼當這個文件肯定是我在批准的數組中有這些類型之一時,我一直在獲取這個文件。我在窗體上有正確的enctype,並將文件名存儲在我在腳本中使用的變量中。任何想法發生什麼事導致每次我嘗試上傳?用php上傳圖片會產生錯誤

<form action="npo3.php" method="post" enctype="multipart/form-data" name="add_car" onsubmit="checkForm();"> 
           <table cellspacing="8" cellpadding="8" style="margin:0 auto;"> 
            <tr> 
             <td> 
              <table cellspacing="8" cellpadding="8"> 
               <tr> 
                <td> 
                 <select name="make" id="make"> 
                  <option value="">- Select Make -</option> 
                  <option value="Acura">Acura</option> 
                  <option value="Audi">Audi</option> 
                  <option value="BMW">BMW</option> 
                  <option value="Buick">Buick</option> 
                  <option value="Cadillac">Cadillac</option> 
                  <option value="Chevrolet">Chevrolet</option> 
                  <option value="Chrysler">Chrysler</option> 
                  <option value="Daewoo">Daewoo</option> 
                  <option value="Dodge">Dodge</option> 
                  <option value="Eagle">Eagle</option> 
                  <option value="Fiat">Fiat</option> 
                  <option value="Ford">Ford</option> 
                  <option value="Geo">Geo</option> 
                  <option value="GMC">GMC</option> 
                  <option value="Honda">Honda</option> 
                  <option value="Hyundai">Hyundai</option> 
                  <option value="Infiniti">Infiniti</option> 
                  <option value="Isuzu">Isuzu</option> 
                  <option value="Jaguar">Jaguar</option> 
                  <option value="Jeep">Jeep</option> 
                  <option value="Kia">Kia</option> 
                  <option value="Land_Rover">Land Rover</option> 
                  <option value="Lexus">Lexus</option> 
                  <option value="Lincoln">Lincoln</option> 
                  <option value="Mazda">Mazda</option> 
                  <option value="Mercedes">Mercedes</option> 
                  <option value="Mercury">Mercury</option> 
                  <option value="Mitsubishi">Mitsubishi</option> 
                  <option value="Nissan">Nissan</option> 
                  <option value="Oldsmobile">Oldsmobile</option> 
                  <option value="Plymouth">Plymouth</option> 
                  <option value="Pontiac">Pontiac</option> 
                  <option value="Porsche">Porsche</option> 
                  <option value="Saab">Saab</option> 
                  <option value="Saturn">Saturn</option> 
                  <option value="Scion">Scion</option> 
                  <option value="Subaru">Subaru</option> 
                  <option value="Suzuki">Suzuki</option> 
                  <option value="Toyota">Toyota</option> 
                  <option value="Volkswagen">Volkswagen</option> 
                  <option value="Volvo">Volvo</option> 
                 </select> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 <input class="gray" type="text" name="stock" id="stock" value="Stock#" onfocus="inputFocus(this);" onblur="inputBlur(this);"/> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 <input class="gray" type="text" name="name" id="name" value="Name" onfocus="inputFocus(this);" onblur="inputBlur(this);"/> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 <input class="gray" type="text" name="year" id="year" value="Year" onfocus="inputFocus(this);" onblur="inputBlur(this);"/> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 <input class="gray" type="text" name="mileage" id="mileage" value="Mileage" onfocus="inputFocus(this);" onblur="inputBlur(this);"/> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 <input class="gray" type="text" name="price" id="price" value="Price" onfocus="inputFocus(this);" onblur="inputBlur(this);"/> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 <input class="gray" type="text" name="desc" id="desc" value="Description" onfocus="inputFocus(this);" onblur="inputBlur(this);"/> 
                </td> 
               </tr> 
              </table> 
             </td> 
             <td> 
              <table cellspacing="8" cellpadding="8"> 
               <tr> 
                <td> 
                 &nbsp; 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 Image 1:&nbsp;<input type="file" name="image1" id="image1" value="Image1"/> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 Image 2:&nbsp;<input type="file" name="image2" id="image2" value="Image2"/> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 Image 3:&nbsp;<input type="file" name="image3" id="image3" value="Image3"/> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 Image 4:&nbsp;<input type="file" name="image4" id="image4" value="Image4"/> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 Image 5:&nbsp;<input type="file" name="image5" id="image5" value="Image5"/> 
                </td> 
               </tr> 
               <tr> 
                <td> 
                 <input type="submit" name="submit" id="submit" value="Submit"/> 
                </td> 
               </tr> 
              </table> 
             </td> 
            </tr> 
           </table> 
          </form> 

這是下面的npo3.php頁面。它也做了一堆其他的東西,但它在擴展檢查期間的for循環期間,我一直在打這個死信息。

<?php 
$stock = $_POST["stock"]; 
$name = $_POST["name"]; 
$year = $_POST["year"]; 
$mileage = $_POST["mileage"]; 
$price = $_POST["price"]; 
$desc = $_POST["desc"]; 
$make2 = $_POST["make"]; 

$path2 = "images/PartsCars/".$make2."/info.json"; 
$string2 = file_get_contents($path2); 
$data2 = json_decode($string2, true); 

if($stock == ""){ 
    die('You need to enter a Stock # to match the image name. Please go back and fill in the Stock#.'); 
} 
else { 
    $data2[$stock] = array("name" => $name, "year" => $year, "mileage" => $mileage, "price" => $price, "description" => $desc); 
} 

for($i = 1; $i <= 5; $i++) { 

    if($_FILES['image1']['name'] != "") { 
     $filename = $_FILES['image'.$i]['name']; 
     $filetmpname = $_FILES['image'.$i]['tmp_name']; 
     $filetype = $_FILES['image'.$i]['type']; 
     $filetype = strtolower($filetype); 

     //check if files contain php or asp and kill it 
     $pos1 = strpos($filename,'php'); 
     $pos2 = strpos($filename,'asp'); 
     if(!($pos1 === false) && !($pos2 === false)) { 
      die('You cannot upload that type of file to our system. Images only.'); 
     } 

     //get the file ext 
     $file_ext = strrchr($filename, '.'); 

     //check if extension is allowed or not 
     $allowedExt = array(".jpg",".jpeg",".gif",".png",".JPG",".PNG"); 
     if (!(in_array($file_ext, $allowedExt))){ 
      die('This extension is not allowed, please upload images only.'); 
     } 

     //check upload type 
     $pos = strpos($filetype,'image'); 
     if($pos === false) { 
      die('Only images types are allowed to be uploaded.'); 
     } 
     $imageinfo = getimagesize($_FILES['image'.$i]['tmp_name']); 
     if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/jpg' && $imageinfo['mime'] != 'image/png') { 
      die('Only images types are allowed to be uploaded.'); 
     } 

     //check double file type (image with comment) 
     if(substr_count($filetype, '/')>1){ 
      die('Only images types are allowed to be uploaded.'); 
     } 

     // upload to upload direcory 
     $uploaddir = 'images/PartsCars/'.$make2.'/' ; 
     move_uploaded_file($filetmpname, $uploaddir.$filename); 
    } 
    else { 
     die('Image 1 did not have any file selected. Please go back and select an image.'); 
    } 
} 

file_put_contents($path2, json_encode($data2)); 

$path = "images/PartsCars/".$make2."/"; 
$temp_files = scandir($path); 
$string = file_get_contents("images/PartsCars/".$make2."/info.json"); 

$data = json_decode($string); 
natsort($temp_files); 
//$temp_files = array_reverse($temp_files); 

echo '<a href="npo3.html"><button>+Add Car</button></a>'; 
echo '<table style="padding-left:20px;width:930px;">'; 

foreach($temp_files as $file) 
{ 
    if($file != "." && $file != ".." && $file != "Thumbs.db" && $file != basename(__FILE__) && $file != "info.json") 
    { 
     echo '<tr>';   
     $info = pathinfo($file); 
     $file_name = basename($file,'.'.$info['extension']); 
     $findme = array(" (2)"," (3)"," (4)"," (5)"); 
     $findme2 = array(" (1)"); 
     $pos1 = strpos($file_name, $findme[0]); 
     $pos2 = strpos($file_name, $findme[1]); 
     $pos3 = strpos($file_name, $findme[2]); 
     $pos4 = strpos($file_name, $findme[3]); 
     $subpictures = str_replace($findme, "", $file_name); 
     $subpictures2 = str_replace($findme2, "", $file_name); 

     if ($pos1 !== false || $pos2 !== false || $pos3 !== false || $pos4 !== false) { 
      echo '<script type="text/javascript">document.getElementById("'.$subpictures.'").innerHTML += "<a href=\"'.$path.$file.'\"><img src=\"'.$path.$file.'\" style=\"width:100px;height:80px;padding:10px;\"/></a>";</script>'; 
     } 
     else { 
      echo '<td style="border-bottom:1px #fff solid;width:240px;"><a href="'.$path.$file.'"><img src="'.$path.$file.'" alt="'.$file.'" style="width:200px;height:150px;padding:20px;"/></a></td>'; 
      echo '<td style="border-bottom:1px #fff solid;width:440px;">'; 
      echo '<span style="font-size:2em;padding:10px;color:#fff;font-weight:bold;">'.$data->{$file_name}->{'name'}.'</span><br/>'; 
      echo '<br/><span style="padding-left:10px;">Year: '.$data->{$file_name}->{'year'}.'</span>'; 
      echo '<br/><span style="padding-left:10px;">Mileage: '.$data->{$file_name}->{'mileage'}.'</span>'; 
      echo '<br/><span style="padding-left:10px;">Price: '.$data->{$file_name}->{'price'}.'</span>'; 
      echo '<br/><span style="padding-left:10px;">Description: '.$data->{$file_name}->{'description'}.'</span>'; 
      echo '<br/><br/><span style="padding-left:10px;">Please <a href="contact.php">contact us</a> for more details.</span></td>'; 
      echo '<td style="border-bottom:1px #fff solid;width:250px;" id="'.$subpictures2.'"></td></tr>'; 
     } 
    } 
} 
echo '</table>'; 

?> 
+0

你會得到擴展錯誤? –

回答

2

我覺得這

for($i = 1; $i <= 5; $i++) { 
    if($_FILES['image1']['name'] != "") { 

應該

for($i = 1; $i <= 5; $i++) { 
    if($_FILES['image'.$i]['name'] != "") { 

而且調試擴展您得到:

echo "Image $i:". $file_ext = strrchr($filename, '.'); 

如果你想atleas一個要上傳的文件,你可以添加一個標誌,如下圖所示。

$fileUpload = false; 
for($i = 1; $i <= 5; $i++) 
{ 
    if($_FILES['image'.$i]['name'] != "") 
    { 
     ..... 
     ..... 
     move_uploaded_file($filetmpname, $uploaddir.$filename); 
     $fileUpload = true; 
    }  
} 
if(!$fileUpload) 
{ 
    die('Please go back and select atleast one image.'); 
} 
+0

好趕上!因爲我只是檢查image1 wasnt「」它試圖在image2-5上運行該驗證,我沒有上傳圖像,所以沒有擴展名來驗證,因此不斷給我這個錯誤。我改變了它,它的工作原理,但我仍然希望能夠確保至少image1被上傳,我如何檢查第一張圖片,然後給出錯誤,如果沒有被選中? – Src1988

+0

我已更新我的代碼至少一個圖像驗證。 – VibhaJ

+0

真棒,如此簡單。感謝你的幫助。一切似乎都像現在一樣工作。對此,我真的非常感激。 – Src1988

1

如果你得到延伸檢查錯誤,那麼你是不是檢查擴展正確的,因爲我們不知道分機爲小寫字母或大寫字母,然後使文件名以一種格式使用strtolower()功能,看到帖子的image upload and resize

 $filename = strtolower($filename); // Name of the file uploaded by you 
     $extension = strrchr($filename, '.'); 
     $auth_exten = array('.jpg', '.jpeg', '.png', '.gif'); 

     if(in_array($extension, $auth_exten)){ 
      return true; 
     } 
     else{ 
      return false; 
     } 
+0

我已經在我的數組中的所有擴展的大寫版本,但我試過你提到的方式,它仍然給我錯誤的擴展名錯誤。 – Src1988

+0

謝謝你的幫助,但VibhaJ發現我的錯誤。我正在檢查我甚至沒有上傳的文件的擴展名。 – Src1988

+0

@ Src1988然後請接受VibhaJ的答案,並使其解決.. –