0

私はuploadifyを試していますが、無駄です。アップロードプロセスが機能しません; uploadsという名前のターゲットフォルダーを作成し、適切なアクセス許可(0755)を持っています。これが私のhtmlコードです:

     <!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.';
    }
}
?>
4

3 に答える 3

2

UploadifyjQueryを次のように変更します。

$('#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);
    }  
});

これにより、発生しているエラーが表示されるか、ファイルが正常に保存された場所が表示されます。

于 2012-06-02T14:25:44.053 に答える
0

$fileParts['extension']文字列を返します。関数in_array($needle, array $haystack)針は文字列になるため、大文字と小文字を区別して比較が行われます。だからそれを見てみてください。アップロードしている画像とリストした画像の拡張子の場合

于 2012-06-02T14:30:00.240 に答える
0

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-すべてきちんとしたダイアログボックスにあります。実際、このため、問題のデバッグも可能でした。スローされてダイアログボックスで読み取り可能になったエラーが原因で、ファイルが間違った場所にアップロードされようとしていることに気付きました。

于 2013-06-13T17:22:44.530 に答える