upload فایل توسط jquery و php

fiachehr

Member
سلام من می خوام توسط jquery و php فایل upload کنم

کد جاوا اسکریپتم اینه
[JSS]
$("#newsImg").change(function () {
var img = $("#newsImg").val();
var postURL = $("#url").val();

$.ajax({
url: postURL+'index.php/news/do_upload',
type:'POST',
dataType: 'json',
enctype: 'multipart/form-data',
data: {'file' : img},
success: function(output_string){
$("#newsContent").html(output_string);
}
});
});

[/JSS]

و کد php هم اینه

[PHPS] function do_upload()
{

$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';

$this->load->library('upload', $config);
$this->upload->initialize($config);

if ( ! $this->upload->do_upload())
{
$data = array('error' => $this->upload->display_errors());

}
else
{
$data = array('upload_data' => $this->upload->data());

}

echo json_encode($data);



}
[/PHPS]

من با code igniter کار می کنم ولی فایل سمت server ارسال نمی شه
کسی می تونه کمک کنه؟
 

ahmad_m1234

Active Member
واسه آپلود فایل میتونی از پلاگین های اماده استفاده کنی

فایل index.html
HTML Code:

<script type="text/javascript" src="ajaxfileupload.js"></script>


JavaScript Code:

JavaScript Code:

[JSS]​
$.ajaxFileUpload
(
{
url:'doajaxfileupload.php',
secureuri:false,
fileElementId:'fileToUpload',
dataType: 'json',
data:{name:'logan', id:'id'},
success: function (data, status)
{
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
$("#img").empty();
$("#img").attr("src",data.msg+'?'+Math.random());
}
}
},
error: function (data, status, e)
{
alert(e);
}
}
)

return false;

}[/JSS]

در doajaxfileupload.php
PHP Code:
PHP Code:

[PHPS]
$error = "";
$msg = "";
$fileElementName = 'fileToUpload';
function checkSize($size)
{
if($size > (1024*1024))
return false;
else
return true;
}
function checkHeight($tmp)
{
$file = getimagesize($tmp);

if($file[1] > 500)
return false;
else
return true;
}

function checkWidth($tmp)
{
$file = getimagesize($tmp);

if($file[0] > 700)
return false;
else
return true;
}

function checkExt($type)
{
$type = getimagesize($type);
if (($type['mime'] != 'image/jpeg') && ($type['mime'] != 'image/jpg') && ($type['mime'] != 'image/gif') && ($type['mime'] != 'image/png'))
return false;
else
return true;
}
if(!empty($_FILES[$fileElementName]['error']))
{
switch($_FILES[$fileElementName]['error'])
{

case '1':
$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
break;
case '2':
$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case '3':
$error = 'The uploaded file was only partially uploaded';
break;
case '4':
$error = 'No file was uploaded.';
break;

case '6':
$error = 'Missing a temporary folder';
break;
case '7':
$error = 'Failed to write file to disk';
break;
case '8':
$error = 'File upload stopped by extension';
break;
case '999':
default:
$error = 'No error code avaiable';
}
}elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none')
{
$error = 'No file was uploaded..';
}elseif( preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $_FILES['fileToUpload']['name'] ) )
{
$error = "does not consist of all letters or digits";

}elseif(!checkSize($_FILES['fileToUpload']['size']))
{
$error = 'File Size is Big';
}elseif(!checkExt($_FILES['fileToUpload']['tmp_name']))
{

$error = "File Type is not Allowed";
}elseif((!checkWidth($_FILES['fileToUpload']['tmp_name'])) || (!checkWidth($_FILES['fileToUpload']['tmp_name'])))
{
if(!checkWidth($_FILES['fileToUpload']['tmp_name'])){
$error = "File Width is Big";
}elseif(!checkWidth($_FILES['fileToUpload']['tmp_name'])){
$error = "File Height is Big";
}
}else{

move_uploaded_file($_FILES['fileToUpload']['tmp_name'],"working/".$result[0]['address']."/1.jpg");
@unlink($_FILES['fileToUpload']);
}
echo "{";
echo "error: '" . $error . "',\n";
echo "msg: '" . $msg . "'\n";
echo "}";[/PHPS]
 

پیوست ها

  • ajaxfileupload.zip
    2.1 کیلوبایت · بازدیدها: 17

جدیدترین ارسال ها

بالا