我想讓我的用戶只用PNG和JPG以及JPEG文件類型上傳圖片。 所以我在HTML文件中使用此代碼:filetype filter在php文件上傳中不起作用
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="utf-8" />
<link href="assets/css/style.css" rel="stylesheet" />
<script type="text/javascript" src='jquery.min.js' ></script>
<script type="text/javascript">
//<![CDATA[
function upload_start(){
document.getElementById('upload-process').style.visibility = 'visible';
return true;
}
function upload_end(check_upload){
var server_response = '';
if (check_upload == 1){
server_response = '<span class="ok">File Uploaded<\/span>';
}
else {
server_response = '<span class="error">file Upload error<\/span>';
}
document.getElementById('upload-process').style.visibility = 'hidden';
document.getElementById('upload-form').innerHTML = server_response;
return true;
}
//]]>
</script>
</head>
<body>
<form action="php-ajax-upload.php" method="post" enctype="multipart/form-data" target="upload-target" onsubmit="upload_start();">
<label for="user-file"></label>
<input type="file" id="user-file" name="user-file" />
<input type="hidden" name="customer_id" value="6000" />
<input type="submit" value="Upload File" />
</form>
而且php-ajax-upload.php
內容是:
<html>
<head>
<link href="assets/css/style.css" rel="stylesheet" />
</head>
<body>
<?php
$customer_id = $_POST['customer_id'] ;
if (($_FILES["user-file"]["type"] == "image/jpeg")|| ($_FILES["user-file"]["type"] == "image/jpg")|| ($_FILES["user-file"]["type"] == "image/png"))
{
if ($_FILES["user-file"]["error"] > 0){
echo "<div class=\"server\">Error: " . $_FILES["user-file"]["error"] . "</div><br />";
$check_result = 0;
}
else{
if (file_exists("user-upload/" . $_FILES["user-file"]["name"])){
echo "<div class=\"server\">This file exists <br /><br />".$_FILES["user-file"]["name"]. "</div><br />";
$check_result = 0;
}
else{
if(isset($_POST['customer_id']) && !empty($_POST['customer_id']) )
{
$customer_id = trim($_POST['customer_id']);
$filename = basename($_FILES['user-file']['name']);
$format = pathinfo($filename, PATHINFO_EXTENSION);
$saved_file_name = $customer_id.'.'.$format ;
if(is_writable(dirname(__FILE__) . './admin-upload/'. $saved_file_name))
{
unlink(dirname(__FILE__) . './admin-upload/'. $saved_file_name);
}
if(is_writable(dirname(__FILE__) . './admin-upload/'.$customer_id.'.jpeg'))
{
unlink(dirname(__FILE__) . './admin-upload/'.$customer_id.'.jpeg');
}
if(is_writable(dirname(__FILE__) . './admin-upload/'.$customer_id.'.jpg'))
{
unlink(dirname(__FILE__) . './admin-upload/'.$customer_id.'.jpg');
}
if(is_writable(dirname(__FILE__) . './admin-upload/'.$customer_id.'.png'))
{
unlink(dirname(__FILE__) . './admin-upload/'.$customer_id.'.png');
}
move_uploaded_file($_FILES["user-file"]["tmp_name"],"admin-upload/".$customer_id.'.'.$format) ;
$check_result = 1;
}
else
{
echo 'Id not found !';
$check_result = 0;
}
}
}
}
else{
if($_FILES["user-file"]["size"] > 1000000){
echo "<div class='server red bold'>File is Large</div>";
}
else{
echo "<div class='server red bold'> File Type is not Valid !</div>";
}
$check_result = 0;
}
?>
<script type="text/javascript">
window.top.window.upload_end(<?php echo $check_result; ?>);
</script>
</body>
</html>
現在,當我想檢查我的代碼,當我選擇一個Imange ir工作的很好,當我選擇其他類型的文件時,例如:pdf,doc,srt,....代碼工作良好並且會說: 文件類型無效! 但只有當我選擇mp3
文件,php-ajax-upload.php
文件錯誤:
注意:未定義指數:CUSTOMER_ID在d:\ SOFTWARE \ WAMP \ WWW \保修和\線路7
PHP的Ajax-upload.php的說明:未定義指數:用戶文件中 d:\ SOFTWARE \瓦帕\ WWW \保修和\上線PHP-Ajax的upload.php的9
說明:未定義指數:用戶文件中 d: \ Software \ wamp \ www \ garanti \ php-ajax-upload.php on line 9
說明:未定義指數:用戶文件中 d:\ SOFTWARE \瓦帕\ WWW \保修和\上線PHP-Ajax的upload.php的9
說明:未定義指數:用戶文件中 d: \ SOFTWARE \ WAMP \ WWW \保修和\ PHP的Ajax-upload.php的線64
,並最終在頁的末尾說:
文件類型無效!
我不知道爲什麼這個錯誤只會發生在mp3文件中?
真的很感謝你! :) –