我试图制作一个简单的上传表单,将图片上传到我的本地服务器, 它确实有效,但问题是有些图片(JPG型)无法读取。 我不知道问题所在。
处理表格的代码:
<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);
?>
要检查我使用此代码段的图片数据
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
答案 0 :(得分:0)
除非你有其他代码
$ftExt = array('jpeg', 'jpg', 'png');
请记住,如果没有strtolower文本......
.JPG与.jpg
不同您可以使用不同的方式,例如1:
$ftExt = array('jpeg', 'jpg', 'png', 'JPG');
或strtolower文件名
答案 1 :(得分:0)
也许是因为文件大小?
您可以尝试在php.ini
upload_max_filesize = 10M
post_max_size = 10M
答案 2 :(得分:0)
您是否可以发送无法上传的图片的特征(文件大小,文件名)?
正如其他人指出的那样,对扩展程序的验证可能是您问题的原因,您可以轻松解决这个问题:
in_array(strtolower($ext), $ftExt)
但是文件大小也可能是一个问题,就像在大多数系统上一样(通常是共享主机方案),最大文件大小限制在2M(这就是为什么你经常看到网站限制你可以上传的图像的大小) 。
如果我记得很清楚,$ _FILES数组包含一个错误索引,所以尝试做一个
print_r($_FILES)
并向我们提供无法上传的图片的结果。