2012-06-02 90 views
0

我正在嘗試uploadify但徒勞。上傳過程不起作用;我創建了名爲uploads的目標文件夾並具有適當的權限(0755)。這裏是我的html代碼:uplodify無法正常工作

 <!DOCTYPE html> 
<html> 
<head> 
    <title>My Uploadify Implementation</title> 
    <link rel="stylesheet" type="text/css" href="uploadify.css"> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script> 
    <script type="text/javascript" src="jquery.uploadify-3.1.min.js"></script> 
    <script type="text/javascript"> 
    $(function() { 
     $('#file_upload').uploadify({ 
      'swf'  : 'uploadify.swf', 
      'uploader' : 'uploadify.php' 

     }); 
    }); 
    </script> 
</head> 
<body> 
<input type="file" name="file_upload" id="file_upload" /> 
</body> 
</html> 

這裏是我的uplodify.php文件:

<?php 
$targetFolder = 'photogallery/uploads/' ; 

if (!empty($_FILES)) { 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; 
    $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name']; 

    // Validate the file type 
    $fileTypes = array('jpg','jpeg','gif','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.'; 
    } 
} 
?> 
+0

只有當文件夾的所有者是網絡服務器用戶(通常是www-data)時,'755'作爲權限纔可以使用。 – Whisperity

+0

您確定目錄的所有者與httpd進程的所有者相同嗎? – heximal

+0

是的。現在我有相同的文件夾的777權限。但問題仍然存在 –

回答

2

更改Uploadify jQuery的是這樣的:

$('#file_upload').uploadify({ 
    'swf'  : 'uploadify.swf', 
    'uploader' : 'uploadify.php', 
    '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); 
    } 
}); 

這將顯示任一錯誤您正在獲取或顯示文件已成功保存到的位置。

+0

我嘗試相同,但它現在似乎錯了選擇按鈕消失,並顯示一個默認的文件上傳按鈕。 –

+0

添加成功和錯誤處理程序應該對您的按鈕不起作用。你確定你沒有改變別的嗎? –

+0

是的,我確定。我只是嘗試複製和粘貼。 –

0

$fileParts['extension']將返回一個字符串,函數in_array($needle, array $haystack)由於針將是一個字符串,所以比較是以區分大小寫的方式完成的。 所以試着看看。擴展您正在上傳的圖片和您已上市的圖片

0

作爲一名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方便地被吐出 - 所有這些都在一個整潔的對話框中。事實上,即使是調試這個問題也是可能的,因爲這個。我意識到這些文件試圖上傳到錯誤的地方,因爲拋出了錯誤,然後在對話框中變得可讀。