Message: | Trying to access array offset on value of type null |
File: | /home/romarekl/public_html/sosyallift.com/ow_plugins/forum/controllers/topic.php |
Line: | 136 |
PHP ile jQuery Ajax nasıl yüklenir
Okuyucularımdan jQuery ve PHP'ye nasıl birden çok resim yükleyeceği konusunda birçok istek aldım , bu yüzden yazdığım yazı ve jQuery ve PHP'yi aynı anda birden çok resim yüklemek . Bunu, web projeleriniz için , özellikle yeni başlayanlara odaklanan, resimlerin yüklenmesindeki prosedürleri anlamak için bir referans olarak kullanabilirsiniz .
İsteklerinizi işlemek için jQuery kitaplığı ve CSS içeren index.html dosyası .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/uploadfilemulti.css" rel="stylesheet">
<script src="js/jquery-1.8.0.min.js"></script>
<script src="js/jquery.fileuploadmulti.min.js"></script>
</head>
<body>
<div id="mulitplefileuploader">Upload</div>
<div id="status"></div>
<script>
$(document).ready(function()
{
var settings = {
url: "upload.php",
method: "POST",
allowedTypes:"jpg,png,gif,doc,pdf,zip",
fileName: "myfile",
multiple: true,
onSuccess:function(files,data,xhr)
{
$("#status").html("<font color='green'>Upload is success</font>");
},
afterUploadAll:function()
{
alert("all images uploaded!!");
},
onError: function(files,status,errMsg)
{
$("#status").html("<font color='red'>Upload is Failed</font>");
}
}
$("#mulitplefileuploader").uploadFile(settings);
});
</script>
</body>
<div id = "mulitplefileuploader"> Yükle </ div> Bu div, form ve girdi dosyası alanıyla değiştirilecek ve bazıları sürükle ve bırak görüntüsü için oluşturulacaktır. <div id = "status"> </ div> Resimlerin durumunu ve resim yüklemenin ilerleme durumunu gösterir .
upload.php
Dosya, yükleme sonrası işlemi içerir ve dosyaları yükleme klasörüne taşır.
<?php
$output_dir = "uploads/";
if(isset($_FILES["myfile"]))
{
$ret = array();
$error =$_FILES["myfile"]["error"];
{
if(!is_array($_FILES["myfile"]['name'])) //single file
{
$RandomNum = time();
$ImageName = str_replace(' ','-',strtolower($_FILES['myfile']['name']));
$ImageType = $_FILES['myfile']['type']; //"image/png", image/jpeg etc.
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $NewImageName);
//echo "<br> Error: ".$_FILES["myfile"]["error"];
$ret[$fileName]= $output_dir.$NewImageName;
}
else
{
$fileCount = count($_FILES["myfile"]['name']);
for($i=0; $i < $fileCount; $i++)
{
$RandomNum = time();
$ImageName = str_replace(' ','-',strtolower($_FILES['myfile']['name'][$i]));
$ImageType = $_FILES['myfile']['type'][$i]; //"image/png", image/jpeg etc.
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
$ret[$NewImageName]= $output_dir.$NewImageName;
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$NewImageName );
}
}
}
echo json_encode($ret);
}
?>
Birden fazla dosya döngüde işlerse, o zaman gelen tek bir dosyanın gelip gelmediğini kontrol ettikten sonra ilk önce dizin ve hata kontrol edin.
$RandomNum = time();
$ImageName = str_replace(' ','-',strtolower($_FILES['myfile']['name'][$i]));
$ImageType = $_FILES['myfile']['type'][$i]; //"image/png", image/jpeg etc.
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
Benzersiz ad oluşturmak için resim adıyla UNIX zaman damgasını ekleyin.