对我来说,没有什么比 AJAX+PHP 更难的了……但我不会放弃。
今天我发现了一个很棒的插件,它允许我使用 jQuery AJAX 和 PHP 很好地上传文件( Krajee Bootstrap FileInput ),但我无法弄清楚是什么导致了这个错误:
这是 HTML 代码:
<input id="escanneo" type="file" name="ticket[]" accept="image/*">
<div id="errorBlock43" class="help-block"></div>
jQuery 脚本:
$(document).ready(function() {
$("#escanneo").fileinput({
//showPreview: false,
browseClass: "btn btn-danger btn-block",
showCaption: false,
showRemove: false,
showUpload: false,
previewFileType: "image",
browseLabel: " Subir ticket escanneado",
browseIcon: '<i class="glyphicon glyphicon-picture"></i>',
allowedFileExtensions: ["jpg", "JPG", "jpeg", "JPEG", "png", "PNG", "pdf"],
elErrorContainer: "#errorBlock43",
msgInvalidFileExtension: 'El formato de "{name}" es incorrecto. Solo archivos "{extensions}" son admitidos.',
//AJAX
dropZoneEnabled: false,
uploadAsync: false,
uploadUrl: "subir.php", // your upload server url
uploadExtraData: function() {
return {
server: $("input[name='server']").val(),
user: $("input[name='user']").val()
};
}
});
});
上传.php文件:
<?php
// upload.php de http://webtips.krajee.com/ajax-based-file-uploads-using-fileinput-plugin/
if (empty($_FILES['ticket'])) {
return; // or process or throw an exception
}
// get the files posted
$ticket = $_FILES['ticket'];
// get server posted
$server = empty($_POST['server']) ? '' : $_POST['server'];
// get user name posted
$user = empty($_POST['user']) ? '' : $_POST['user'];
// a flag to see if everything is ok
$success = null;
// file paths to store
$paths= [];
// loop and process files
for($i=0; $i < count($ticket); $i++){
$ext = explode('.', basename($ticket['name'][$i]));
$target = "tickets/" . md5(uniqid()) . "." . array_pop($ext);
if(move_uploaded_file($ticket['tmp_name'][$i], $target)) {
$success = true;
$paths[] = $target;
} else{
$success = false;
break;
}
}
// check and process based on successful status
if ($success === true) {
// call the function to save all data to database
// code for the following function `save_data` is not
// mentioned in this example
save_data($userid, $username, $paths);
// store a successful response (default at least an empty array). You
// could return any additional response info you need to the plugin for
// advanced implementations.
$output = [];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading ticket. Contact the system administrator'];
// delete any uploaded files
foreach ($paths as $file) {
unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}
// return a json encoded response for plugin to process successfully
echo json_encode($output);
?>
和文件结构(subir.php = upload.php):
如果它有用,这是我的 php 错误日志:
[21-Jan-2015 06:34:53 Europe/Paris] PHP Warning: move_uploaded_file(/tickets/cebcea25d53b5708b9e4612fd9871284.png): failed to open stream: No such file or directory in C:\wamp\www\character-empower\subir.php on line 26
[21-Jan-2015 06:34:53 Europe/Paris] PHP Stack trace:
[21-Jan-2015 06:34:53 Europe/Paris] PHP 1. {main}() C:\wamp\www\character-empower\subir.php:0
[21-Jan-2015 06:34:53 Europe/Paris] PHP 2. move_uploaded_file() C:\wamp\www\character-empower\subir.php:26
[21-Jan-2015 06:34:53 Europe/Paris] PHP Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\php195F.tmp' to '/tickets/cebcea25d53b5708b9e4612fd9871284.png' in C:\wamp\www\character-empower\subir.php on line 26
[21-Jan-2015 06:34:53 Europe/Paris] PHP Stack trace:
[21-Jan-2015 06:34:53 Europe/Paris] PHP 1. {main}() C:\wamp\www\character-empower\subir.php:0
[21-Jan-2015 06:34:53 Europe/Paris] PHP 2. move_uploaded_file() C:\wamp\www\character-empower\subir.php:26
您可以在这里下载并测试我为这个问题制作的“精简版”版本:https://drive.google.com/file/d/0Bw5fBW4Q-fJQNHlGNXB6azNTRkE/view?usp=sharing
某处应该有语法错误!我无法弄清楚...
非常感谢任何提示/帮助!
最佳答案
您的上传目录需要可读/可写并且应该存在。看来您在 Windows 上使用它,并且文件夹分隔符很重要。
在 php 中使用 DIRECTORY_SEPARATOR 代替代码中的正斜杠(因此它可以在 Windows 或 unix/linux Web 服务器平台上运行):
$target = "tickets" . DIRECTORY_SEPARATOR . md5(uniqid()) . "." . array_pop($ext);
已更新in the webtip 。顺便说一句,祝你好运 using my plugin 。
关于php - 使用 AJAX 和 PHP 上传文件出现未知错误(BIF jQuery 插件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28060347/