對不起,如果標題混亂,我也很困惑如何說出來。 我試圖做一個簡單的上傳表單上傳圖片到我的本地服務器, 它實際上工作,但問題是,一些圖片(類型JPG)無法閱讀。 我不知道是什麼問題..圖片上傳PHP只適用於某些圖片(已解決)
碼處理形式:
<form id="addPTK" method="post" action="scripts/php/addPTK.php" enctype="multipart/form-data">
<table cellpadding="3px">
<tr>
<td>
<div style="margin-bottom:10px;">
<img id="noPic" src="images/contains/no-pic.jpg" style="width:148px;height:188px;border:2px solid #000" ><br>
<input type="file" name="picPTK" style="width:55%" onchange="readURL(this)" />
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#noPic').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
</script>
</div>
</td>
</tr>
</table>
</form>
和PHP處理代碼:
<?php
// Pic Var
$nmFoto = $_FILES['picPTK']['name'];
$tyFoto = $_FILES['picPTK']['type'];
$szFoto = $_FILES['picPTK']['size'];
$erFile = $_FILES['picPTK']['error'];
$temp = $_FILES['picPTK']['tmp_name'];
$tyFoto = $_FILES['picPTK']['type'];
$dir = '../../images/ptk/';
$ftExt = array('jpeg', 'jpg', 'png');
$ftTemp = explode('.' , $nmFoto);
$ext = end($ftTemp);
?>
要檢查數據的PIC我正在使用此代碼段
echo $tyFoto;
echo $temp;
echo $szFoto;
var_dump($erFile);
成功顯示
Pic Name : 3dpencil10.jpg
Pic Type : image/jpeg
Pic Temp : C:\xampp\tmp\phpFBAD.tmp
Pic Size : 101632
Error file (var_dump) : int(0) << error shows (0) (i think it means (false)
了一個不成功的圖像也顯示
Pic Name : IMG_0668.JPG << It gets the name, but not the type
Pic Type :
Pic Temp :
Pic Size : 0
Error file (var_dump) : int(1) << error shows (1) (i think it means (true)
這是我的整個上傳腳本:
我知道POST文件的名稱是不同的,一個是我使用上面僅爲舉例。
<?php
/**
* ========= CREDIT GOES TO :==============
* Author : Ardian Septiadi AR a.k.a Refs
* Name : addPTK.php
* Version : V.01
* =======================================
*/
session_start();
if((isset($_SESSION['usercode'])) && (!empty($_SESSION['usercode']))){
try{
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'class.Table.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'fungsi.php';
// Manipulasi SESSION
$_SESSION['nik'] = trim($_POST['nik']);
$_SESSION['namaPTK'] = trim($_POST['namaPTK']);
$_SESSION['jkPTK'] = trim($_POST['jkPTK']);
$_SESSION['unitPTK'] = trim($_POST['unitPTK']);
$_SESSION['jabatan'] = trim($_POST['jabatan']);
$_SESSION['alamatPTK'] = trim($_POST['alamatPTK']);
$_SESSION['pdkTerakhir'] = trim($_POST['pdkTerakhir']);
$_SESSION['gelar'] = trim($_POST['gelar']);
$_SESSION['kawin'] = trim($_POST['kawin']);
//Rubah Telp
$telpPtk = noTelp(trim($_POST['telpPtk']));
$_SESSION['telpPtk'] = $telpPtk;
//Rubah Tgl Lahir
$tglLahir = rubahTgl(trim($_POST['tglLahir']));
$_SESSION['tglLahir'] = $tglLahir;
//Username
if(isset($_POST['hakAkses']) && $_POST['hakAkses'] == 1){
$_SESSION['hakAkses'] = $_POST['hakAkses'];
}
// Batas
// Untuk Data Pribadi
$objAddPtk = new Table('PTK');
// Variabel Foto
$nmFoto = strtolower($_FILES['fotoPTK']['name']);
$tyFoto = $_FILES['fotoPTK']['type'];
$szFoto = $_FILES['fotoPTK']['size'];
$erFile = $_FILES['fotoPTK']['error'];
$temp = $_FILES['fotoPTK']['tmp_name'];
$tyFoto = $_FILES['fotoPTK']['type'];
$dir = '../../images/ptk/';
$ftExt = array('jpeg', 'jpg', 'png' , 'JPEG', 'JPG', 'PNG');
$ftTemp = explode('.' , $nmFoto);
$ext = end($ftTemp);
// Proses Connect
if ($objAddPtk->connect()) {
if($nmFoto==''
OR $_POST['nik']==''
OR $_POST['namaPTK']==''
OR $_POST['jkPTK']==''
OR $_POST['unitPTK']==''
OR $_POST['jabatan']==''
OR $_POST['tglLahir']==''
OR $_POST['telpPtk']==''
OR $_POST['alamatPTK']==''
OR $_POST['pdkTerakhir']==''
OR $_POST['gelar']==''
OR $_POST['kawin']==''){
header('Location: ../../dashboard.php?panel=ptk&warn=1');
}elseif(!preg_match("#[0-9]+#", $_POST['nik'])){
header('Location: ../../dashboard.php?panel=ptk&warn=8');
}elseif(!preg_match("#[0-9]+#", $_POST['telpPtk'])){
header('Location: ../../dashboard.php?panel=ptk&warn=9');
}else{
// Upload Foto
if((($tyFoto=='image/jpeg')
|| ($tyFoto=='image/jpg')
|| ($tyFoto=='image/pjpg')
|| ($tyFoto=='image/x-png')
|| ($tyFoto=='image/png'))
&& ($szFoto < 2000000)
&& in_array($ext, $ftExt))
{
// Set nama baru foto
$fileBaru = $_SESSION['namaPTK'].'-'.$_SESSION['nik'].'.'.$ext;
// Kondisi penyimpanan Data
if($erFile > 0){
header('Location: ../../dashboard.php?panel=ptk&warn=10');
}else{
if(file_exists($dir.$fileBaru)){
header('Location: ../../dashboard.php?panel=ptk&warn=11');
}elseif(isset($_SESSION['hakAkses']) && !empty($_SESSION['hakAkses'])){
// Manipulasi Username
$username = explode(' ', $_POST['namaPTK'], 3);
$rand = rand(1111, 9999);
$user = $username[0].$_POST['unitPTK'].$rand;
// Batas
$savePtk = $objAddPtk->save(array(
'NIK' => $_SESSION['nik'],
'PIC_PTK' => trim($fileBaru),
'NM_PTK' => $_SESSION['namaPTK'],
'JK_PTK' => $_SESSION['jkPTK'],
'POSISI' => $_SESSION['unitPTK'],
'JABATAN' => $_SESSION['jabatan'],
'TGLLAHIR_PTK' => trim($_SESSION['tglLahir']),
'NOTLP_PTK' => trim($_SESSION['telpPtk']),
'ALAMAT_PTK' => trim($_SESSION['alamatPTK']),
'STATUS_PTK' => $_SESSION['statusKaryawan'],
'TMT' => $_SESSION['TMT'],
'AKHIR_PROBATION' => $_SESSION['akhirProbation'],
'STATUS_KAWIN' => $_SESSION['kawin'],
'PDK_TERKAHIR' => $_SESSION['pdkTerakhir'],
'GELAR_AKADEMIK' => $_SESSION['gelar'],
'TGL_REGISTRASI' => date('Y-m-d'),
'USERNAME' => trim($user),
'PASSWORD' => md5(trim($user)),
'LEVEL_AKSES' => 'Operator',
));
move_uploaded_file($temp,$dir.$fileBaru);
// Manipulasi SESSION
$_SESSION['nik'] = '';
$_SESSION['namaPTK'] = '';
$_SESSION['jkPTK'] = '';
$_SESSION['unitPTK'] = '';
$_SESSION['jabatan'] = '';
$_SESSION['tglLahir'] = '';
$_SESSION['telpPtk'] = '';
$_SESSION['alamatPTK'] = '';
$_SESSION['statusKaryawan'] = '';
$_SESSION['TMT'] = '';
$_SESSION['akhirProbation'] = '';
$_SESSION['kawin'] = '';
$_SESSION['pdkTerakhir'] = '';
$_SESSION['gelar'] = '';
// Batas
header('Location: ../../save_success.php');
}else{
$savePtk = $objAddPtk->save(array(
'NIK' => $_SESSION['nik'],
'PIC_PTK' => trim($fileBaru),
'NM_PTK' => $_SESSION['namaPTK'],
'JK_PTK' => $_SESSION['jkPTK'],
'POSISI' => $_SESSION['unitPTK'],
'JABATAN' => $_SESSION['jabatan'],
'TGLLAHIR_PTK' => trim($_SESSION['tglLahir']),
'NOTLP_PTK' => trim($_SESSION['telpPtk']),
'ALAMAT_PTK' => trim($_SESSION['alamatPTK']),
'STATUS_PTK' => $_SESSION['statusKaryawan'],
'TMT' => $_SESSION['TMT'],
'AKHIR_PROBATION' => $_SESSION['akhirProbation'],
'STATUS_KAWIN' => $_SESSION['kawin'],
'PDK_TERKAHIR' => $_SESSION['pdkTerakhir'],
'GELAR_AKADEMIK' => $_SESSION['gelar'],
'TGL_REGISTRASI' => date('Y-m-d'),
));
move_uploaded_file($temp,$dir.$fileBaru);
// Manipulasi SESSION
$_SESSION['nik'] = '';
$_SESSION['namaPTK'] = '';
$_SESSION['jkPTK'] = '';
$_SESSION['unitPTK'] = '';
$_SESSION['jabatan'] = '';
$_SESSION['tglLahir'] = '';
$_SESSION['telpPtk'] = '';
$_SESSION['alamatPTK'] = '';
$_SESSION['statusKaryawan'] = '';
$_SESSION['TMT'] = '';
$_SESSION['akhirProbation'] = '';
$_SESSION['kawin'] = '';
$_SESSION['pdkTerakhir'] = '';
$_SESSION['gelar'] = '';
// Batas
header('Location: ../../save_success.php');
}
}
}else{
header('Location: ../../dashboard.php?panel=ptk&warn=12');
}
}
}
}catch(Exception $e){
echo 'Error: '.$e->getMessage();
}
}else{
header('Location: ../../index.php');
}
我曾試圖把它變成用strtolower,也以大寫字母添加幾個類型,但它仍然無法正常工作..
找
if((($tyFoto=='image/jpeg')
|| ($tyFoto=='image/jpg')
|| ($tyFoto=='image/pjpg')
|| ($tyFoto=='image/x-png')
|| ($tyFoto=='image/png'))
&& ($szFoto < 2000000)
&& in_array($ext, $ftExt))
{
我用它來防止用戶上傳文件,但圖片, 和我不斷收到錯誤,因爲圖片的類型假定爲未知(不是圖片類型).. 所以我認爲這是問題。
最後改變我已經向我的上傳腳本解決我的問題提出,感謝所有:
<?php
/**
* ========= CREDIT GOES TO :==============
* Author : Ardian Septiadi AR a.k.a Refs
* Name : addPTK.php
* Version : V.01
* =======================================
*/
session_start();
if((isset($_SESSION['usercode'])) && (!empty($_SESSION['usercode']))){
try{
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'class.Table.php';
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'fungsi.php';
// Manipulasi SESSION
$_SESSION['nik'] = trim($_POST['nik']);
$_SESSION['namaPTK'] = trim($_POST['namaPTK']);
$_SESSION['jkPTK'] = trim($_POST['jkPTK']);
$_SESSION['unitPTK'] = trim($_POST['unitPTK']);
$_SESSION['jabatan'] = trim($_POST['jabatan']);
$_SESSION['alamatPTK'] = trim($_POST['alamatPTK']);
$_SESSION['pdkTerakhir'] = trim($_POST['pdkTerakhir']);
$_SESSION['gelar'] = trim($_POST['gelar']);
$_SESSION['kawin'] = trim($_POST['kawin']);
//Rubah Telp
$telpPtk = noTelp(trim($_POST['telpPtk']));
$_SESSION['telpPtk'] = $telpPtk;
//Rubah Tgl Lahir
$tglLahir = rubahTgl(trim($_POST['tglLahir']));
$_SESSION['tglLahir'] = $tglLahir;
//Username
if(isset($_POST['hakAkses']) && $_POST['hakAkses'] == 1){
$_SESSION['hakAkses'] = $_POST['hakAkses'];
}
// Batas
// Untuk Data Pribadi
$objAddPtk = new Table('PTK');
// Variabel Foto
$nmFoto = $_FILES['fotoPTK']['name'];
$tyFoto = $_FILES['fotoPTK']['type'];
$szFoto = $_FILES['fotoPTK']['size'];
$erFile = $_FILES['fotoPTK']['error'];
$temp = $_FILES['fotoPTK']['tmp_name'];
$tyFoto = $_FILES['fotoPTK']['type'];
$dir = '../../images/ptk/';
$ftExt = array('jpeg', 'jpg', 'png' , 'JPEG', 'JPG', 'PNG');
$ftTemp = explode('.' , $nmFoto);
$ext = end($ftTemp);
// Proses Connect
if ($objAddPtk->connect()) {
if($nmFoto==''
OR $_POST['nik']==''
OR $_POST['namaPTK']==''
OR $_POST['jkPTK']==''
OR $_POST['unitPTK']==''
OR $_POST['jabatan']==''
OR $_POST['tglLahir']==''
OR $_POST['telpPtk']==''
OR $_POST['alamatPTK']==''
OR $_POST['pdkTerakhir']==''
OR $_POST['gelar']==''
OR $_POST['kawin']==''){
header('Location: ../../dashboard.php?panel=ptk&warn=1');
}elseif(!preg_match("#[0-9]+#", $_POST['nik'])){
header('Location: ../../dashboard.php?panel=ptk&warn=8');
}elseif(!preg_match("#[0-9]+#", $_POST['telpPtk'])){
header('Location: ../../dashboard.php?panel=ptk&warn=9');
}else{
// Upload Foto
if((($tyFoto=='image/jpeg')
|| ($tyFoto=='image/jpg')
|| ($tyFoto=='image/pjpg')
|| ($tyFoto=='image/x-png')
|| ($tyFoto=='image/png')
|| ($tyFoto=='IMAGE/JPG')
|| ($tyFoto=='IMAGE/PJPG')
|| ($tyFoto=='IMAGE/X-PNG')
|| ($tyFoto=='IMAGE/PNG'))
&& ($szFoto < 5000000)
&& in_array($ext, $ftExt))
{
// Set nama baru foto
$fileBaru = $_SESSION['namaPTK'].'-'.$_SESSION['nik'].'.'.$ext;
// Kondisi penyimpanan Data
if($erFile > 0){
header('Location: ../../dashboard.php?panel=ptk&warn=10');
}else{
if(file_exists($dir.$fileBaru)){
header('Location: ../../dashboard.php?panel=ptk&warn=11');
}elseif(isset($_SESSION['hakAkses']) && !empty($_SESSION['hakAkses'])){
// Manipulasi Username
$username = explode(' ', $_POST['namaPTK'], 3);
$rand = rand(1111, 9999);
$user = $username[0].$_POST['unitPTK'].$rand;
// Batas
$savePtk = $objAddPtk->save(array(
'NIK' => $_SESSION['nik'],
'PIC_PTK' => trim($fileBaru),
'NM_PTK' => $_SESSION['namaPTK'],
'JK_PTK' => $_SESSION['jkPTK'],
'POSISI' => $_SESSION['unitPTK'],
'JABATAN' => $_SESSION['jabatan'],
'TGLLAHIR_PTK' => trim($_SESSION['tglLahir']),
'NOTLP_PTK' => trim($_SESSION['telpPtk']),
'ALAMAT_PTK' => trim($_SESSION['alamatPTK']),
'STATUS_PTK' => $_SESSION['statusKaryawan'],
'TMT' => $_SESSION['TMT'],
'AKHIR_PROBATION' => $_SESSION['akhirProbation'],
'STATUS_KAWIN' => $_SESSION['kawin'],
'PDK_TERKAHIR' => $_SESSION['pdkTerakhir'],
'GELAR_AKADEMIK' => $_SESSION['gelar'],
'TGL_REGISTRASI' => date('Y-m-d'),
'USERNAME' => trim($user),
'PASSWORD' => md5(trim($user)),
'LEVEL_AKSES' => 'Operator',
));
move_uploaded_file($temp,$dir.$fileBaru);
// Manipulasi SESSION
$_SESSION['nik'] = '';
$_SESSION['namaPTK'] = '';
$_SESSION['jkPTK'] = '';
$_SESSION['unitPTK'] = '';
$_SESSION['jabatan'] = '';
$_SESSION['tglLahir'] = '';
$_SESSION['telpPtk'] = '';
$_SESSION['alamatPTK'] = '';
$_SESSION['statusKaryawan'] = '';
$_SESSION['TMT'] = '';
$_SESSION['akhirProbation'] = '';
$_SESSION['kawin'] = '';
$_SESSION['pdkTerakhir'] = '';
$_SESSION['gelar'] = '';
// Batas
header('Location: ../../save_success.php');
}else{
$savePtk = $objAddPtk->save(array(
'NIK' => $_SESSION['nik'],
'PIC_PTK' => trim($fileBaru),
'NM_PTK' => $_SESSION['namaPTK'],
'JK_PTK' => $_SESSION['jkPTK'],
'POSISI' => $_SESSION['unitPTK'],
'JABATAN' => $_SESSION['jabatan'],
'TGLLAHIR_PTK' => trim($_SESSION['tglLahir']),
'NOTLP_PTK' => trim($_SESSION['telpPtk']),
'ALAMAT_PTK' => trim($_SESSION['alamatPTK']),
'STATUS_PTK' => $_SESSION['statusKaryawan'],
'TMT' => $_SESSION['TMT'],
'AKHIR_PROBATION' => $_SESSION['akhirProbation'],
'STATUS_KAWIN' => $_SESSION['kawin'],
'PDK_TERKAHIR' => $_SESSION['pdkTerakhir'],
'GELAR_AKADEMIK' => $_SESSION['gelar'],
'TGL_REGISTRASI' => date('Y-m-d'),
));
move_uploaded_file($temp,$dir.$fileBaru);
// Manipulasi SESSION
$_SESSION['nik'] = '';
$_SESSION['namaPTK'] = '';
$_SESSION['jkPTK'] = '';
$_SESSION['unitPTK'] = '';
$_SESSION['jabatan'] = '';
$_SESSION['tglLahir'] = '';
$_SESSION['telpPtk'] = '';
$_SESSION['alamatPTK'] = '';
$_SESSION['statusKaryawan'] = '';
$_SESSION['TMT'] = '';
$_SESSION['akhirProbation'] = '';
$_SESSION['kawin'] = '';
$_SESSION['pdkTerakhir'] = '';
$_SESSION['gelar'] = '';
// Batas
header('Location: ../../save_success.php');
}
}
}else{
header('Location: ../../dashboard.php?panel=ptk&warn=12');
}
}
}
}catch(Exception $e){
echo 'Error: '.$e->getMessage();
}
}else{
header('Location: ../../index.php');
}
最後改變我已經向我的php.ini
作出;上傳文件的最大允許大小。 ; http://php.net/upload-max-filesize upload_max_filesize = 10M
; PHP將接受的POST數據的最大大小。 ;它的值可能是0來禁用限制。如果POST數據讀取 ;通過enable_post_data_reading被禁用。 ; http://php.net/post-max-size 的post_max_size = 10M
您是否將'$ _POST'值設置到'$ _SESSION'數組中以便稍後清除它們? – AmazingDreams 2014-12-19 09:08:37
不,我這樣做,因爲進程腳本是在不同的文件,並且當它顯示錯誤的文本框或其他的價值將消失,所以有點棘手我使用會話保持它..這只是因爲我不知道如何保持它使用其他方式 – TheRefs 2014-12-19 09:20:19
您可以通過簡單地執行'$ _SESSION ['post_data'] = $ _POST'來拯救自己' – AmazingDreams 2014-12-19 10:53:41