PHP 用のファイル アップロード スクリプトを作成しました。Windows Apache サーバーでテストしていますが、最終的には Apache を使用する CentOS サーバーで動作する必要があります。私はまだデバッグ中なので、ライブの Linux マシンでテストしていません。グーグルで検索しましたが、良い解決策が見つかりません。
何が起こるのですか?.png または .jp(e)g ファイルをアップロードすると、すべてうまくいきます。スクリプトはファイルを正しいディレクトリに移動しますが、読み取り不能なファイルを出力します。下の画像を確認してください。スクリプトの私のアップロード部分:
if ($posting_photo === true) {
$uploaded_file = $_FILES['review_photo'];
$uploaded_file['ext'] = explode('.', $uploaded_file['name']);
//Only continue if a file was uploaded with a name and an extension
if ((!empty($uploaded_file['name'])) && (is_array($uploaded_file['ext']))) {
$uploaded_file['ext'] = secure(strtolower(end($uploaded_file['ext'])));
$upload_session = secure($_COOKIE[COOKIE_NAME_POST_REVIEW_SESSION]);
$upload_dir = realpath(getcwd() . DIR_REVIEW_IMAGES) . DIRECTORY_SEPARATOR . $upload_session . DIRECTORY_SEPARATOR;
$upload_ext = array('jpg', 'jpeg', 'png');
//Only continue if a file was uploaded in a right way
if (($uploaded_file['error'] == 0) && ($uploaded_file['size'] > 0) && ($uploaded_file['size'] <= MAX_FILESIZE_REVIEW_PHOTO_BYTES) && (in_array($uploaded_file['ext'], $upload_ext))) {
//Check if upload dir already exists. If so, there will be probably files in it. So we check for that too. If not, we can start with the first file.
if (is_dir($upload_dir)) {
//
//
//Part where a new file name gets generated. Not very interesting and it works well, so I left it out on Stack Overflow
//
//
} else {
mkdir($upload_dir, 0777, true);
$upload_name = $upload_session . '-1.' . $uploaded_file['ext'];
}
//Check if new upload name was generated. If not, something is wrong and we will not continue
if (!empty($upload_name)) {
chmod($upload_dir, 0777);
if (move_uploaded_file($uploaded_file['tmp_name'], $upload_dir . $upload_name)) {
$files = array_diff(@scandir($upload_dir), array('.', '..'));
//Change slashes on Windows machines for showing the image later
$upload_dir = str_replace('\\', '/', $upload_dir);
}
}
}
}
}
ここで初期化されていないすべての変数は、以前に初期化されます。Cookie は事前に設定およびチェックされ、一意のディレクトリおよびファイル名に使用されます。出力ファイルについては、この画像を確認してください。左下の x は Dropbox からのものです (Dropbox は言う: 同期できません ... アクセスが拒否されました)。フォト ビューアー ウィンドウには、次のように表示されます。ファイルの場所にアクセスできないため、この画像を開けません。ブラウザーでファイルを表示すると、許可が拒否されます。 画像へのリンク
この問題に精通している、または解決策を知っているのは誰ですか? ここで私を助けてくれることを願っています!