php手机端图片批量上传图片,手机站批量上传图片

手机站批量上传图片

pc站批量上传图片,用swfupload,使用起来很方便,做手机站时我就也用了这个插件,但是发现在手机上根本用不了,提示flash版本过低,也不可能让每一个手机客户都安装flash吧,所以就百度搜了一个手机版批量传图的方法。也很实用,唯一确定是每次只能选择一张,不能像qq空间那样,可以复选,不过能用在手机上也行了。下面贴出代码

晒图:

$(‘#test’).diyUpload({

url:’fileupload.php’,

success:function( data ) {

//alert(data.content);

console.info( data[‘result’]);

var res = ‘’;

document.getElementById(‘borough_picture_dis’).innerHTML += res;

},

error:function( err ) {

console.info( err );

}

});

header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);

header(“Last-Modified: ” . gmdate(“D, d M Y H:i:s”) . ” GMT”);

header(“Cache-Control: no-store, no-cache, must-revalidate”);

header(“Cache-Control: post-check=0, pre-check=0”, false);

header(“Pragma: no-cache”);

// Support CORS

// header(“Access-Control-Allow-Origin: *”);

// other CORS headers if any…

if ($_SERVER[‘REQUEST_METHOD’] == ‘OPTIONS’) {

exit; // finish preflight CORS requests here

}

if ( !empty($_REQUEST[ ‘debug’ ]) ) {

$random = rand(0, intval($_REQUEST[ ‘debug’ ]) );

if ( $random === 0 ) {

header(“HTTP/1.0 500 Internal Server Error”);

exit;

}

}

// header(“HTTP/1.0 500 Internal Server Error”);

// exit;

// 5 minutes execution time

@set_time_limit(5 * 60);

// Uncomment this one to fake upload time

usleep(5000);

// Settings

// $targetDir = ini_get(“upload_tmp_dir”) . DIRECTORY_SEPARATOR . “plupload”;

$targetDir = ‘upload_tmp’;

$uploadDir = ‘../data/comment’;

$cleanupTargetDir = true; // Remove old files

$maxFileAge = 5 * 3600; // Temp file age in seconds

// Create target dir

if (!file_exists($targetDir)) {

@mkdir($targetDir);

}

// Create target dir

if (!file_exists($uploadDir)) {

@mkdir($uploadDir);

}

// Get a file name

if (isset($_REQUEST[“name”])) {

$fileName = $_REQUEST[“name”];

} elseif (!empty($_FILES)) {

$fileName = $_FILES[“file”][“name”];

} else {

$fileName = uniqid(“file_”);

}

$md5File = @file(‘md5list.txt’, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$md5File = $md5File ? $md5File : array();

if (isset($_REQUEST[“md5”]) && array_search($_REQUEST[“md5”], $md5File ) !== FALSE ) {

die(‘{“jsonrpc” : “2.0”, “result” : null, “id” : “id”, “exist”: 1}’);

}

if ($fileName) {

$date = date(‘Ymdhis’).rand(1000,9999); //得到当前时间

$file1 = $fileName;

$name = explode(‘.’, $file1); //将文件名以’.’分割得到后缀名,得到一个数组

$file1 = $date . ‘.’ . $name[1]; //得到即新的路径

$file2 = $path . $file1;

$flag = 1;

}//END IF

$filePath = $targetDir . DIRECTORY_SEPARATOR . $file2;

$uploadPath = $uploadDir . DIRECTORY_SEPARATOR . $file2;

// Chunking might be enabled

$chunk = isset($_REQUEST[“chunk”]) ? intval($_REQUEST[“chunk”]) : 0;

$chunks = isset($_REQUEST[“chunks”]) ? intval($_REQUEST[“chunks”]) : 1;

// Remove old temp files

if ($cleanupTargetDir) {

if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {

die(‘{“jsonrpc” : “2.0”, “error” : {“code”: 100, “message”: “Failed to open temp directory.”}, “id” : “id”}’);

}

while (($file = readdir($dir)) !== false) {

$tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;

// If temp file is current file proceed to the next

if ($tmpfilePath == “{$filePath}_{$chunk}.part” || $tmpfilePath == “{$filePath}_{$chunk}.parttmp”) {

continue;

}

// Remove temp file if it is older than the max age and is not the current file

if (preg_match(‘/\.(part|parttmp)$/’, $file) && (@filemtime($tmpfilePath) < time() – $maxFileAge)) {

@unlink($tmpfilePath);

}

}

closedir($dir);

}

// Open temp file

if (!$out = @fopen(“{$filePath}_{$chunk}.parttmp”, “wb”)) {

die(‘{“jsonrpc” : “2.0”, “error” : {“code”: 102, “message”: “Failed to open output stream.”}, “id” : “id”}’);

}

if (!empty($_FILES)) {

if ($_FILES[“file”][“error”] || !is_uploaded_file($_FILES[“file”][“tmp_name”])) {

die(‘{“jsonrpc” : “2.0”, “error” : {“code”: 103, “message”: “Failed to move uploaded file.”}, “id” : “id”}’);

}

// Read binary input stream and append it to temp file

if (!$in = @fopen($_FILES[“file”][“tmp_name”], “rb”)) {

die(‘{“jsonrpc” : “2.0”, “error” : {“code”: 101, “message”: “Failed to open input stream.”}, “id” : “id”}’);

}

} else {

if (!$in = @fopen(“php://input”, “rb”)) {

die(‘{“jsonrpc” : “2.0”, “error” : {“code”: 101, “message”: “Failed to open input stream.”}, “id” : “id”}’);

}

}

while ($buff = fread($in, 4096)) {

fwrite($out, $buff);

}

@fclose($out);

@fclose($in);

rename(“{$filePath}_{$chunk}.parttmp”, “{$filePath}_{$chunk}.part”);

$index = 0;

$done = true;

for( $index = 0; $index < $chunks; $index++ ) {

if ( !file_exists(“{$filePath}_{$index}.part”) ) {

$done = false;

break;

}

}

if ( $done ) {

if (!$out = @fopen($uploadPath, “wb”)) {

die(‘{“jsonrpc” : “2.0”, “error” : {“code”: 102, “message”: “Failed to open output stream.”}, “id” : “id”}’);

}

if ( flock($out, LOCK_EX) ) {

for( $index = 0; $index < $chunks; $index++ ) {

if (!$in = @fopen(“{$filePath}_{$index}.part”, “rb”)) {

break;

}

while ($buff = fread($in, 4096)) {

fwrite($out, $buff);

}

@fclose($in);

@unlink(“{$filePath}_{$index}.part”);

}

flock($out, LOCK_UN);

}

@fclose($out);

}

// Return Success JSON-RPC response

die(‘{“jsonrpc” : “2.0”, “result” : “‘.$file2.'”, “id” : “id”}’);

标签:500, ==, ===, file, GET, input, IT, Open, PHP, target, upload

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
百度WebUploader是一个基于HTML5的文件上传组件,它支持多文件、分片上传、断点续传等功能。要实现图片批量上传,可以结合PHP来完成。 首先,引入WebUploader插件及其样式文件。然后在HTML中创建一个容器,用来展示上传按钮和上传结果的反馈信息。 接下来,在PHP文件中设置图片上传的相关配置。首先要指定上传的文件类型为图片类型,通过设置`accept`属性为`image/*`实现。 在PHP文件中,使用`$_FILES`来获取上传的文件信息。通过遍历`$_FILES`数组,可以获取到每个文件的相关属性,如文件名、大小、临时路径等。 然后,可以使用PHP的文件处理函数来对每个上传的文件进行处理,比如判断文件大小是否在限定范围内,判断文件类型是否为图片等。可以使用`getimagesize()`函数来判断文件是否为图片。 对于合法的图片文件,可以使用`move_uploaded_file()`函数将其从临时目录移动到指定的目录中,同时生成新的文件名,以防止文件名重复。可以使用`uniqid()`函数生成一个唯一的文件名,拼接上原文件的后缀名。 在处理完所有上传的文件后,可以将结果返回给前端页面。可以通过`json_encode()`函数将结果以JSON格式返回,其中包括上传成功的文件名和上传失败的文件列表。 最后,在前端页面的JavaScript代码中,使用百度WebUploader的API方法来实现上传功能。通过监听`fileQueued`事件,将每次选择的文件添加到队列中。可以通过设定`auto=true`来将文件自动上传。 在监听到`uploadSuccess`事件时,可以获取到上传成功的文件名,可以使用JavaScript动态生成一个缩略图,并显示在页面上。 总结起来,实现百度WebUploader图片批量上传PHP,主要包括:引入WebUploader插件和样式文件、在PHP中设置文件上传的配置、通过`$_FILES`获取上传文件的信息、判断上传的文件是否为图片并处理、返回上传结果给前端页面、在前端页面通过监听事件实现上传功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值