作为一个 PHP 新手,我在这个问题上挣扎了几个小时,并认为我会分享我的发现。
像 OP 一样,我看到文件上传但它/它们没有出现在uploads
文件夹中(尽管有正确的权限)。只是为了让事情变得非常清楚,这是index.php
我用来帮助我识别问题的方法
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>UploadiFY 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>
<script type="text/javascript">
<?php $timestamp = time();?>
$(function() {
$('#file_upload').uploadify({
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5('unique_salt' . $timestamp);?>'
},
'swf' : 'uploadify.swf',
'uploader' : 'uploadify.php',
// Snippet of code shared by cillosis
'onUploadSuccess' : function(file, data, response) {
alert('The file was saved to: ' + data);
},
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
}
});
});
</script>
</body>
</html>
cillosis
请注意,我复制并粘贴了共享的非常有用的代码片段。另外,这是uploadify.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
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
// The following line
// $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
// didn't work in my case since I had 'index.php
// (and all the other Uploadify files) at
// home/mySite/public_html/Folder1/Folder2/Folder3
// and $_SERVER['DOCUMENT_ROOT'] was returning
// home/mySite/public_html/Folder1
// and so $targetPath was actually
// home/mySite/public_html/Folder1/uploads
// which didn't (obviously exist)
// I ended up just using the 'getcwd' function and
// appending $targetFolder like so
$targetPath = getcwd() . $targetFolder;
// I guess another solution would be to use
// $_SERVER['DOCUMENT_ROOT'] and append Folder2 and Folder3
// with the necessary slashes
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
// Validate the file type
$fileTypes = array('docx','doc','rtf'); // 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.';
}
}
?>
正如源代码注释中所写的那样,我在(理解和)使用方面遇到了问题$_SERVER['DOCUMENT_ROOT']
- 所以,我getcwd()
改用了。我可以通过使用如下语句得出这个解决方案
echo $getcwd();
和
echo $_SERVER['DOCUMENT_ROOT'];
看看发生了什么。提供的echos
代码很方便地吐出它们cillosis
- 所有这些都在一个简洁的对话框中。事实上,即使是调试问题也是可能的。我意识到文件正试图上传到错误的位置,因为抛出的错误然后在对话框中变得可读。