0

我正在尝试上传,但徒劳无功。上传过程不起作用;我创建了名为 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

将您的 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);
    }  
});

这将显示您遇到的错误或显示文件成功保存到的位置。

于 2012-06-02T14:25:44.053 回答
0

$fileParts['extension']将返回一个字符串,函数in_array($needle, array $haystack)由于 needle 将是一个字符串,因此比较以区分大小写的方式进行。所以试着看看那个。您上传的图片和您列出的图片的扩展情况

于 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 回答