3
我需要一个由纯jQuery/Javascript和php5创建的多文件上传器。不使用任何Flash文件。如果任何机构有代码,请与我分享。我尝试使用File API,但不支持IE和Opera。使用纯JQuery和PHP5的多文件上传器?
我需要一个由纯jQuery/Javascript和php5创建的多文件上传器。不使用任何Flash文件。如果任何机构有代码,请与我分享。我尝试使用File API,但不支持IE和Opera。使用纯JQuery和PHP5的多文件上传器?
您可以使用blueimp jQuery File Upload为客户端上传多个文件。有很多选择,可定制。
对于PHP方面,您可能对某些想法有兴趣了解一些关于如何操作的想法(只是我的图片上传表单的一些代码片段,所以有些变量在这里没有像$ log或$ confImages那样定义,但是您可以得到这个想法):
//Сheck that we have a file
if (empty($uploaded_image))
return false;
if ($uploaded_image['error']) {
$log->LogError($uploaded_image['error'] . ": Error uploading file.");
return false;
}
list($dirname, $basename, $extension, $filename) = array_values(pathinfo($uploaded_image['name'])); //Extract variables: dirname, basename, extension, filename
//Normalize file name. I may suggest using http://cubiq.org/the-perfect-php-clean-url-generator than this
$filename = iconv("utf-8","ascii//TRANSLIT",$filename);
$allowedExt = array("jpg","jpeg","gif","png");
$extension = strtolower($extension);
//Check if the image has allowed type
if (empty($extension) || !in_array($extension, $allowedExt)) {
$log->LogError("invalid_extension: Not allowed to upload file ".(empty($extension) ? "without extension" : "with extension $extension").". Allowed Extensions: ". implode(",",$allowedExt));
return false;
}
//Generate some random string in case if such file exists on server
$postfix = substr(md5(uniqid(rand(),1)), 3, 5);
$newFile = $_SERVER['DOCUMENT_ROOT'].$upload_folder."/".$filename.'_'.$postfix.'.'.$extension; //new filename with appended random string to original filename);
//Check if the file with the same name is already exists on the server
if (file_exists($newFile)) {
$log->LogError("file_exists: File couldn't be uploaded, because such filename exist ".basename($newFile)." jau eksistē");
}
//Attempt to move the uploaded file to it's new place
if (move_uploaded_file($uploaded_image['tmp_name'],$newFile)) {
//I do some resizing also using http://phpthumb.gxdlabs.com/
$convertedimage = PhpThumbFactory::create($newFile, array("jpegQuality" => 80));
if (isset($confImages["max_width"]) || isset($confImages["max_height"]))
$convertedimage->resize($confImages["max_width"], $confImages["max_height"]);
$convertedimage->save($newFile);
//Save image path to database
...
return $image;
} else {
$log->LogError("Couldn't copy file from temp folder " . $uploaded_image['tmp_name'] . " to location " . $newFile);
}
return false;
嗨Janis Veinbergs非常感谢您的帮助。 :) – learner 2011-05-12 06:09:55