2011-01-27 68 views
0

我編輯過的代碼從1圖像上傳到2,但它很醜 - 基本上只是複製了以前的變量。我想獲得8張圖片,並想象有一種方法可以創建一個數組並將其放到循環中,而不是爲每個圖像創建一個單獨的實例。循環多圖像上傳到SQL數據庫

感謝任何見解..

這是我到目前爲止的代碼:

<?PHP 

include("inc/header.php"); 

$back = "<a href='sell.php'>Click Here To Go Back And Try Again</a>"; 

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0 || $_FILES['userfile2']['size'] > 0) 
{ 

$title = $_POST['title']; 
$description = $_POST['description']; 
$category = $_POST['category']; 
$price = $_POST['price']; 
$name = $_POST['name']; 
$number = $_POST['number']; 
$email = $_POST['email']; 
$password = $_POST['password'];  


// PICTURE UPLOAD SYSTEM 

$imagename = basename($_FILES['userfile']['name']); 
$imagename2 = basename($_FILES['userfile2']['name']); 


if(empty($imagename) || empty($imagename2)) { 
$error = 1; 
echo"<h2 class='error'>The name of the image was not found.</h2>".$back; 
} 

if($error != 1 && $noimg != 1) 
{ 

     $filename = stripslashes($_FILES['userfile']['name']); 
     $extension = substr(strrchr($filename, '.'), 1); 
     $extension = strtolower($extension); 

     $filename2 = stripslashes($_FILES['userfile2']['name']); 
     $extension2 = substr(strrchr($filename2, '.'), 1); 
     $extension2 = strtolower($extension2); 

//if it is not a known extension, we will suppose it is an error and will not upload the file, 
//otherwise we will do more tests 
    } 

if (($extension != "jpg") && ($extension2 != "jpg") && ($extension != "jpeg") && ($extension != "jpeg") && ($extension != "png") && ($extension2 != "png") && ($extension != "gif") && ($extension2 != "gif")) 
     { 
     //print error message 
      echo '<h2 class="error">Error. Images Must Be Jpg, Gif, or Png Format! Please Go Back And Try Another Image.</h2>'.$back.''; 
      $errors=1; 
     } 
     else 
     { 
$time = time(); 
$newimage = "photos/" . $time . $imagename; 
$newimage2 = "photos/" . $time . $imagename2; 

$result = @move_uploaded_file($_FILES['userfile']['tmp_name'], $newimage); 
$result2 = @move_uploaded_file($_FILES['userfile2']['tmp_name'], $newimage2); 
if(empty($result)) { 
$error = 1; 
echo "<h2 class='error'>There was an error moving the uploaded file.</h2><br/>".$back.""; 
} 

// insert to SQL 

$date = date("Y-m-d G:i:s"); 
$query = "INSERT INTO classifieds (adid, title, description, cat, price, name, number, email, password, picture, picture2, date, views, authorized) VALUES ('', '$title', '$description', '$category', '$price', '$name', '$number', '$email', '$password', '$newimage', '$newimage2', '$date', '0', '0')"; 
mysql_query($query) or die(mysql_error()); 

} 
+0

爲什麼使用`@move_uploaded_file()`? – JakeParis 2011-01-27 04:16:52

+0

我沒有寫出所有這些 - 這是我正在編輯的開箱即用的東西。看起來他們不喜歡不得不調用$ _FILES全局圖像到SQL表中,因此他們使用move_uploaded_file來獲取臨時文件並將其加載到變量$ newimage中。不像你指出的那樣是一個真正的「舉動」。 – 2011-01-27 14:26:11

回答

1

如果強行輸入到一個數組中使用

<input type='file' name='file[]' /> 
<input type='file' name='file[]' /> 
<input type='file' name='file[]' /> 

然後用下面的功能我從file-upload.multiple.php

function reArrayFiles(&$file_post) { 
    $file_ary = array(); 
    $file_count = count($file_post['name']); 
    $file_keys = array_keys($file_post); 

    for ($i=0; $i<$file_count; $i++) { 
     foreach ($file_keys as $key) { 
      $file_ary[$i][$key] = $file_post[$key][$i]; 
     } 
    } 
    return $file_ary; 
} 

發現你可以遍歷數組很容易,幾乎沒有注意到你處理多個文件。

if (isset ($_POST['upload'])) { 
    extract ($_POST, EXTR_PREFIX_ALL, 'post'); 

    $file_ary = reArrayFiles ($_FILES['file']); 

    $error = 0; 
    foreach ($file_ary as $file) { 
     // Test a file is uploaded for each input 
     if ($file['size'] == 0 || empty ($file['name'])) { 
      $error = 1; 
      break; 
      // Note: If not all input slots are required to be filled, 
      //  replace the above with: 
      //   continue; 
      //  This will stop running this step of the loop here 
      //  and start the next one. 
     } 
     // Test file format 
     if (!in_array ($file['type'], array (
       'image/jpg', 'image/png', 
       'image/jpeg', 'image/gif')) { 
      $error = 2; 
      break; 
     } 
    } 

    if (!$error) { // True is returned if value is not 0; 
     $newpath = 'photos/'.time(); 
     foreach ($file_ary as $file) { 
      $res = @move_uploaded_file($file['tmp_name'], 
        $newpath.'-'.$file['name']); 
      unlink ($file['tmp_name']); 
      if (!$res) { 
       $error = 3; 
       break; 
      } 
     } 
    } 

    if (!$error) { 
     $res = mysql_query(' ... insert query ... '); 
     if (!$res) { 
      $error = 4; 
     } 
    } 

    if ($error) { 
     // If you wanted to be super thorough, you could loop through 
     // and make sure that if any files did get moved that they 
     // were deleted. 
     $msg = ""; 
     switch ($error) { 
      case 1: $msg = 'File not found.'; break; 
      case 2: $msg = 'Incorrect file format.'; break; 
      case 3: $msg = 'Error moving uploaded file.'; break; 
      case 4: $msg = 'Database query failed. '.mysql_error(); break; 
      default: $msg = 'Error'; 
     } 
     echo "<h2 class='error'>$msg</h2><a href='sell.php'>Try Again.</a>"; 
    } 
}