php - 图片上传PHP只适用于某些图片( 解决)

  显示原文与译文双语对照的内容
108 3

如果标题很混乱,我也很困惑。我试图让一个简单的上传表单上传到我的本地服务器,但是问题是有些图片( 类型 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); 
?>

要检查图片的数据,我使用这个 Fragment

echo $tyFoto;
 echo $temp;
 echo $szFoto;
 var_dump($erFile);

成功时显示

Pic Name : 3dpencil10.jpg
Pic Type : image/jpeg
Pic Temp : C:xampptmpphpFBAD.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文件的NAME 不同,我只用上面的例子。

<?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接受的发布数据的最大大小。它的值可能为 0,以禁用限制。如果数据读取被忽略,则忽略它;通过enable_post_data_reading禁用。http://php.net/post-max-size post_max_size=10M

时间:原作者:0个回答

127 1

除非你有其他代码

$ftExt = array('jpeg', 'jpg', 'png');

请记住如果没有strtolower文本。

jpg与。jpg不同

你可以使用不同的方法,例如 1:

$ftExt = array('jpeg', 'jpg', 'png', 'JPG');

或者strtolower文件名

原作者:
132 1

可能是因为大小?

你可以尝试在 php.ini 中设置下列值

upload_max_filesize = 10M
post_max_size = 10M
原作者:
73 1

你能给我们一张没有上传图像的( 文件大小,文件名)的特征?

就像其他人指出的,扩展的验证可能是你问题的原因,你可以轻松地解决这里问题:

in_array(strtolower($ext), $ftExt)

但是,文件名也可以能是一个问题,就像大多数系统( 典型的共享主机计划) 一样,2M ( 这就是为什么你经常看到网站限制你可以上传的图片大小) 中的最大文件限制。

如果我记得,$_FILES array 包含一个错误索引,那么尝试

print_r($_FILES)

给我们一个没有上传的图像的结果。

原作者:
...