【发布时间】:2015-05-02 03:59:52
【问题描述】:
我是编码新手,我正在尝试在我的网站中使用 uploadify。上传文件后,我收到一条成功消息,但文件未上传到文件夹。我尝试使用在 stackoverflow 上找到的所有答案来调试代码,但似乎没有任何效果。这是 index.php 的代码
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<hr>
<title>Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="jquery.uploadify.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="uploadify.css">
<style type="text/css">
body {
font: 13px Arial, Helvetica, Sans-serif;
}
</style>
</head>
<body>
<h1>Uploadify Demo</h1>
<form>
<div id="queue"></div>
<input id="file_upload" name="file_upload" type="file" multiple="true">
</form>
<?php $timestamp=t ime();?>
<script type="text/javascript">
$(function() {
$("#file_upload").uploadify({
'uploader': 'uploadify.swf',
'cancelImg': 'uploadify-cancel.png',
'buttonText': 'Browse Files',
'script': 'uploadify.php',
'folder': '/uploads',
'fileDesc': 'Image Files',
'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
'queueSizeLimit': 9999,
'simUploadLimit': 2,
'sizeLimit': 4000000,
'checkExisting': false,
'multi': true,
'formData': {
'timestamp': '<?php echo $timestamp;?>',
'token': '<?php echo md5("unique_salt" . $timestamp);?>'
},
'auto': true,
'onUploadError': function(file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
},
'onUploadSuccess': function(file, data, response) {
alert('The file ' + file.name + ' was successfully uploaded with a response of ' /*+ response +*/ + ':' + data);
},
});
});
</script>
</body>
</html>
这是uploadify.php文件
<?php
// Define a destination
$targetFolder = '/uploads'; // Relative to the root
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
// Validate the file type
$fileTypes = array('jpg','JPG','jpeg','JPEG','gif','GIF','png','PNG'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>
还有 check-exists.php 文件
<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
// Define a destination
$targetFolder = '/uploads'; // Relative to the root and should match the upload folder in the uploader script
if (file_exists($_SERVER['DOCUMENT_ROOT'] . $targetFolder . '/' . $_POST['filename'])) {
echo 1;
} else {
echo 0;
}
?>
PS:我尝试将 $targetFolder 更改为 uploads、/uploads、/uploads/,但没有任何效果。 该文件夹有777个权限 我收到的成功消息是“文件 ________ 已成功上传,响应为:CWSaa” 我不知道 CWSaa 是什么以及它是如何回显的。
请帮忙。
【问题讨论】:
-
它只是说它是成功的,因为服务器响应了 200 状态代码,这意味着没有服务器错误,但是您可能通过将上传代码一起传递或将其过滤掉。查看您的 ajax 调用的响应以找出实际问题。
-
在uploadify.php 中回显$targetPath 以便您可以跟踪上传的路径。
-
@Selva kumar 我试图回显路径,但它仍然是空白
标签: php jquery uploadify jquery-file-upload