PHP 파일 업로드의 백 슬래시
-
21-12-2019 - |
문제
작은 배경 :
Godaddy의 공유 Windows 호스팅 계획에 대한 교회 웹 사이트가 있습니다. Windows가 싫어하고 사이트를 Linux 기반 서버로 이동하면 호스팅 계정이 아니므로 그렇게 할 수 없습니다. 나는 현재 웹 사이트에 설교를 업로드하기 위해 GUI를 구현하려고 노력하고 있습니다. Linux 서버 에서이 작업을 수행하는 방법을 알고 있으므로 Windows 코드가 비슷한 줄 알고 있습니다. 내가 붙어있는 한 곳은 업로드 경로입니다. Godaddy 서버의 절대 경로는
입니다.D:\Hosting\5402716\html\WFBC\wacofamily.com\sermons\2014\
.
그러나 백 슬래시는 특수 문자를 이스케이프로 이스므로 두 번 필요했습니다.
D:\\Hosting\\5402716\\html\\WFBC\\wacofamily.com\\sermons\\2014\\
.
문제점 :
i 다음 업로드를 테스트했지만이 오류가 발생했습니다 :
다음 시스템 수준의 메시지가 수신되었습니다. 사용 권한 또는 관련 오류 파일이 D : 호스팅, 02716HTMLWFBCWACOFAMILE.COMSERMONS4WFBC20130106AM.MP3
PHP가 \ 54 및 \ 201을 제외한 모든 백 슬래시를 제거하는 것으로 보입니다. 이 사이트 , \ 54 쉼표이고 \ 201 미사용 (또는 공백)입니다. 이것은 내가 왜 쉼표를 얻고 있는지와 2014 년에 2014 년이 왜 사라지는지 설명합니다. 그러나 이중 백 슬래시가 단일 백 슬래시가되지 않는 이유는 설명하지 않습니다. 다음은 이미지를 업로드 해야하는 PHP 스크립트입니다.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
require_once 'authorize.php';
if(!(strtolower($this_user_type) == 'admin') && !(strtolower($this_user_type) == 'administrator') && !(strtolower($this_user_type) == 'elder')) {
header('Location: /login.php?message=You%20do%20not%20have%20permission%20to%20view%20this%20page.%20%20You%20are%20a(n)%20'.$this_user_type.'.');
exit;
}
ini_set('upload_max_filesize', '20971520');
ini_set('post_max_size', '20971520');
ini_set('memory_limit', '20971520');
ini_set('max_input_time', 360);
ini_set('max_execution_time', 360);
require_once 'appConfig.php';
require_once 'databaseConnection.php';
$php_errors = array(1 => 'Maximum file size in php.ini exceeded', 2 => 'Maximum file size in HTML form exceeded', 3 => 'Only part of the file was uploaded', 4 => 'No file was selected to upload.');
$article_id = htmlentities(trim($_REQUEST['sermon_id']));
$date = htmlentities(trim($_REQUEST['date']));
$pastor = htmlentities(trim($_REQUEST['pastor']));
$title = htmlentities(trim($_REQUEST['title']));
$passage = htmlentities(trim($_REQUEST['passage']));
$folder_name = date('Y', strtotime($date));
if (!is_dir('../../sermons/'.$folder_name."/")) {
mkdir('../../sermons/'.$folder_name, 0777) or handle_error("the server couldn't upload the image you selected.", 'could not create directory');
}
$upload_dir = HOST_WWW_ROOT.'sermons\\'.$folder_name.'\\';
$image_fieldname = "sermon_mp3";
($_FILES[$image_fieldname]['error'] == 0) or handle_error("the server couldn't upload the image you selected.", $php_errors[$_FILES[$image_fieldname]['error']]);
@is_uploaded_file($_FILES[$image_fieldname]['tmp_name']) or handle_error("you were trying to do something naughty. Shame on you!", "Uploaded request: file named '{$_FILES[$image_fieldname]['tmp_name']}'");
$upload_filename = $upload_dir.$_FILES[$image_fieldname]['name'];
@move_uploaded_file($_FILES[$image_fieldname]['tmp_name'], $upload_filename) or handle_error("we had a problem saving your image to its permanent location.", "permissions or related error moving file to {$upload_filename}");
if ($article_id) {
$stmt = $mysqli->prepare("UPDATE `wfbcsermons`.`sermons` SET `date`=?, `pastor`=?, `sermon`=?, `book`=?, `chapter`=?, `end_chapter`=?, `start_verse`=?, `end_verse`=?, `path`=? WHERE `id`=?;") or handle_error("There was a problem updating the database.", "prepare failed :".htmlspecialchars($mysqli->error));
$stmt->bind_param('sssssssssi', $sermon_date, $sermon_pastor, $sermon_title, $book, $chapter, $end_chapter, $start_verse, $end_verse, $sermon_path, $id) or handle_error("There was a problem updating the database.", "bind_param failed :".htmlspecialchars($stmt->error));
$sermon_date = $date;
$sermon_pastor = $pastor;
$sermon_title = $title;
$passage_pieces = explode(" ", $passage);
$book = $passage_pieces[0];
$number_pieces = explode("-", $passage_pieces[1]);
$start_pieces = explode(":", $number_pieces[0]);
$chapter = $start_pieces[0];
$start_verse = $start_pieces[1];
$end_pieces = explode(":", $number_pieces[1]);
$end_chapter = $start_pieces[0];
$end_verse = $start_pieces[1];
$sermon_path = 'http://www.wacofamily.com/sermons/'.$folder_name.'/'.$_FILES[$image_fieldname]['name'];
$id = $sermon_id;
$stmt->execute() or handle_error("There was a problem updating the database.", "execute failed :".htmlspecialchars($stmt->error));
}
else {
$stmt = $mysqli->prepare("INSERT INTO `wfbcsermons`.`sermons` (`date`, `pastor`, `sermon`, `book`, `chapter`, `end_chapter`, `start_verse`, `end_verse`, `path`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);") or handle_error("There was a problem updating the database.", "prepare failed :".htmlspecialchars($mysqli->error));
$stmt->bind_param('sssssssssi', $sermon_date, $sermon_pastor, $sermon_title, $book, $chapter, $end_chapter, $start_verse, $end_verse, $sermon_path) or handle_error("There was a problem updating the database.", "bind_param failed :".htmlspecialchars($stmt->error));
$sermon_date = $date;
$sermon_pastor = $pastor;
$sermon_title = $title;
$passage_pieces = explode(" ", $passage);
$book = $passage_pieces[0];
$number_pieces = explode("-", $passage_pieces[1]);
$start_pieces = explode(":", $number_pieces[0]);
$chapter = $start_pieces[0];
$start_verse = $start_pieces[1];
$end_pieces = explode(":", $number_pieces[1]);
$end_chapter = $start_pieces[0];
$end_verse = $start_pieces[1];
$sermon_path = 'http://www.wacofamily.com/sermons/'.$folder_name.'/'.$_FILES[$image_fieldname]['name'];
$stmt->execute() or handle_error("There was a problem updating the database.", "execute failed :".htmlspecialchars($stmt->error));
}
$stmt->close();
$mysqli->close();
header("Location: ../../admin.php");
exit();
?>
.
host_www_root :
를 정의하는 app_config.php입니다.<?php
define("DEBUG_MODE", true);
define("SITE_ROOT", "http://www.wacofamily.com/");
define("DATABASE_HOST", "wfbcsermons.db.5402716.hostedresource.com");
define("DATABASE_USERNAME", "**********");
define("DATABASE_PASSWORD", "**********");
define("DATABASE_NAME", "wfbcsermons");
define("HOST_WWW_ROOT", "D:\\Hosting\\5402716\\html\\WFBC\\wacofamily.com\\");
function js_redirect($url, $seconds=0) {
echo "<script language=\"JavaScript\">\n";
echo "<!-- hide from old browser\n\n";
echo "function redirect() {\n";
echo "window.location = \"" . $url . "\";\n";
echo "}\n\n";
echo "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n";
echo "-->\n";
echo "</script>\n";
return true;
}
function handle_error($user_error_message, $system_error_message) {
js_redirect('http://www.wacofamily.com/error.php?error_message='.$user_error_message.'&system_error_message='.$system_error_message, 0);
exit();
}
function debug_print($message) {
if (DEBUG_MODE) {
echo $message;
}
}
?>
.
내가 시도한 것 :
다음 문자열을 시도했습니다.
4 개의 백 슬래시 (regex) :
define("HOST_WWW_ROOT", "D:\\\\Hosting\\\\5402716\\\\html\\\\WFBC\\\\wacofamily.com\\\\");
.
단일 백 슬래시 내부 작은 따옴표 :
define("HOST_WWW_ROOT", 'D:\Hosting\5402716\html\WFBC\wacofamily.com\\');
.
백 슬래시의 HTML 문자 코드 인 \
를 사용하여
define("HOST_WWW_ROOT", "D:\Hosting\5402716\html\WFBC\wacofamily.com\");
.
백 슬래시의 8 진수 순서가되어야하는 \134
를 사용하여
define("HOST_WWW_ROOT", "D:\134Hosting\1345402716\134html\134WFBC\134wacofamily.com\134");
.
앞으로 슬래시를 이 질문
define("HOST_WWW_ROOT", "D:/Hosting/402716/html/WFBC/wacofamily.com/");
.
directory_separator를 사용하여 제안 :
define("HOST_WWW_ROOT", "D:".DIRECTORY_SEPARATOR."Hosting".DIRECTORY_SEPARATOR ."402716".DIRECTORY_SEPARATOR ."html".DIRECTORY_SEPARATOR ."WFBC".DIRECTORY_SEPARATOR ."wacofamily.com".DIRECTORY_SEPARATOR );
.
및 물론 이중 백 슬래시
define("HOST_WWW_ROOT", "D:\\Hosting\\5402716\\html\\WFBC\\wacofamily.com\\");
.
업로드 경로 생성과 관련된 모든 문자열을 변경했습니다. 공간을 절약하기 위해 host_www_root 정의 만 포함했습니다.
sirnarsh가 나를 생각 나게했던 것처럼, 내가 사용하는 경로 에코 에코, 그것은 백 슬래시와 잘 나타납니다. 그러나, 뭔가가 건초 와이어가 될 move_uploaded_file 함수에 대한 경로를 전달하는 경우입니다.
해결책 2
OK, 나는 그것을 알아 냈고 이제는 벙어리를 느낍니다.나는 슬래시를 사용하여 끝났습니다.이것이 이전에 작동하지 않은 이유는 사용 권한 (내가 확인한 것이라고 생각했던 것)이 나를 쓰지 않도록 설정되지 않았다는 것입니다.그래서,이 문제가있는 다른 누구에게도 :
- 앞으로 슬래시를 사용
- 귀하의 권한을 확인하십시오 (및 이중 및 삼중 체크)
다른 팁
SptipsLashes ()를 사용하여 데이터를 읽을 수 있습니다