作爲一名PHP新手,我在這個問題上奮鬥了幾個小時,並認爲我會分享我發現的內容。
與OP一樣,我看到文件上傳但它/他們沒有出現在uploads
文件夾中(儘管權限正確)。只是爲了讓事情非常清楚,這是我以前幫我找出我的問題
<!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
的非常有用的片段中的index.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.';
}
}
?>
正如寫在源代碼中的註釋中的uploadify.php
版本,我不得不使用$_SERVER['DOCUMENT_ROOT']
(理解和)的問題 - 所以,我用getcwd()
代替。我可以在這個解決方案通過使用之類的語句
echo $getcwd();
和
echo $_SERVER['DOCUMENT_ROOT'];
抵達看看發生了什麼事情。代碼cillosis
提供的echos
方便地被吐出 - 所有這些都在一個整潔的對話框中。事實上,即使是調試這個問題也是可能的,因爲這個。我意識到這些文件試圖上傳到錯誤的地方,因爲拋出了錯誤,然後在對話框中變得可讀。
只有當文件夾的所有者是網絡服務器用戶(通常是www-data)時,'755'作爲權限纔可以使用。 – Whisperity
您確定目錄的所有者與httpd進程的所有者相同嗎? – heximal
是的。現在我有相同的文件夾的777權限。但問題仍然存在 –