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