我正在尝试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.';
}
}
?>
答案 0 :(得分: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);
}
});
这将显示您收到的错误或显示文件成功保存到的位置。
答案 1 :(得分:0)
$fileParts['extension']
将返回一个字符串,函数in_array($needle, array $haystack)
由于针将是一个字符串,因此以区分大小写的方式进行比较。
所以试着看一下。扩展您正在上传的图片和列出的图片的情况
答案 2 :(得分: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
方便地吐出 - 所有这些都在一个整洁的对话框中。实际上,由于这个原因,甚至可以调试问题。我意识到文件试图上传到错误的位置,因为抛出的错误然后在对话框中可读。