Barra rovesciata nel caricamento di file PHP
-
21-12-2019 - |
Domanda
Un po' di background:
Ho il sito web della nostra chiesa sul piano di hosting Windows condiviso di Godaddy.Anche se odio Windows e sposterei il sito su un server basato su Linux, questo non è il mio account di hosting, quindi non posso farlo.Attualmente sto cercando di implementare una GUI per caricare i sermoni sul sito web.So come farlo su un server Linux, quindi ho pensato che il codice Windows sarebbe stato simile.L'unico posto in cui mi blocco è il percorso di caricamento.Il percorso assoluto sul nostro server Godaddy è
D:\Hosting\5402716\html\WFBC\wacofamily.com\sermons\2014\
Tuttavia, la barra rovesciata evita i caratteri speciali, quindi ho pensato di averne bisogno di due.
D:\\Hosting\\5402716\\html\\WFBC\\wacofamily.com\\sermons\\2014\\
Il problema:
Ho quindi testato il caricamento, ma ottengo questo errore:
È stato ricevuto il seguente messaggio a livello di sistema:autorizzazioni o errore correlato nello spostamento del file in D:Hosting,02716htmlWFBCwacofamily.comsermons4WFBC20130106AM.mp3
Sembra che PHP stia eliminando tutte le barre rovesciate tranne \54 e \201.Secondo questo sito, \54 è una virgola e \201 è inutilizzato (o vuoto).Questo spiega perché ricevo la virgola e il 201 nel 2014 sta scomparendo.Ma non spiega perché la doppia barra rovesciata non diventi semplicemente una singola barra rovesciata.Ecco lo script PHP che dovrebbe caricare l'immagine:
<?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();
?>
Ecco il file app_config.php che definisce HOST_WWW_ROOT:
<?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;
}
}
?>
Cosa ho provato:
Ho provato le seguenti stringhe:
Quattro barre rovesciate (come nelle espressioni regolari):
define("HOST_WWW_ROOT", "D:\\\\Hosting\\\\5402716\\\\html\\\\WFBC\\\\wacofamily.com\\\\");
Barra rovesciata singola tra virgolette singole:
define("HOST_WWW_ROOT", 'D:\Hosting\5402716\html\WFBC\wacofamily.com\\');
Utilizzando \
, che è il codice carattere HTML per una barra rovesciata:
define("HOST_WWW_ROOT", "D:\Hosting\5402716\html\WFBC\wacofamily.com\");
Utilizzando \134
che dovrebbe essere la sequenza ottale per una barra rovesciata
define("HOST_WWW_ROOT", "D:\134Hosting\1345402716\134html\134WFBC\134wacofamily.com\134");
Utilizzando le barre come questa domanda disse:
define("HOST_WWW_ROOT", "D:/Hosting/402716/html/WFBC/wacofamily.com/");
Utilizzando DIRECTORY_SEPARATOR come suggerito da Machavity:
define("HOST_WWW_ROOT", "D:".DIRECTORY_SEPARATOR."Hosting".DIRECTORY_SEPARATOR ."402716".DIRECTORY_SEPARATOR ."html".DIRECTORY_SEPARATOR ."WFBC".DIRECTORY_SEPARATOR ."wacofamily.com".DIRECTORY_SEPARATOR );
E, naturalmente, la doppia barra rovesciata
define("HOST_WWW_ROOT", "D:\\Hosting\\5402716\\html\\WFBC\\wacofamily.com\\");
Ho apportato queste modifiche a tutte le stringhe coinvolte nella creazione del percorso di caricamento.Ho incluso solo la definizione HOST_WWW_ROOT per risparmiare spazio.
Come mi ha ricordato SirNarsh, se faccio eco al percorso che sto utilizzando, appare perfettamente con le barre rovesciate.Tuttavia, è quando passo il percorso alla funzione move_uploaded_file che qualcosa va in tilt.
Soluzione 2
Ok, l'ho capito e ora mi sento stupido.Alla fine ho usato le barre.Il motivo per cui prima non funzionava è che i permessi (che pensavo di aver controllato) non erano impostati per permettermi di scrivere.Quindi, per chiunque altro abbia questo problema:
- Utilizza le barre
- Controlla le tue autorizzazioni (e controllale due o tre volte)
Altri suggerimenti
Dovresti usare stripslashes() per leggere i dati