Мне нужно загрузить из админки 3 разных файла, каждый из которых будет находиться в разных папках.2 - текстовые файлы, а 1 - файл изображения.
Я хочу познакомиться с примерами w3schools для части PHP, а именно:
ORIGINAL
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
, в то время как моя измененная версия для 3 файла имеет вид:
<?php
if ($_POST["rtestoita"]!="") {
$target_dirIT = "/filecomunicati/";
$target_fileIT = $target_dirIT . basename($_FILES["filetesto"]);
$uploadOkIT = 1;}
if ($_POST["rtestoing"]!="") {
$target_dirEN = "/filecomunicati/";
$target_fileEN = $target_dirEN . basename($_FILES["filetestoing"]);
$uploadOkEN = 1;}
if ($_POST["rfoto"]!=""){
$target_dirIMG = "/imgcomunicati/";
$target_fileIMG = $target_dirIMG . basename($_FILES["imagefile"]);
$uploadOkIMG = 1;
$imageFileType = strtolower(pathinfo($target_fileIMG,PATHINFO_EXTENSION));
}
if(isset($_POST["Go"])){
$servername = "xxx.xxx.xxx.xxx";
$username = "goofy";
$password = "mypassword";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if image file is a actual image or fake image
if ($_POST["imagefile"]!=""){
$check = getimagesize($_FILES["imagefile"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOkIMG = 0;
}
// Check file size
if ($_FILES["imagefile"]["size"] > 2000000) {
echo "Sorry, your file is too large.";
$uploadOkIMG = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOkIMG = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOkIMG == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["imagefile"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["imagefile"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
if ($_POST["testofile"]!=""){
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOkIT = 0;
}
}
// Check file size
if ($_FILES["testofile"]["size"] > 2000000) {
echo "Sorry, your file is too large.";
$uploadOkIT = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOkIT == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["testofile"]["tmp_" & $_POST["rtestoita"]], $target_file)) {
echo "The file ". basename( $_FILES["testofile"] & $_POST["rtestoita"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
if ($_POST["testofileing"]!=""){
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOkEN = 0;
}
}
// Check file size
if ($_FILES["testofileing"]["size"] > 2000000) {
echo "Sorry, your file is too large.";
$uploadOkEN = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOkEN == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["testofileing"]["tmp_" & $_POST["rtestoing"]], $target_file)) {
echo "The file ". basename( $_FILES["testofileing"] & $_POST["rtestoing"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
if (empty($_POST["rfoto"]))
{$sql = "INSERT INTO comunicati (id, idoperatore, operatore, data, titolo, argomento, titoloing, argomentoing, fileita, fileing, primapagina , img, foto, pathtxt, pathimg, tipotesto, tipoimg, note)
VALUES ('".$_POST["NULL"]."','".$_POST["idoperatore"]."','".$_POST["operatore"]."','".$_POST["data"]."','".$_POST["titolo"]."','".$_POST["argomento"]."', '".$_POST["titoloing"]."','".$_POST["argomentoing"]."','".$_POST["fileita"]."','".$_POST["fileing"]."','".$_POST["primapagina"]."','".$_POST["logopreso"]."', '".$_POST["logopreso"]."','".$_POST["pathtxt"]."','".$_POST["pathimg"]."','".$_POST["tipotesto"]."','".$_POST["tipoimg"]."','".$_POST["note"]."')";
} else
{$sql = "INSERT INTO comunicati (id, idoperatore, operatore, data, titolo, argomento, titoloing, argomentoing, fileita, fileing, primapagina , img, foto, pathtxt, pathimg, tipotesto, tipoimg, note)
VALUES ('".$_POST["NULL"]."','".$_POST["idoperatore"]."','".$_POST["operatore"]."','".$_POST["data"]."','".$_POST["titolo"]."','".$_POST["argomento"]."', '".$_POST["titoloing"]."','".$_POST["argomentoing"]."','".$_POST["fileita"]."','".$_POST["fileing"]."','".$_POST["primapagina"]."','".$_POST["rfoto"]."', '".$_POST["rfoto"]."','".$_POST["pathtxt"]."','".$_POST["pathimg"]."','".$_POST["tipotesto"]."','".$_POST["tipoimg"]."','".$_POST["note"]."')";
}
Но она не загружается.У меня также проблема в том, что во входном тексте, где должно быть написано только имя текстового файла, у меня есть c:\fakepath\myfile.ext
, а в поле моего БД для имени файла я получил c:fakepatmyfle.ext
.
Это мой HTML,У меня должен быть один и тот же текст (но только простой текст без пути), появляющийся во входном файле 3, в каждом соответствующем текстовом поле, поэтому я подумал поместить эти сценарии до </head>
<script>
document.getElementById( 'rfoto' ).value = document.getElementById( 'imagefile' ).files[ 0 ].name;
</script>
<script>
function copiaIT(){ $("#rtestoita").val($("#testofile").val());}
function copiaEN(){ $("#rtestoing").val($("#testofileing").val());}
function copiaIMG(){ $("#rfoto").val($("#imagefile").val());
}
</script>
Хотямоя форма:
<div class="container">
<h4>INSERIMENTO COMUNICATI</h4>
<div class="container">
<form class="form-horizontal" action=<?php echo $_SERVER['PHP_SELF']; ?> method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="id">id:</label>
<input type="text" class="form-control" id="id" placeholder="ID" name="id" size="5">
</div>
<div class="form-group">
<label for="idoperatore">id Operatore:</label>
<input type="text" class="form-control" id="idoperatore" placeholder="id Operatore" name="idoperatore" value="<?Php echo($idoperatore) ?>" Maxlength ="5"/>
</div>
<div class="form-group">
<label for="operatore">Operatore:</label>
<input type="text" class="form-control" id="operatore" placeholder="Operatore" name="operatore" value="<?Php echo($operatore) ?>" maxlength="20"/>
</div>
<div class="row">
<div class="col-sm-2 col-lg-2">
<div class="form-group date">
<label for="data">Data:</label>
<div class="input-group">
<input type="text" class="form-control" id="datepicker" placeholder="Data" name="data" value="<?php echo($data); ?>" readonly="readonly">
<span class="input-group-btn">
<button class="btn btn-default" type="button"><span class="glyphicon glyphicon-calendar"></span></button>
</div></div>
</div></div>
<div class="form-group">
<label for="testofile">File Testo Italiano:</label>
<input type="file"id="testofile" name="testofile" onchange="javascript:copiaIT()">
</div>
<div class="form-group">
<input name="rtestoita" class="form-control" id="rtestoita" type="text" value="<?Php echo($rtestoita) ?>" size="25"/>
<input type="hidden"id="pathtxt" class="form-control" name="pathtxt" value="filecomunicati/" >
<input type="hidden"id="tipotesto" class="form-control" name="tipotesto" value="text/plain" >
</div>
<div class="form-group">
<label for="testofileing">File Testo Inglese:</label>
<input name="testofileing" type="file" id="testofileing" onchange="javascript:copiaEN()">
</div>
<div class="form-group">
<input name="rtestoing" class="form-control" id="rtestoing" type="text" value="<?Php echo($rtestoing) ?>" size="25" readonly="readonly"/>
</div>
<div class="form-group">
<input type="file" name="imagefile" id="imagefile" onchange="javascript:copiaIMG()">
<input name="rfoto" id="rfoto" type="text" value="<?Php echo($rfoto) ?>" size="25" readonly/>
NB: SE NON VERRA' SELEZIONATO ALCUN FILE DA CARICARE, VERRA' INSERITO AUTOMATICAMENTE IL FILE "LOGOPRESO.JPG".
<input type="hidden"id="pathimg" class="form-control" name="pathimg" value="imgcomunicati/" >
<input type="hidden"id="tipoimg" class="form-control" name="tipoimg" value="image/jpeg" >
<input type="hidden"id="logopreso" class="form-control" name="logopreso" value="logopreso.JPG" >
</div>
<div class="form-group">
<label for="visualizza">Uploaded Image:</label>
<span id="uploaded_image"></span>
</div>
<div class="form-group">
<label for="titolo">Titolo Italiano:</label>
<input type="text" class="form-control" id="titolo" placeholder="Titolo" name="titolo" value="<?Php echo($titolo) ?>" maxlength="150"/>
</div>
<div class="form-group">
<label for="argomento">Argomento Italiano:</label>
<input type="text" class="form-control" id="argomento" placeholder="Argomento" name="argomento" value="<?Php echo($argomento) ?>" maxlength="150"/>
</div>
<div class="form-group">
<label for="titoloing">Titolo Inglese:</label>
<input type="text" class="form-control" id="titoloing" placeholder="Titolo inglese" name="titoloing" value="<?Php echo($titoloing) ?>" maxlength="150"/>
</div>
<div class="form-group">
<label for="argomentoing">Argomento Inglese:</label>
<input type="text" class="form-control" id="argomentoing" placeholder="Argomento inglese" name="argomentoing" value="<?Php echo($argomentoing) ?>" maxlength="150"/>
</div>
<div class="form-group">
<label for="note">Note:</label>
<input type="text" class="form-control" id="note" placeholder="Note" name="note" value="<?Php echo($note) ?>" maxlength="150"/>
</div>
<div class="form-group">
<label for="Prima Pagina">Prima pagina:</label>
<input name="primapagina" value="1" type="checkbox" id="checkbox" <?Php echo($checked) ?> >
</div>
<table align="center">
<button name="Go" value="Submit" type="submit" class="btn btn-success btn-block">Invia</button>
<br>
</table>
Я думаю, что это все ... Я поместил здесь много кода ... пожалуйста, не ненавидите меня :) Я просто хотел хорошо объяснить ситуацию в одном посте.
Заранее благодарим за помощь.