0
我有3個文件輸入,我一直試圖保證,但我沒有成功。 我需要這些文件輸入只需要jpeg,jpg,png,和gif。我知道這個mime類型使用起來並不可靠,所以我想不再使用它。如果有更清潔或更快的方式來做到這一點,PHP程序的方式比IF語句,會更好。如何在PHP中允許某些擴展類型?
HTML代碼
<input type="file" name="index_desl_Cfile1" class="upload-image" />
<input type="file" name="index_desl_Cfile2" class="upload-image" />
<input type="file" name="index_desl_Cfile3" class="upload-image" />
PHP代碼
$target_dir = "../site_images/";
$index_deslC1 = $target_dir . basename($_FILES["index_desl_Cfile1"]["name"]);
$index_deslC2 = $target_dir . basename($_FILES["index_desl_Cfile2"]["name"]);
$index_deslC3 = $target_dir . basename($_FILES["index_desl_Cfile3"]["name"]);
// Check if file already exists
$src1 = 'http://localhost//397/admin/site_images/'.$index_deslC1;
$src2 = 'http://localhost/397/admin/site_images/'.$index_deslC2;
$src3 = 'http://localhost/397/admin/site_images/'.$index_deslC3;
if (@getimagesize($src1)) {
echo "Sorry, file already exists 1. ";
$uploadOk = 0;
}
else if (@getimagesize($src2)) {
echo "Sorry, file already exists 2. ";
$uploadOk = 0;
}
else if (@getimagesize($src3)) {
echo "Sorry, file already exists 3. ";
$uploadOk = 0;
}
$imageFileTypeC1 = $_FILES["index_desl_Cfile1"]["type"];
$imageFileTypeC2 = $_FILES["index_desl_Cfile2"]["type"];
$imageFileTypeC3 = $_FILES["index_desl_Cfile3"]["type"];
$allowed_types = array('image/jpg','image/png','image/jpeg','image/gif');
if (!in_array($imageFileTypeC1, $allowed_types)) {
echo "ILLEGAL FILE TYPE 1";
$uploadOk = 0;
}
else if (!in_array($imageFileTypeC2, $allowed_types)) {
echo "ILLEGAL FILE TYPE 2";
$uploadOk = 0;
}
else if (!in_array($imageFileTypeC3, $allowed_types)) {
echo "ILLEGAL FILE TYPE 3";
$uploadOk = 0;
}
else {
$uploadOk = 1;
}
if ($uploadOk == 0) {
echo " Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["index_desl_Cfile1"]["tmp_name"], $index_deslC1)) {
echo "The file ". basename($_FILES["index_desl_Cfile1"]["name"]). " has been uploaded.";
}
else if (move_uploaded_file($_FILES["index_desl_Cfile2"]["tmp_name"], $index_deslC2)) {
echo "The file ". basename($_FILES["index_desl_Cfile2"]["name"]). " has been uploaded.";
}
else if (move_uploaded_file($_FILES["index_desl_Cfile3"]["tmp_name"], $index_deslC3)) {
echo "The file ". basename($_FILES["index_desl_Cfile3"]["name"]). " has been uploaded.";
}
這將有3個以上的文件輸入。
你說上面的不工作? – 2015-11-20 03:10:59
它可能看起來像它,但它不起作用。這是我得到的錯誤 - ILLEGAL FILE TYPE 2抱歉,您的文件沒有上傳。 – jQueryster
'print_r($ _ FILES)是什麼?''它會一直是3嗎? – 2015-11-20 03:15:16