2016-02-13 46 views
0

我想用圖像和文件上傳文件。 我一直在嘗試修復它的歷史,並沒有設法得到它的工作。 (我是新手) PHP:php多個文件上傳與兩個輸入

$target_dir1 = "/Files/images"; 
$target_dir2 = "/Files/files"; 
$target_file1 = $target_dir1 . basename($_FILES["file"]["name"][0]); 
$target_file2 = $target_dir2 . basename($_FILES["file"]["name"][1]); 
$uploadOk = 1; 
$fileType1 = pathinfo($target_file1,PATHINFO_EXTENSION); 
$fileType2 = pathinfo($target_file2,PATHINFO_EXTENSION); 
// Check if files already exists 
if (file_exists($target_file1)) { 
    $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, file already exists.</div>\");</script>"; 
    $uploadOk = 0; 
} 
if (file_exists($target_file2)) { 
    $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, file already exists.</div>\");</script>"; 
    $uploadOk = 0; 
} 
// Check files size 
if ($_FILES["file"]["size"][0] > 1500000) { 
    $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, your file is too large.</div>\");</script>"; 
    $uploadOk= 0; 
} 
if ($_FILES["file"]["size"][1] > 1500000) { 
    $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, your file is too large.</div>\");</script>"; 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "error"; 
} else { 
    if (move_uploaded_file($_FILES["file"]["tmp_name"][0], $target_file1)) { 
     $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> The file ". basename($_FILES["file"]["name"][0]). " has been uploaded.</div>\");</script>"; 
    } 
} 
if ($uploadOk == 0) { 
    echo "error1"; 
} else { 
    if (move_uploaded_file($_FILES["file"]["tmp_name"][1], $target_file2)) { 
     $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> The file ". basename($_FILES["file"]["name"][1]). " has been uploaded.</div>\");</script>"; 
    } 

} 

HTML:

<form class="col-lg-3" role="form" method='post' action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]."?page=Upload");?>" enctype="multipart/form-data"> 
    <div class="form-group"> 
    <label for="image">Image</label> 
    <input type="file" name="file[]" id="image"> 
    </div> 
    <div class="form-group"> 
    <label for="file">File</label> 
    <input type="file" name="file[]" id="file"> 
    </div> 
    </form> 

由於某種原因,這是行不通的。 我想要它做的是將兩個文件上傳到目錄。

更新

我後來想通,我必須要爲刪除斜線對$ TARGET_DIR面前,然後它的工作。

+0

什麼_does不工作_請更具體地 – RiggsFolly

+0

另外:你的表格是如何提交的? – fusion3k

+0

它給我太大的錯誤或只是不上傳。 – DrevanTonder

回答

0

首先,你爲什麼困惑自己。對於image,請將name保存爲image[]。並且,做下面的必要變更。

<form class="col-lg-3" role="form" method='post' action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]."?page=Upload");?>" enctype="multipart/form-data"> 
    <div class="form-group"> 
    <label for="image">Image</label> 
    <input type="file" name="image[]" id="image"> 
    </div> 
    <div class="form-group"> 
    <label for="file">File</label> 
    <input type="file" name="file[]" id="file"> 
    </div> 
</form> 

<?php 

$target_dir1 = "/Files/images"; 
$target_dir2 = "/Files/files"; 
$target_file1 = $target_dir1 . basename($_FILES["image"]["name"]); 
$target_file2 = $target_dir2 . basename($_FILES["file"]["name"]); 
$uploadOkImage = 1; 
$uploadOkFile = 1; 
$fileType1 = pathinfo($target_file1,PATHINFO_EXTENSION); 
$fileType2 = pathinfo($target_file2,PATHINFO_EXTENSION); 

// Check if files already exists 
if (file_exists($target_file1)) { 
    $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, file already exists.</div>\");</script>"; 
    $uploadOkImage = 0; 
} 
if (file_exists($target_file2)) { 
    $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, file already exists.</div>\");</script>"; 
    $uploadOkFile = 0; 
} 

// Check files size 
if ($_FILES["image"]["size"] > 1500000) { 
    $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, your file is too large.</div>\");</script>"; 
    $uploadOkImage= 0; 
} 
if ($_FILES["file"]["size"] > 1500000) { 
    $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, your file is too large.</div>\");</script>"; 
    $uploadOkFile = 0; 
} 

// Check if $uploadOk is set to 0 by an error 
if ($uploadOkImage == 0) { 
    echo "error"; 
} else { 
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file1)) { 
     $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> The file ". basename($_FILES["image"]["name"]). " has been uploaded.</div>\");</script>"; 
    } 
} 

if ($uploadOkFile == 0) { 
    echo "error1"; 
} else { 
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file2)) { 
     $msg .= "<script>document.write(\"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> The file ". basename($_FILES["file"]["name"]). " has been uploaded.</div>\");</script>"; 
    } 
} 
+0

即使兩者的文件大小都小於2mb,我仍然會得到太大的錯誤。 – DrevanTonder